8510cd9dc385dbd80715b2a6af0ef3faa6c7e402
[goodguy/cinelerra.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 // _SIFT/_SURF are broken in v4, although they work in v3, no upgrade
67 #define _SIFT
68 #define _SURF
69 #define _ORB
70 #define _AKAZE
71 #define _BRISK
72
73 // Limits of global range in percent
74 #define MIN_RADIUS 1
75 #define MAX_RADIUS 200
76
77 // Limits of block size in percent.
78 #define MIN_BLOCK 1
79 #define MAX_BLOCK 100
80
81 #define MIN_LAYER 0
82 #define MAX_LAYER 255
83
84 #define MIN_BLEND 1
85 #define MAX_BLEND 100
86
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
93
94 #define MODE_NONE          -1
95 #define MODE_SQUARE         0
96 #define MODE_RHOMBUS        1
97 #define MODE_RECTANGLE      2
98 #define MODE_PARALLELOGRAM  3
99 #define MODE_QUADRILATERAL  4
100 #define MODE_MAX            5
101
102 class FindObjConfig
103 {
104 public:
105         FindObjConfig();
106
107         void reset();
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);
112         void boundaries();
113
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;
120
121         int aspect, scale, translate, rotate;
122         int draw_keypoints, draw_match;
123         int draw_scene_border;
124         int replace_object;
125         int draw_object_border;
126         int draw_replace_border;
127
128         int object_layer;
129         int replace_layer;
130         int scene_layer;
131         int blend;
132 };
133
134 class FindObjMain : public PluginVClient
135 {
136 public:
137         FindObjMain(PluginServer *server);
138         ~FindObjMain();
139
140         int process_buffer(VFrame **frame, int64_t start_position, double frame_rate);
141 #ifdef _SIFT
142         void set_sift();
143 #endif
144 #ifdef _SURF
145         void set_surf();
146 #endif
147 #ifdef _ORB
148         void set_orb();
149 #endif
150 #ifdef _AKAZE
151         void set_akaze();
152 #endif
153 #ifdef _BRISK
154         void set_brisk();
155 #endif
156         void process_match();
157         void reshape();
158
159         void draw_vectors(VFrame *frame);
160         int is_multichannel();
161         int is_realtime();
162         void save_data(KeyFrame *keyframe);
163         void read_data(KeyFrame *keyframe);
164         void update_gui();
165
166         PLUGIN_CLASS_MEMBERS2(FindObjConfig)
167
168         AffineEngine *affine;
169         OverlayFrame *overlayer;
170         VFrame *object, *scene, *replace;
171
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);
179
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;
184
185         int w, h;
186         int object_layer;
187         int scene_layer;
188         int replace_layer;
189
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
196         int init_border;
197
198 //opencv
199         typedef vector<DMatch> DMatchV;
200         typedef vector<DMatchV> DMatches;
201         typedef vector<KeyPoint> KeyPointV;
202         typedef vector<Point2f> ptV;
203
204         BC_CModel cvmodel;
205         Mat object_mat, scene_mat;
206         Mat obj_descrs;  KeyPointV obj_keypts;
207         Mat scn_descrs;  KeyPointV scn_keypts;
208         DMatches pairs;
209
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);
213         void match();
214         void filter_matches(ptV &p1, ptV &p2, double ratio=0.75);
215
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();
222 };
223
224 #endif