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;
72 // Limits of global range in percent
74 #define MAX_RADIUS 200
76 // Limits of block size in percent.
86 #define NO_ALGORITHM 0
87 #define ALGORITHM_SIFT 1
88 #define ALGORITHM_SURF 2
89 #define ALGORITHM_ORB 3
90 #define ALGORITHM_AKAZE 4
91 #define ALGORITHM_BRISK 5
98 int equivalent(FindObjConfig &that);
99 void copy_from(FindObjConfig &that);
100 void interpolate(FindObjConfig &prev, FindObjConfig &next,
101 int64_t prev_frame, int64_t next_frame, int64_t current_frame);
104 int algorithm, use_flann;
105 int drag_object, drag_scene;
106 float object_x, object_y, object_w, object_h;
107 float scene_x, scene_y, scene_w, scene_h;
110 int draw_scene_border;
112 int draw_object_border;
120 class FindObjMain : public PluginVClient
123 FindObjMain(PluginServer *server);
126 int process_buffer(VFrame **frame, int64_t start_position, double frame_rate);
142 void process_match();
144 void draw_vectors(VFrame *frame);
145 int is_multichannel();
147 void save_data(KeyFrame *keyframe);
148 void read_data(KeyFrame *keyframe);
151 PLUGIN_CLASS_MEMBERS2(FindObjConfig)
153 AffineEngine *affine;
154 OverlayFrame *overlayer;
155 VFrame *object, *scene, *replace;
157 static void draw_line(VFrame *vframe, int x1, int y1, int x2, int y2);
158 static void draw_rect(VFrame *vframe, int x1, int y1, int x2, int y2);
159 static void draw_circle(VFrame *vframe, int x, int y, int r);
161 int object_x, object_y, object_w, object_h;
162 int scene_x, scene_y, scene_w, scene_h;
169 // Latest coordinates of object in scene
170 int border_x1, border_y1;
171 int border_x2, border_y2;
172 int border_x3, border_y3;
173 int border_x4, border_y4;
174 // Coordinates of object in scene with blending
175 float obj_x1, obj_y1;
176 float obj_x2, obj_y2;
177 float obj_x3, obj_y3;
178 float obj_x4, obj_y4;
182 typedef vector<DMatch> DMatchV;
183 typedef vector<DMatchV> DMatches;
184 typedef vector<KeyPoint> KeyPointV;
185 typedef vector<Point2f> ptV;
188 Mat object_mat, scene_mat;
189 Mat obj_descrs; KeyPointV obj_keypts;
190 Mat scn_descrs; KeyPointV scn_keypts;
193 static void to_mat(Mat &mat, int mcols, int mrows,
194 VFrame *inp, int ix,int iy, BC_CModel mcolor_model);
195 void detect(Mat &mat, KeyPointV &keypts, Mat &descrs);
197 void filter_matches(ptV &p1, ptV &p2, double ratio=0.75);
199 Ptr<Feature2D> detector;
200 Ptr<DescriptorMatcher> matcher;
201 Ptr<DescriptorMatcher> flann_kdtree_matcher();
202 Ptr<DescriptorMatcher> flann_lshidx_matcher();
203 Ptr<DescriptorMatcher> bf_matcher_norm_l2();
204 Ptr<DescriptorMatcher> bf_matcher_norm_hamming();