]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blob - src/kcf.h
Prepare for rotation branch merge
[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 <memory>
7 #include "fhog.hpp"
8
9 #ifdef CUFFT
10 #include "complexmat.cuh"
11 #include "cuda_functions.cuh"
12 #include "cuda_error_check.hpp"
13 #include <cuda_runtime.h>
14 #else
15 #include "complexmat.hpp"
16 #endif
17
18 #include "cnfeat.hpp"
19 #include "fft.h"
20 #include "pragmas.h"
21
22 class Kcf_Tracker_Private;
23 struct ThreadCtx;
24
25 struct BBox_c
26 {
27     double cx, cy, w, h;
28
29     inline void scale(double factor)
30     {
31         cx *= factor;
32         cy *= factor;
33         w  *= factor;
34         h  *= factor;
35     }
36
37     inline void scale_x(double factor)
38     {
39         cx *= factor;
40         w  *= factor;
41     }
42
43     inline void scale_y(double factor)
44     {
45         cy *= factor;
46         h  *= factor;
47     }
48
49     inline cv::Rect get_rect()
50     {
51         return cv::Rect(int(cx-w/2.), int(cy-h/2.), int(w), int(h));
52     }
53
54 };
55
56 class KCF_Tracker
57 {
58     friend ThreadCtx;
59 public:
60     bool m_debug     {false};
61     const bool m_use_scale {true};
62     const bool m_use_color {true};
63     const bool m_use_subpixel_localization {true};
64     const bool m_use_subgrid_scale {true};
65     const bool m_use_cnfeat {true};
66     const bool m_use_linearkernel {false};
67
68     /*
69     padding             ... extra area surrounding the target           (1.5)
70     kernel_sigma        ... gaussian kernel bandwidth                   (0.5)
71     lambda              ... regularization                              (1e-4)
72     interp_factor       ... linear interpolation factor for adaptation  (0.02)
73     output_sigma_factor ... spatial bandwidth (proportional to target)  (0.1)
74     cell_size           ... hog cell size                               (4)
75     */
76     KCF_Tracker(double padding, double kernel_sigma, double lambda, double interp_factor, double output_sigma_factor, int cell_size);
77     KCF_Tracker();
78     ~KCF_Tracker();
79
80     // Init/re-init methods
81     void init(cv::Mat & img, const cv::Rect & bbox, int fit_size_x, int fit_size_y);
82     void setTrackerPose(BBox_c & bbox, cv::Mat & img, int fit_size_x, int fit_size_y);
83     void updateTrackerPosition(BBox_c & bbox);
84
85     // frame-to-frame object tracking
86     void track(cv::Mat & img);
87     BBox_c getBBox();
88     double getFilterResponse() const; // Measure of tracking accuracy
89
90 private:
91     Fft &fft;
92
93     BBox_c p_pose;
94     double max_response = -1.;
95
96     bool p_resize_image = false;
97     bool p_fit_to_pw2 = false;
98
99     const double p_downscale_factor = 0.5;
100     double p_scale_factor_x = 1;
101     double p_scale_factor_y = 1;
102     const double p_floating_error = 0.0001;
103
104     const double p_padding = 1.5;
105     const double p_output_sigma_factor = 0.1;
106     double p_output_sigma;
107     const double p_kernel_sigma = 0.5;    //def = 0.5
108     const double p_lambda = 1e-4;         //regularization in learning step
109     const double p_interp_factor = 0.02;  //def = 0.02, linear interpolation factor for adaptation
110     const int p_cell_size = 4;            //4 for hog (= bin_size)
111     cv::Size p_windows_size;
112
113     const uint p_num_scales = m_use_scale ? 7 : 1;
114     const double p_scale_step = 1.02;
115     double p_current_scale = 1.;
116     double p_min_max_scale[2];
117     std::vector<double> p_scales;
118
119     const uint p_num_angles = 1;
120     const int p_angle_step = 10;
121     std::vector<double> p_angles = {0};
122
123     const int p_num_of_feats = 31 + (m_use_color ? 3 : 0) + (m_use_cnfeat ? 10 : 0);
124     cv::Size p_roi;
125
126     Kcf_Tracker_Private &d;
127
128     //model
129     ComplexMat p_yf;
130     ComplexMat p_model_alphaf;
131     ComplexMat p_model_alphaf_num;
132     ComplexMat p_model_alphaf_den;
133     ComplexMat p_model_xf;
134     ComplexMat p_xf;
135
136     class GaussianCorrelation {
137       public:
138         GaussianCorrelation(uint num_scales, cv::Size size)
139             : xf_sqr_norm(num_scales)
140             , xyf(Fft::freq_size(size), 1, num_scales)
141             , ifft_res(num_scales, size)
142             , k(num_scales, size)
143         {}
144         void operator()(ComplexMat &result, const ComplexMat &xf, const ComplexMat &yf, double sigma, bool auto_correlation, const KCF_Tracker &kcf);
145
146       private:
147         DynMem xf_sqr_norm;
148         DynMem yf_sqr_norm{1};
149         ComplexMat xyf;
150         MatScales ifft_res;
151         MatScales k;
152     };
153
154     //helping functions
155     void scale_track(ThreadCtx &vars, cv::Mat &input_rgb, cv::Mat &input_gray);
156     cv::Mat get_subwindow(const cv::Mat &input, int cx, int cy, int size_x, int size_y) const;
157     cv::Mat gaussian_shaped_labels(double sigma, int dim1, int dim2);
158     std::unique_ptr<GaussianCorrelation> gaussian_correlation;
159     cv::Mat circshift(const cv::Mat &patch, int x_rot, int y_rot);
160     cv::Mat cosine_window_function(int dim1, int dim2);
161     cv::Mat get_features(cv::Mat &input_rgb, cv::Mat &input_gray, int cx, int cy, int size_x, int size_y, double scale) const;
162     cv::Point2f sub_pixel_peak(cv::Point &max_loc, cv::Mat &response) const;
163     double sub_grid_scale(uint index);
164     void resizeImgs(cv::Mat &input_rgb, cv::Mat &input_gray);
165     void train(cv::Mat input_rgb, cv::Mat input_gray, double interp_factor);
166     double findMaxReponse(uint &max_idx, cv::Point2f &new_location) const;
167 };
168
169 #endif //KCF_HEADER_6565467831231