4 * Copyright (C) 1997-2012 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 // This is mainly a test for object tracking
39 #include "bccmodels.h"
41 #include "filexml.inc"
42 #include "keyframe.inc"
43 #include "findobj.inc"
44 #include "overlayframe.inc"
45 #include "pluginvclient.h"
49 #include "opencv2/core/types.hpp"
50 #include "opencv2/core/mat.hpp"
51 #include "opencv2/imgproc/imgproc.hpp"
52 #include "opencv2/xfeatures2d.hpp"
53 #include "opencv2/calib3d.hpp"
54 #include "opencv2/flann/defines.h"
55 #include "opencv2/flann/params.h"
62 using namespace cv::xfeatures2d;
63 using namespace cvflann;
66 // _SIFT/_SURF are broken in v4, although they work in v3, no upgrade
73 // Limits of global range in percent
75 #define MAX_RADIUS 200
77 // Limits of block size in percent.
87 #define NO_ALGORITHM -1
88 #define ALGORITHM_SIFT 1
89 #define ALGORITHM_SURF 2
90 #define ALGORITHM_ORB 3
91 #define ALGORITHM_AKAZE 4
92 #define ALGORITHM_BRISK 5
96 #define MODE_RHOMBUS 1
97 #define MODE_RECTANGLE 2
98 #define MODE_PARALLELOGRAM 3
99 #define MODE_QUADRILATERAL 4
108 int equivalent(FindObjConfig &that);
109 void copy_from(FindObjConfig &that);
110 void interpolate(FindObjConfig &prev, FindObjConfig &next,
111 int64_t prev_frame, int64_t next_frame, int64_t current_frame);
114 int algorithm, use_flann, mode;
115 int drag_object, drag_scene, drag_replace;
116 float object_x, object_y, object_w, object_h;
117 float scene_x, scene_y, scene_w, scene_h;
118 float replace_x, replace_y, replace_w, replace_h;
119 float replace_dx, replace_dy;
121 int aspect, scale, translate, rotate;
122 int draw_keypoints, draw_match;
123 int draw_scene_border;
125 int draw_object_border;
126 int draw_replace_border;
134 class FindObjMain : public PluginVClient
137 FindObjMain(PluginServer *server);
140 int process_buffer(VFrame **frame, int64_t start_position, double frame_rate);
156 void process_match();
159 void draw_vectors(VFrame *frame);
160 int is_multichannel();
162 void save_data(KeyFrame *keyframe);
163 void read_data(KeyFrame *keyframe);
166 PLUGIN_CLASS_MEMBERS2(FindObjConfig)
168 AffineEngine *affine;
169 OverlayFrame *overlayer;
170 VFrame *object, *scene, *replace;
172 static void draw_point(VFrame *vframe,int x1, int y1);
173 static void draw_line(VFrame *vframe, int x1, int y1, int x2, int y2);
174 static void draw_quad(VFrame *vframe,
175 int x1, int y1, int x2, int y2,
176 int x3, int y3, int x4, int y4);
177 static void draw_rect(VFrame *vframe, int x1, int y1, int x2, int y2);
178 static void draw_circle(VFrame *vframe, int x, int y, int r);
180 float object_x, object_y, object_w, object_h;
181 float scene_x, scene_y, scene_w, scene_h;
182 float replace_x, replace_y, replace_w, replace_h;
183 float replace_dx, replace_dy;
190 // Latest coordinates of match / shape / object in scene
191 float match_x1, match_y1, shape_x1, shape_y1, out_x1, out_y1;
192 float match_x2, match_y2, shape_x2, shape_y2, out_x2, out_y2;
193 float match_x3, match_y3, shape_x3, shape_y3, out_x3, out_y3;
194 float match_x4, match_y4, shape_x4, shape_y4, out_x4, out_y4;
195 // Coordinates of object in scene with blending
199 typedef vector<DMatch> DMatchV;
200 typedef vector<DMatchV> DMatches;
201 typedef vector<KeyPoint> KeyPointV;
202 typedef vector<Point2f> ptV;
205 Mat object_mat, scene_mat;
206 Mat obj_descrs; KeyPointV obj_keypts;
207 Mat scn_descrs; KeyPointV scn_keypts;
210 static void to_mat(Mat &mat, int mcols, int mrows,
211 VFrame *inp, int ix,int iy, BC_CModel mcolor_model);
212 void detect(Mat &mat, KeyPointV &keypts, Mat &descrs);
214 void filter_matches(ptV &p1, ptV &p2, double ratio=0.75);
216 Ptr<Feature2D> detector;
217 Ptr<DescriptorMatcher> matcher;
218 Ptr<DescriptorMatcher> flann_kdtree_matcher();
219 Ptr<DescriptorMatcher> flann_lshidx_matcher();
220 Ptr<DescriptorMatcher> bf_matcher_norm_l2();
221 Ptr<DescriptorMatcher> bf_matcher_norm_hamming();