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"
48 #include "opencv2/core/types.hpp"
49 #include "opencv2/core/mat.hpp"
50 #include "opencv2/imgproc/imgproc.hpp"
51 #include "opencv2/xfeatures2d.hpp"
52 #include "opencv2/calib3d.hpp"
53 #include "opencv2/flann/defines.h"
54 #include "opencv2/flann/params.h"
60 using namespace cv::xfeatures2d;
61 using namespace cvflann;
70 // Limits of global range in percent
72 #define MAX_RADIUS 200
74 // Limits of block size in percent.
84 #define NO_ALGORITHM 0
85 #define ALGORITHM_SIFT 1
86 #define ALGORITHM_SURF 2
87 #define ALGORITHM_ORB 3
88 #define ALGORITHM_AKAZE 4
89 #define ALGORITHM_BRISK 5
96 int equivalent(FindObjConfig &that);
97 void copy_from(FindObjConfig &that);
98 void interpolate(FindObjConfig &prev, FindObjConfig &next,
99 int64_t prev_frame, int64_t next_frame, int64_t current_frame);
102 int algorithm, use_flann;
103 float object_x, object_y, object_w, object_h;
104 float scene_x, scene_y, scene_w, scene_h;
109 int draw_object_border;
117 class FindObjMain : public PluginVClient
120 FindObjMain(PluginServer *server);
123 int process_buffer(VFrame **frame, int64_t start_position, double frame_rate);
139 void process_match();
141 void draw_vectors(VFrame *frame);
142 int is_multichannel();
144 void save_data(KeyFrame *keyframe);
145 void read_data(KeyFrame *keyframe);
148 PLUGIN_CLASS_MEMBERS2(FindObjConfig)
150 AffineEngine *affine;
151 OverlayFrame *overlayer;
152 VFrame *object, *scene, *replace;
154 static void draw_line(VFrame *vframe, int x1, int y1, int x2, int y2);
155 static void draw_rect(VFrame *vframe, int x1, int y1, int x2, int y2);
156 static void draw_circle(VFrame *vframe, int x, int y, int r);
158 int object_x, object_y, object_w, object_h;
159 int scene_x, scene_y, scene_w, scene_h;
166 // Latest coordinates of object in scene
167 int border_x1, border_y1;
168 int border_x2, border_y2;
169 int border_x3, border_y3;
170 int border_x4, border_y4;
171 // Coordinates of object in scene with blending
172 float obj_x1, obj_y1;
173 float obj_x2, obj_y2;
174 float obj_x3, obj_y3;
175 float obj_x4, obj_y4;
179 typedef vector<DMatch> DMatchV;
180 typedef vector<DMatchV> DMatches;
181 typedef vector<KeyPoint> KeyPointV;
182 typedef vector<Point2f> ptV;
185 Mat object_mat, scene_mat;
186 Mat obj_descrs; KeyPointV obj_keypts;
187 Mat scn_descrs; KeyPointV scn_keypts;
190 static void to_mat(Mat &mat, int mcols, int mrows,
191 VFrame *inp, int ix,int iy, BC_CModel mcolor_model);
192 void detect(Mat &mat, KeyPointV &keypts, Mat &descrs);
194 void filter_matches(ptV &p1, ptV &p2, double ratio=0.75);
196 Ptr<Feature2D> detector;
197 Ptr<DescriptorMatcher> matcher;
198 Ptr<DescriptorMatcher> flann_kdtree_matcher();
199 Ptr<DescriptorMatcher> flann_lshidx_matcher();
200 Ptr<DescriptorMatcher> bf_matcher_norm_l2();
201 Ptr<DescriptorMatcher> bf_matcher_norm_hamming();