remove whitespace at eol
[goodguy/history.git] / cinelerra-5.1 / plugins / findobject / findobject.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 FINDOBJECT_H
30 #define FINDOBJECT_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 "bchash.inc"
40 #include "filexml.inc"
41 #include "keyframe.inc"
42 #include "findobjectwindow.inc"
43 #include "overlayframe.inc"
44 #include "pluginvclient.h"
45 #include "vframe.inc"
46
47 #include "opencv2/core/core_c.h"
48 #include "opencv2/objdetect/objdetect.hpp"
49 #include "opencv2/core/mat.hpp"
50 #include "opencv2/imgproc/types_c.h"
51 #include "opencv2/imgproc/imgproc.hpp"
52 #include "opencv2/legacy/blobtrack.hpp"
53
54 class FindObjectMain;
55 class FindObjectWindow;
56
57 using namespace cv;
58
59
60
61
62 // Limits of global range in percent
63 #define MIN_RADIUS 1
64 #define MAX_RADIUS 200
65
66 // Limits of block size in percent.
67 #define MIN_BLOCK 1
68 #define MAX_BLOCK 100
69
70 #define MIN_LAYER 0
71 #define MAX_LAYER 255
72
73 #define MIN_BLEND 1
74 #define MAX_BLEND 100
75
76 // Sizes must be quantized a certain amount for OpenCV
77 #define QUANTIZE 8
78
79 // Storage for results
80 #define FINDOBJECT_FILE "/tmp/findobject"
81
82 #define MIN_CAMSHIFT 0
83 #define MAX_CAMSHIFT 256
84
85
86 #define NO_ALGORITHM 0
87 #if HAVE_OPENCV_SURF
88 #define ALGORITHM_SURF 1
89 #endif
90 #define ALGORITHM_CAMSHIFT 2
91 #define ALGORITHM_BLOB 3
92
93 class FindObjectConfig
94 {
95 public:
96         FindObjectConfig();
97
98         int equivalent(FindObjectConfig &that);
99         void copy_from(FindObjectConfig &that);
100         void interpolate(FindObjectConfig &prev,
101                 FindObjectConfig &next,
102                 int64_t prev_frame,
103                 int64_t next_frame,
104                 int64_t current_frame);
105         void boundaries();
106
107         int global_range_w;
108         int global_range_h;
109 // Block size as percent of image size
110 // Object must be a rectangle for the algorithm to work,
111 // so no oblique edges unless we also do an affine transform.
112         float global_block_w;
113         float global_block_h;
114 // Block position in percentage 0 - 100
115         float block_x;
116         float block_y;
117 // Draw key points
118         int draw_keypoints;
119 // Draw border over object in scene layer
120         int draw_border;
121 // Draw transparency over object in object layer
122         int replace_object;
123 // Draw border over object
124         int draw_object_border;
125
126 // Which layer is the object 0 or 1
127         int object_layer;
128 // Which layer replaces the object
129         int replace_layer;
130 // Which layer is the object searched for in
131         int scene_layer;
132
133         int algorithm;
134 // Camshift parameters
135         int vmin, vmax, smin;
136 // Amount to blend new object position in
137         int blend;
138 };
139
140
141
142
143 class FindObjectMain : public PluginVClient
144 {
145 public:
146         FindObjectMain(PluginServer *server);
147         ~FindObjectMain();
148
149         int process_buffer(VFrame **frame,
150                 int64_t start_position,
151                 double frame_rate);
152         void draw_vectors(VFrame *frame);
153         int is_multichannel();
154         int is_realtime();
155         void save_data(KeyFrame *keyframe);
156         void read_data(KeyFrame *keyframe);
157         void update_gui();
158 // Calculate frame to copy from and frame to move
159         void calculate_pointers(VFrame **frame, VFrame **src, VFrame **dst);
160         void allocate_temp(int w, int h, int color_model);
161
162         PLUGIN_CLASS_MEMBERS2(FindObjectConfig)
163
164
165         AffineEngine *affine;
166 // Temporary for affine overlay
167         VFrame *temp;
168         OverlayFrame *overlayer;
169
170         static void draw_pixel(VFrame *frame, int x, int y);
171         static void draw_line(VFrame *frame, int x1, int y1, int x2, int y2);
172         static void draw_rect(VFrame *frame, int x1, int y1, int x2, int y2);
173
174         void grey_crop(unsigned char *dst,
175                 VFrame *src,
176                 int x1,
177                 int y1,
178                 int x2,
179                 int y2,
180                 int dst_w,
181                 int dst_h);
182
183
184
185         void process_surf();
186         void process_camshift();
187         void process_blob();
188
189
190
191 // clamped coordinates
192         int object_w;
193         int object_h;
194         int object_x1;
195         int object_y1;
196         int object_x2;
197         int object_y2;
198         int scene_w;
199         int scene_h;
200         int scene_x1;
201         int scene_y1;
202         int scene_x2;
203         int scene_y2;
204 // input frame size
205         int w, h;
206 // clamped layers
207         int object_layer;
208         int scene_layer;
209         int replace_layer;
210
211 // Latest coordinates of object in scene
212         int border_x1;
213         int border_y1;
214         int border_x2;
215         int border_y2;
216         int border_x3;
217         int border_y3;
218         int border_x4;
219         int border_y4;
220 // Coordinates of object in scene with blending
221         float border_x1_accum;
222         float border_y1_accum;
223         float border_x2_accum;
224         float border_y2_accum;
225         float border_x3_accum;
226         float border_y3_accum;
227         float border_x4_accum;
228         float border_y4_accum;
229         int init_border;
230
231
232         IplImage *object_image;
233         IplImage *scene_image;
234
235
236 // Comparison with current object_image
237         unsigned char *prev_object;
238 // Quantized sizes
239         int object_image_w;
240         int object_image_h;
241         int scene_image_w;
242         int scene_image_h;
243         CvSeq *object_keypoints;
244         CvSeq *object_descriptors;
245         CvSeq *scene_keypoints;
246         CvSeq *scene_descriptors;
247         CvMemStorage *storage;
248
249 // camshift
250 // object histogram
251         Mat hist;
252         Rect trackWindow;
253
254
255 // Blob
256         int blob_initialized;
257         CvBlobTrackerAutoParam1 blob_param;
258         CvBlobTrackerAuto* blob_pTracker;
259
260
261 };
262
263
264
265
266
267
268
269
270
271
272 #endif
273
274
275
276
277
278