]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blob - src/kcf.h
Unify GaussianCorrelation constructor
[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/cuda_error_check.cuh"
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     bool m_use_scale {true};
62     bool m_use_color {true};
63 #ifdef ASYNC
64     bool m_use_multithreading {true};
65 #else
66     bool m_use_multithreading {false};
67 #endif //ASYNC
68     bool m_use_subpixel_localization {true};
69     bool m_use_subgrid_scale {true};
70     bool m_use_cnfeat {true};
71     bool m_use_linearkernel {false};
72 #ifdef CUFFT
73     bool m_use_cuda {true};
74 #else
75     bool m_use_cuda {false};
76 #endif
77
78     /*
79     padding             ... extra area surrounding the target           (1.5)
80     kernel_sigma        ... gaussian kernel bandwidth                   (0.5)
81     lambda              ... regularization                              (1e-4)
82     interp_factor       ... linear interpolation factor for adaptation  (0.02)
83     output_sigma_factor ... spatial bandwidth (proportional to target)  (0.1)
84     cell_size           ... hog cell size                               (4)
85     */
86     KCF_Tracker(double padding, double kernel_sigma, double lambda, double interp_factor, double output_sigma_factor, int cell_size);
87     KCF_Tracker();
88     ~KCF_Tracker();
89
90     // Init/re-init methods
91     void init(cv::Mat & img, const cv::Rect & bbox, int fit_size_x, int fit_size_y);
92     void setTrackerPose(BBox_c & bbox, cv::Mat & img, int fit_size_x, int fit_size_y);
93     void updateTrackerPosition(BBox_c & bbox);
94
95     // frame-to-frame object tracking
96     void track(cv::Mat & img);
97     BBox_c getBBox();
98     double getFilterResponse() const; // Measure of tracking accuracy
99
100 private:
101     Fft &fft;
102
103     BBox_c p_pose;
104     double max_response = -1.;
105
106     bool p_resize_image = false;
107     bool p_fit_to_pw2 = false;
108
109     const double p_downscale_factor = 0.5;
110     double p_scale_factor_x = 1;
111     double p_scale_factor_y = 1;
112     const double p_floating_error = 0.0001;
113
114     double p_padding = 1.5;
115     double p_output_sigma_factor = 0.1;
116     double p_output_sigma;
117     double p_kernel_sigma = 0.5;    //def = 0.5
118     double p_lambda = 1e-4;         //regularization in learning step
119     double p_interp_factor = 0.02;  //def = 0.02, linear interpolation factor for adaptation
120     int p_cell_size = 4;            //4 for hog (= bin_size)
121     cv::Size p_windows_size;
122     uint p_num_scales {7};
123     double p_scale_step = 1.02;
124     double p_current_scale = 1.;
125     double p_min_max_scale[2];
126     std::vector<double> p_scales;
127
128     const int p_num_of_feats = 31 + m_use_color ? 3 : 0 + m_use_cnfeat ? 10 : 0;
129     cv::Size p_roi;
130
131     Kcf_Tracker_Private &d;
132
133     //model
134     ComplexMat p_yf;
135     ComplexMat p_model_alphaf;
136     ComplexMat p_model_alphaf_num;
137     ComplexMat p_model_alphaf_den;
138     ComplexMat p_model_xf;
139     ComplexMat p_xf;
140
141     class GaussianCorrelation {
142       public:
143         GaussianCorrelation(cv::Size size, uint num_scales, uint num_feats)
144             : xf_sqr_norm(num_scales)
145             , xyf(Fft::freq_size(size), num_scales)
146             , ifft_res({int(num_feats * num_scales), size.height, size.width}, CV_32F)
147             , k({int(num_scales), size.height, size.width}, CV_32F)
148         {}
149         void operator()(const KCF_Tracker &kcf, ComplexMat &result, const ComplexMat &xf, const ComplexMat &yf, double sigma, bool auto_correlation = false);
150
151       private:
152         DynMem xf_sqr_norm;
153         DynMem yf_sqr_norm{sizeof(float)};
154         ComplexMat xyf;
155         MatDynMem ifft_res;
156         MatDynMem k;
157     };
158
159     //helping functions
160     void scale_track(ThreadCtx &vars, cv::Mat &input_rgb, cv::Mat &input_gray);
161     cv::Mat get_subwindow(const cv::Mat &input, int cx, int cy, int size_x, int size_y);
162     MatDynMem gaussian_shaped_labels(double sigma, int dim1, int dim2);
163     std::unique_ptr<GaussianCorrelation> gaussian_correlation;
164     MatDynMem circshift(const cv::Mat &patch, int x_rot, int y_rot);
165     cv::Mat cosine_window_function(int dim1, int dim2);
166     void get_features(MatDynMem &feat_3d, cv::Mat &input_rgb, cv::Mat &input_gray, int cx, int cy, int size_x, int size_y, double scale);
167     cv::Point2f sub_pixel_peak(cv::Point &max_loc, cv::Mat &response);
168     double sub_grid_scale(uint index);
169     void resizeImgs(cv::Mat &input_rgb, cv::Mat &input_gray);
170     void train(cv::Mat input_gray, cv::Mat input_rgb, double interp_factor);
171 };
172
173 #endif //KCF_HEADER_6565467831231