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