]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blob - src/kcf.h
Added Big Batch mode for forward_window, which enables to transforming all features...
[hercules2020/kcf.git] / src / kcf.h
1 #ifndef KCF_HEADER_6565467831231
2 #define KCF_HEADER_6565467831231
3
4 #include <opencv2/opencv.hpp>
5 #include <vector>
6 #include "fhog.hpp"
7 #include "complexmat.hpp"
8 #include "cnfeat.hpp"
9 #include "fft.h"
10
11 struct BBox_c
12 {
13     double cx, cy, w, h;
14
15     inline void scale(double factor)
16     {
17         cx *= factor;
18         cy *= factor;
19         w  *= factor;
20         h  *= factor;
21     }
22
23     inline cv::Rect get_rect()
24     {
25         return cv::Rect(cx-w/2., cy-h/2., w, h);
26     }
27
28 };
29
30 class KCF_Tracker
31 {
32 public:
33     bool m_debug     {false};
34 #ifdef OPENCV_CUFFT
35     bool m_use_scale {false};
36     bool m_use_color {false};
37 #else //OPENCV_CUFFT
38     bool m_use_scale {true};
39     bool m_use_color {true};
40 #endif //OPENCV_CUFFT
41 #ifdef ASYNC
42     bool m_use_multithreading {true};
43 #else
44     bool m_use_multithreading {false};
45 #endif //ASYNC
46     bool m_use_subpixel_localization {true};
47     bool m_use_subgrid_scale {true};
48     bool m_use_cnfeat {true};
49     bool m_use_linearkernel {false};
50 #ifdef BIG_BATCH
51     bool m_use_big_batch {true};
52 #else
53     bool m_use_big_batch {false};
54 #endif
55
56     /*
57     padding             ... extra area surrounding the target           (1.5)
58     kernel_sigma        ... gaussian kernel bandwidth                   (0.5)
59     lambda              ... regularization                              (1e-4)
60     interp_factor       ... linear interpolation factor for adaptation  (0.02)
61     output_sigma_factor ... spatial bandwidth (proportional to target)  (0.1)
62     cell_size           ... hog cell size                               (4)
63     */
64     KCF_Tracker(double padding, double kernel_sigma, double lambda, double interp_factor, double output_sigma_factor, int cell_size);
65     KCF_Tracker();
66     ~KCF_Tracker();
67
68     // Init/re-init methods
69     void init(cv::Mat & img, const cv::Rect & bbox);
70     void setTrackerPose(BBox_c & bbox, cv::Mat & img);
71     void updateTrackerPosition(BBox_c & bbox);
72
73     // frame-to-frame object tracking
74     void track(cv::Mat & img);
75     BBox_c getBBox();
76
77 private:
78     Fft &fft;
79
80     BBox_c p_pose;
81     bool p_resize_image = false;
82
83     bool first = true;
84
85     const double p_downscale_factor = 0.5;
86
87     double p_padding = 1.5;
88     double p_output_sigma_factor = 0.1;
89     double p_output_sigma;
90     double p_kernel_sigma = 0.5;    //def = 0.5
91     double p_lambda = 1e-4;         //regularization in learning step
92     double p_interp_factor = 0.02;  //def = 0.02, linear interpolation factor for adaptation
93     int p_cell_size = 4;            //4 for hog (= bin_size)
94     int p_windows_size[2];
95     int p_num_scales {7};
96     double p_scale_step = 1.02;
97     double p_current_scale = 1.;
98     double p_min_max_scale[2];
99     std::vector<double> p_scales;
100
101     //for big batch
102     int num_of_feats;
103
104     //model
105     ComplexMat p_yf;
106     ComplexMat p_model_alphaf;
107     ComplexMat p_model_alphaf_num;
108     ComplexMat p_model_alphaf_den;
109     ComplexMat p_model_xf;
110     //helping functions
111     cv::Mat get_subwindow(const cv::Mat & input, int cx, int cy, int size_x, int size_y);
112     cv::Mat gaussian_shaped_labels(double sigma, int dim1, int dim2);
113     ComplexMat gaussian_correlation(const ComplexMat & xf, const ComplexMat & yf, double sigma, bool auto_correlation = false);
114     cv::Mat circshift(const cv::Mat & patch, int x_rot, int y_rot);
115     cv::Mat cosine_window_function(int dim1, int dim2);
116     std::vector<cv::Mat> get_features(cv::Mat & input_rgb, cv::Mat & input_gray, int cx, int cy, int size_x, int size_y, double scale = 1.);
117     cv::Point2f sub_pixel_peak(cv::Point & max_loc, cv::Mat & response);
118     double sub_grid_scale(std::vector<double> & responses, int index = -1);
119
120 };
121
122 #endif //KCF_HEADER_6565467831231