7396f31b92b091f80563f9d7631fe790ab8a7c62
[goodguy/history.git] / cinelerra-5.1 / plugins / findobj / findobj.h
1
2 /*
3  * CINELERRA
4  * Copyright (C) 1997-2012 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22
23
24 // This is mainly a test for object tracking
25
26
27
28
29 #ifndef FINDOBJ_H
30 #define FINDOBJ_H
31
32 //#include "config.h"
33
34 #include <math.h>
35 #include <stdint.h>
36 #include <string.h>
37
38 #include "affine.inc"
39 #include "bccmodels.h"
40 #include "bchash.inc"
41 #include "filexml.inc"
42 #include "keyframe.inc"
43 #include "findobj.inc"
44 #include "overlayframe.inc"
45 #include "pluginvclient.h"
46 #include "vframe.inc"
47
48 #define Mutex CvMutex
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"
56 #undef Mutex
57
58 #include <vector>
59
60 using namespace std;
61 using namespace cv;
62 using namespace cv::xfeatures2d;
63 using namespace cvflann;
64
65 // enabled detectors
66 #define _SIFT
67 #define _SURF
68 #define _ORB
69 #define _AKAZE
70 #define _BRISK
71
72 // Limits of global range in percent
73 #define MIN_RADIUS 1
74 #define MAX_RADIUS 200
75
76 // Limits of block size in percent.
77 #define MIN_BLOCK 1
78 #define MAX_BLOCK 100
79
80 #define MIN_LAYER 0
81 #define MAX_LAYER 255
82
83 #define MIN_BLEND 1
84 #define MAX_BLEND 100
85
86 #define NO_ALGORITHM   -1
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
92
93 #define MODE_NONE          -1
94 #define MODE_SQUARE         0
95 #define MODE_RHOMBUS        1
96 #define MODE_RECTANGLE      2
97 #define MODE_PARALLELOGRAM  3
98 #define MODE_QUADRILATERAL  4
99 #define MODE_MAX            5
100
101 class FindObjConfig
102 {
103 public:
104         FindObjConfig();
105
106         void reset();
107         int equivalent(FindObjConfig &that);
108         void copy_from(FindObjConfig &that);
109         void interpolate(FindObjConfig &prev, FindObjConfig &next,
110                 int64_t prev_frame, int64_t next_frame, int64_t current_frame);
111         void boundaries();
112
113         int algorithm, use_flann;
114         int drag_object, drag_scene, drag_replace;
115         float object_x, object_y, object_w, object_h;
116         float scene_x, scene_y, scene_w, scene_h;
117         float replace_x, replace_y, replace_w, replace_h;
118         float replace_dx, replace_dy;
119
120         int mode, scale, translate, rotate;
121         int draw_keypoints;
122         int draw_scene_border;
123         int replace_object;
124         int draw_object_border;
125         int draw_replace_border;
126
127         int object_layer;
128         int replace_layer;
129         int scene_layer;
130         int blend;
131 };
132
133 class FindObjMain : public PluginVClient
134 {
135 public:
136         FindObjMain(PluginServer *server);
137         ~FindObjMain();
138
139         int process_buffer(VFrame **frame, int64_t start_position, double frame_rate);
140 #ifdef _SIFT
141         void set_sift();
142 #endif
143 #ifdef _SURF
144         void set_surf();
145 #endif
146 #ifdef _ORB
147         void set_orb();
148 #endif
149 #ifdef _AKAZE
150         void set_akaze();
151 #endif
152 #ifdef _BRISK
153         void set_brisk();
154 #endif
155         void process_match();
156         void reshape();
157
158         void draw_vectors(VFrame *frame);
159         int is_multichannel();
160         int is_realtime();
161         void save_data(KeyFrame *keyframe);
162         void read_data(KeyFrame *keyframe);
163         void update_gui();
164
165         PLUGIN_CLASS_MEMBERS2(FindObjConfig)
166
167         AffineEngine *affine;
168         OverlayFrame *overlayer;
169         VFrame *object, *scene, *replace;
170
171         static void draw_line(VFrame *vframe, int x1, int y1, int x2, int y2);
172         static void draw_rect(VFrame *vframe, int x1, int y1, int x2, int y2);
173         static void draw_circle(VFrame *vframe, int x, int y, int r);
174
175         float object_x, object_y, object_w, object_h;
176         float scene_x, scene_y, scene_w, scene_h;
177         float replace_x, replace_y, replace_w, replace_h;
178         float replace_dx, replace_dy;
179
180         int w, h;
181         int object_layer;
182         int scene_layer;
183         int replace_layer;
184
185 // Latest coordinates of object in scene
186         float border_x1, border_y1;
187         float border_x2, border_y2;
188         float border_x3, border_y3;
189         float border_x4, border_y4;
190 // Coordinates of object in scene with blending
191         float obj_x1, obj_y1;
192         float obj_x2, obj_y2;
193         float obj_x3, obj_y3;
194         float obj_x4, obj_y4;
195         int init_border;
196
197 //opencv
198         typedef vector<DMatch> DMatchV;
199         typedef vector<DMatchV> DMatches;
200         typedef vector<KeyPoint> KeyPointV;
201         typedef vector<Point2f> ptV;
202
203         BC_CModel cvmodel;
204         Mat object_mat, scene_mat;
205         Mat obj_descrs;  KeyPointV obj_keypts;
206         Mat scn_descrs;  KeyPointV scn_keypts;
207         DMatches pairs;
208
209         static void to_mat(Mat &mat, int mcols, int mrows,
210                 VFrame *inp, int ix,int iy, BC_CModel mcolor_model);
211         void detect(Mat &mat, KeyPointV &keypts, Mat &descrs);
212         void match();
213         void filter_matches(ptV &p1, ptV &p2, double ratio=0.75);
214
215         Ptr<Feature2D> detector;
216         Ptr<DescriptorMatcher> matcher;
217         Ptr<DescriptorMatcher> flann_kdtree_matcher();
218         Ptr<DescriptorMatcher> flann_lshidx_matcher();
219         Ptr<DescriptorMatcher> bf_matcher_norm_l2();
220         Ptr<DescriptorMatcher> bf_matcher_norm_hamming();
221 };
222
223 #endif