remove filepng little-endian, reactivate sift in opencv
[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 "track.inc"
47 #include "vframe.inc"
48
49 #define Mutex CvMutex
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"
57 #undef Mutex
58
59 #include <vector>
60
61 using namespace std;
62 using namespace cv;
63 using namespace cv::xfeatures2d;
64 using namespace cvflann;
65
66 // enabled detectors
67 #define _SIFT
68 // _SURF broken in v4, although works in v3 (patented)
69 //#define _SURF
70 #define _ORB
71 #define _AKAZE
72 #define _BRISK
73
74 // Limits of global range in percent
75 #define MIN_RADIUS 1
76 #define MAX_RADIUS 200
77
78 // Limits of block size in percent.
79 #define MIN_BLOCK 1
80 #define MAX_BLOCK 100
81
82 #define MIN_LAYER 0
83 #define MAX_LAYER 255
84
85 #define MIN_BLEND 1
86 #define MAX_BLEND 100
87
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
94
95 #define MODE_NONE          -1
96 #define MODE_SQUARE         0
97 #define MODE_RHOMBUS        1
98 #define MODE_RECTANGLE      2
99 #define MODE_PARALLELOGRAM  3
100 #define MODE_QUADRILATERAL  4
101 #define MODE_MAX            5
102
103 class FindObjConfig
104 {
105 public:
106         FindObjConfig();
107
108         void reset();
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);
113         void boundaries();
114
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;
121
122         int aspect, scale, translate, rotate;
123         int draw_keypoints, draw_match;
124         int draw_scene_border;
125         int replace_object;
126         int draw_object_border;
127         int draw_replace_border;
128
129         int object_layer;
130         int replace_layer;
131         int scene_layer;
132         int blend;
133 };
134
135 class FindObjMain : public PluginVClient
136 {
137 public:
138         FindObjMain(PluginServer *server);
139         ~FindObjMain();
140
141         int process_buffer(VFrame **frame, int64_t start_position, double frame_rate);
142 #ifdef _SIFT
143         void set_sift();
144 #endif
145 #ifdef _SURF
146         void set_surf();
147 #endif
148 #ifdef _ORB
149         void set_orb();
150 #endif
151 #ifdef _AKAZE
152         void set_akaze();
153 #endif
154 #ifdef _BRISK
155         void set_brisk();
156 #endif
157         void process_match();
158         void reshape();
159
160         void draw_vectors(VFrame *frame);
161         int is_multichannel();
162         int is_realtime();
163         void save_data(KeyFrame *keyframe);
164         void read_data(KeyFrame *keyframe);
165         void update_gui();
166         Track *get_plugin_track();
167
168         PLUGIN_CLASS_MEMBERS2(FindObjConfig)
169
170         AffineEngine *affine;
171         OverlayFrame *overlayer;
172         VFrame *object, *scene, *replace;
173
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);
181
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;
186
187         int w, h;
188         int object_layer;
189         int scene_layer;
190         int replace_layer;
191
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
198         int init_border;
199
200 //opencv
201         typedef vector<DMatch> DMatchV;
202         typedef vector<DMatchV> DMatches;
203         typedef vector<KeyPoint> KeyPointV;
204         typedef vector<Point2f> ptV;
205
206         BC_CModel cvmodel;
207         Mat object_mat, scene_mat;
208         Mat obj_descrs;  KeyPointV obj_keypts;
209         Mat scn_descrs;  KeyPointV scn_keypts;
210         DMatches pairs;
211
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);
215         void match();
216         void filter_matches(ptV &p1, ptV &p2, double ratio=0.75);
217
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();
224 };
225
226 #endif