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