rework findobject, add opecv_build
[goodguy/history.git] / cinelerra-5.1 / plugins / findobject / findobject.h
index b8a0a434ce3b983427a1c72d9a89b22420901665..7825d25f9dd69acf0d3aed4450ad40cd82252530 100644 (file)
 #include <string.h>
 
 #include "affine.inc"
+#include "bccmodels.h"
 #include "bchash.inc"
 #include "filexml.inc"
 #include "keyframe.inc"
-#include "findobjectwindow.inc"
+#include "findobject.inc"
 #include "overlayframe.inc"
 #include "pluginvclient.h"
 #include "vframe.inc"
 
-#include "opencv2/core/core_c.h"
-#include "opencv2/objdetect/objdetect.hpp"
+#include "opencv2/core/types.hpp"
 #include "opencv2/core/mat.hpp"
-#include "opencv2/imgproc/types_c.h"
 #include "opencv2/imgproc/imgproc.hpp"
-#include "opencv2/legacy/blobtrack.hpp"
+#include "opencv2/xfeatures2d.hpp"
+#include "opencv2/calib3d.hpp"
+#include "opencv2/flann/defines.h"
+#include "opencv2/flann/params.h"
 
-class FindObjectMain;
-class FindObjectWindow;
+#include <vector>
 
+using namespace std;
 using namespace cv;
+using namespace cv::xfeatures2d;
+using namespace cvflann;
 
-
-
+// enabled detectors
+#define _SIFT
+#define _SURF
+#define _ORB
+#define _AKAZE
+#define _BRISK
 
 // Limits of global range in percent
 #define MIN_RADIUS 1
@@ -73,22 +81,12 @@ using namespace cv;
 #define MIN_BLEND 1
 #define MAX_BLEND 100
 
-// Sizes must be quantized a certain amount for OpenCV
-#define QUANTIZE 8
-
-// Storage for results
-#define FINDOBJECT_FILE "/tmp/findobject"
-
-#define MIN_CAMSHIFT 0
-#define MAX_CAMSHIFT 256
-
-
 #define NO_ALGORITHM 0
-#if HAVE_OPENCV_SURF
-#define ALGORITHM_SURF 1
-#endif
-#define ALGORITHM_CAMSHIFT 2
-#define ALGORITHM_BLOB 3
+#define ALGORITHM_SIFT  1
+#define ALGORITHM_SURF  2
+#define ALGORITHM_ORB   3
+#define ALGORITHM_AKAZE 4
+#define ALGORITHM_BRISK 5
 
 class FindObjectConfig
 {
@@ -97,182 +95,110 @@ public:
 
        int equivalent(FindObjectConfig &that);
        void copy_from(FindObjectConfig &that);
-       void interpolate(FindObjectConfig &prev,
-               FindObjectConfig &next,
-               int64_t prev_frame,
-               int64_t next_frame,
-               int64_t current_frame);
+       void interpolate(FindObjectConfig &prev, FindObjectConfig &next,
+               int64_t prev_frame, int64_t next_frame, int64_t current_frame);
        void boundaries();
 
-       int global_range_w;
-       int global_range_h;
-// Block size as percent of image size
-// Object must be a rectangle for the algorithm to work,
-// so no oblique edges unless we also do an affine transform.
-       float global_block_w;
-       float global_block_h;
-// Block position in percentage 0 - 100
-       float block_x;
-       float block_y;
-// Draw key points
+       int algorithm, use_flann;
+       float object_x, object_y, object_w, object_h;
+       float scene_x, scene_y,   scene_w, scene_h;
+
        int draw_keypoints;
-// Draw border over object in scene layer
        int draw_border;
-// Draw transparency over object in object layer
        int replace_object;
-// Draw border over object
        int draw_object_border;
 
-// Which layer is the object 0 or 1
        int object_layer;
-// Which layer replaces the object
        int replace_layer;
-// Which layer is the object searched for in
        int scene_layer;
-
-       int algorithm;
-// Camshift parameters
-       int vmin, vmax, smin;
-// Amount to blend new object position in
        int blend;
 };
 
