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"
50 #include "opencv2/core/types.hpp"
51 #include "opencv2/core/mat.hpp"
52 #include "opencv2/imgproc/imgproc.hpp"
53 #include "opencv2/xfeatures2d.hpp"
54 #include "opencv2/calib3d.hpp"
55 #include "opencv2/flann/defines.h"
56 #include "opencv2/flann/params.h"
63 using namespace cv::xfeatures2d;
64 using namespace cvflann;
67 // _SIFT/_SURF are broken in v4, although they work in v3, no upgrade
74 // Limits of global range in percent
76 #define MAX_RADIUS 200
78 // Limits of block size in percent.
88 #define NO_ALGORITHM -1
89 #define ALGORITHM_SIFT 1
90 #define ALGORITHM_SURF 2
91 #define ALGORITHM_ORB 3
92 #define ALGORITHM_AKAZE 4
93 #define ALGORITHM_BRISK 5
97 #define MODE_RHOMBUS 1
98 #define MODE_RECTANGLE 2
99 #define MODE_PARALLELOGRAM 3
100 #define MODE_QUADRILATERAL 4
109 int equivalent(FindObjConfig &that);
110 void copy_from(FindObjConfig &that);
111 void interpolate(FindObjConfig &prev, FindObjConfig &next,
112 int64_t prev_frame, int64_t next_frame, int64_t current_frame);
115 int algorithm, use_flann, mode;
116 int drag_object, drag_scene, drag_replace;
117 float object_x, object_y, object_w, object_h;
118 float scene_x, scene_y, scene_w, scene_h;
119 float replace_x, replace_y, replace_w, replace_h;
120 float replace_dx, replace_dy;
122 int aspect, scale, translate, rotate;
123 int draw_keypoints, draw_match;
124 int draw_scene_border;
126 int draw_object_border;
127 int draw_replace_border;
135 class FindObjMain : public PluginVClient
138 FindObjMain(PluginServer *server);
141 int process_buffer(VFrame **frame, int64_t start_position, double frame_rate);
157 void process_match();
160 void draw_vectors(VFrame *frame);
161 int is_multichannel();
163 void save_data(KeyFrame *keyframe);
164 void read_data(KeyFrame *keyframe);
166 Track *get_plugin_track();
168 PLUGIN_CLASS_MEMBERS2(FindObjConfig)
170 AffineEngine *affine;
171 OverlayFrame *overlayer;
172 VFrame *object, *scene, *replace;
174 static void draw_point(VFrame *vframe,int x1, int y1);
175 static void draw_line(VFrame *vframe, int x1, int y1, int x2, int y2);
176 static void draw_quad(VFrame *vframe,
177 int x1, int y1, int x2, int y2,
178 int x3, int y3, int x4, int y4);
179 static void draw_rect(VFrame *vframe, int x1, int y1, int x2, int y2);
180 static void draw_circle(VFrame *vframe, int x, int y, int r);
182 float object_x, object_y, object_w, object_h;
183 float scene_x, scene_y, scene_w, scene_h;
184 float replace_x, replace_y, replace_w, replace_h;
185 float replace_dx, replace_dy;
192 // Latest coordinates of match / shape / object in scene
193 float match_x1, match_y1, shape_x1, shape_y1, out_x1, out_y1;
194 float match_x2, match_y2, shape_x2, shape_y2, out_x2, out_y2;
195 float match_x3, match_y3, shape_x3, shape_y3, out_x3, out_y3;
196 float match_x4, match_y4, shape_x4, shape_y4, out_x4, out_y4;
197 // Coordinates of object in scene with blending
201 typedef vector<DMatch> DMatchV;
202 typedef vector<DMatchV> DMatches;
203 typedef vector<KeyPoint> KeyPointV;
204 typedef vector<Point2f> ptV;
207 Mat object_mat, scene_mat;
208 Mat obj_descrs; KeyPointV obj_keypts;
209 Mat scn_descrs; KeyPointV scn_keypts;
212 static void to_mat(Mat &mat, int mcols, int mrows,
213 VFrame *inp, int ix,int iy, BC_CModel mcolor_model);
214 void detect(Mat &mat, KeyPointV &keypts, Mat &descrs);
216 void filter_matches(ptV &p1, ptV &p2, double ratio=0.75);
218 Ptr<Feature2D> detector;
219 Ptr<DescriptorMatcher> matcher;
220 Ptr<DescriptorMatcher> flann_kdtree_matcher();
221 Ptr<DescriptorMatcher> flann_lshidx_matcher();
222 Ptr<DescriptorMatcher> bf_matcher_norm_l2();
223 Ptr<DescriptorMatcher> bf_matcher_norm_hamming();