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