-
-
-
 class FindObjectMain : public PluginVClient
 {
 public:
        FindObjectMain(PluginServer *server);
        ~FindObjectMain();
 
-       int process_buffer(VFrame **frame,
-               int64_t start_position,
-               double frame_rate);
+       int process_buffer(VFrame **frame, int64_t start_position, double frame_rate);
+#ifdef _SIFT
+       void set_sift();
+#endif
+#ifdef _SURF
+       void set_surf();
+#endif
+#ifdef _ORB
+       void set_orb();
+#endif
+#ifdef _AKAZE
+       void set_akaze();
+#endif
+#ifdef _BRISK
+       void set_brisk();
+#endif
+       void process_match();
+
        void draw_vectors(VFrame *frame);
        int is_multichannel();
        int is_realtime();
        void save_data(KeyFrame *keyframe);
        void read_data(KeyFrame *keyframe);
        void update_gui();
-// Calculate frame to copy from and frame to move
-       void calculate_pointers(VFrame **frame, VFrame **src, VFrame **dst);
-       void allocate_temp(int w, int h, int color_model);
 
        PLUGIN_CLASS_MEMBERS2(FindObjectConfig)
 
-
        AffineEngine *affine;
-// Temporary for affine overlay
-       VFrame *temp;
        OverlayFrame *overlayer;
+       VFrame *object, *scene, *replace;
+
+       static void draw_line(VFrame *vframe, int x1, int y1, int x2, int y2);
+       static void draw_rect(VFrame *vframe, int x1, int y1, int x2, int y2);
+       static void draw_circle(VFrame *vframe, int x, int y, int r);
+
+       int object_x, object_y, object_w, object_h;
+       int scene_x, scene_y, scene_w, scene_h;
 
-       static void draw_pixel(VFrame *frame, int x, int y);
-       static void draw_line(VFrame *frame, int x1, int y1, int x2, int y2);
-       static void draw_rect(VFrame *frame, int x1, int y1, int x2, int y2);
-
-       void grey_crop(unsigned char *dst,
-               VFrame *src,
-               int x1,
-               int y1,
-               int x2,
-               int y2,
-               int dst_w,
-               int dst_h);
-
-
-
-       void process_surf();
-       void process_camshift();
-       void process_blob();
-
-
-
-// clamped coordinates
-       int object_w;
-       int object_h;
-       int object_x1;
-       int object_y1;
-       int object_x2;
-       int object_y2;
-       int scene_w;
-       int scene_h;
-       int scene_x1;
-       int scene_y1;
-       int scene_x2;
-       int scene_y2;
-// input frame size
        int w, h;
-// clamped layers
        int object_layer;
        int scene_layer;
        int replace_layer;
 
 // Latest coordinates of object in scene
-       int border_x1;
-       int border_y1;
-       int border_x2;
-       int border_y2;
-       int border_x3;
-       int border_y3;
-       int border_x4;
-       int border_y4;
+       int border_x1, border_y1;
+       int border_x2, border_y2;
+       int border_x3, border_y3;
+       int border_x4, border_y4;
 // Coordinates of object in scene with blending
-       float border_x1_accum;
-       float border_y1_accum;
-       float border_x2_accum;
-       float border_y2_accum;
-       float border_x3_accum;
-       float border_y3_accum;
-       float border_x4_accum;
-       float border_y4_accum;
+       float obj_x1, obj_y1;
+       float obj_x2, obj_y2;
+       float obj_x3, obj_y3;
+       float obj_x4, obj_y4;
        int init_border;
 
-
-       IplImage *object_image;
-       IplImage *scene_image;
-
-
-// Comparison with current object_image
-       unsigned char *prev_object;
-// Quantized sizes
-       int object_image_w;
-       int object_image_h;
-       int scene_image_w;
-       int scene_image_h;
-       CvSeq *object_keypoints;
-       CvSeq *object_descriptors;
-       CvSeq *scene_keypoints;
-       CvSeq *scene_descriptors;
-       CvMemStorage *storage;
-
-// camshift
-// object histogram
-       Mat hist;
-       Rect trackWindow;
-
-
-// Blob
-       int blob_initialized;
-       CvBlobTrackerAutoParam1 blob_param;
-       CvBlobTrackerAuto* blob_pTracker;
-
-
+//opencv
+       typedef vector<DMatch> DMatchV;
+       typedef vector<DMatchV> DMatches;
+       typedef vector<KeyPoint> KeyPointV;
+       typedef vector<Point2f> ptV;
+
+       BC_CModel cvmodel;
+       Mat object_mat, scene_mat;
+       Mat obj_descrs;  KeyPointV obj_keypts;
+       Mat scn_descrs;  KeyPointV scn_keypts;
+       DMatches pairs;
+
+       static void to_mat(Mat &mat, int mcols, int mrows,
+               VFrame *inp, int ix,int iy, BC_CModel mcolor_model);
+       void detect(Mat &mat, KeyPointV &keypts, Mat &descrs);
+       void match();
+       void filter_matches(ptV &p1, ptV &p2, double ratio=0.75);
+
+       Ptr<Feature2D> detector;
+       Ptr<DescriptorMatcher> matcher;
+       Ptr<DescriptorMatcher> flann_kdtree_matcher();
+       Ptr<DescriptorMatcher> flann_lshidx_matcher();
+       Ptr<DescriptorMatcher> bf_matcher_norm_l2();
+       Ptr<DescriptorMatcher> bf_matcher_norm_hamming();
 };
 
-
-
-
-
-
-
-
-
-
 #endif
-
-
-
-
-
-