]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blob - src/kcf.h
Prepare for using cuFFTW
[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 #include "complexmat.hpp"
8 #include "cnfeat.hpp"
9
10 struct BBox_c
11 {
12     double cx, cy, w, h;
13
14     inline void scale(double factor)
15     {
16         cx *= factor;
17         cy *= factor;
18         w  *= factor;
19         h  *= factor;
20     }
21
22     inline cv::Rect get_rect()
23     {
24         return cv::Rect(cx-w/2., cy-h/2., w, h);
25     }
26
27 };
28
29 class KCF_Tracker
30 {
31 public:
32 #ifdef OPENCV_CUFFT
33     bool m_use_scale {false};
34     bool m_use_color {false};
35 #else //OPENCV_CUFFT
36     bool m_use_scale {true};
37     bool m_use_color {true};
38 #endif //OPENCV_CUFFT
39 #ifdef ASYNC
40     bool m_use_multithreading {true};
41 #else
42     bool m_use_multithreading {false};
43 #endif //ASYNC
44     bool m_use_subpixel_localization {true};
45     bool m_use_subgrid_scale {true};
46     bool m_use_cnfeat {true};
47     bool m_use_linearkernel {false};
48
49     /*
50     padding             ... extra area surrounding the target           (1.5)
51     kernel_sigma        ... gaussian kernel bandwidth                   (0.5)
52     lambda              ... regularization                              (1e-4)
53     interp_factor       ... linear interpolation factor for adaptation  (0.02)
54     output_sigma_factor ... spatial bandwidth (proportional to target)  (0.1)
55     cell_size           ... hog cell size                               (4)
56     */
57     KCF_Tracker(double padding, double kernel_sigma, double lambda, double interp_factor, double output_sigma_factor, int cell_size) :
58         p_padding(padding), p_output_sigma_factor(output_sigma_factor), p_kernel_sigma(kernel_sigma),
59         p_lambda(lambda), p_interp_factor(interp_factor), p_cell_size(cell_size) {}
60     KCF_Tracker() {}
61
62     // Init/re-init methods
63     void init(cv::Mat & img, const cv::Rect & bbox);
64     void setTrackerPose(BBox_c & bbox, cv::Mat & img);
65     void updateTrackerPosition(BBox_c & bbox);
66
67     // frame-to-frame object tracking
68     void track(cv::Mat & img);
69     BBox_c getBBox();
70
71 private:
72     BBox_c p_pose;
73     bool p_resize_image = false;
74
75     bool first = true;
76
77     double p_padding = 1.5;
78     double p_output_sigma_factor = 0.1;
79     double p_output_sigma;
80     double p_kernel_sigma = 0.5;    //def = 0.5
81     double p_lambda = 1e-4;         //regularization in learning step
82     double p_interp_factor = 0.02;  //def = 0.02, linear interpolation factor for adaptation
83     int p_cell_size = 4;            //4 for hog (= bin_size)
84     int p_windows_size[2];
85     cv::Mat p_cos_window;
86     int p_num_scales {7};
87     double p_scale_step = 1.02;
88     double p_current_scale = 1.;
89     double p_min_max_scale[2];
90     std::vector<double> p_scales;
91
92 #ifdef OPENCV_CUFFT
93     cv::cuda::GpuMat src_gpu,dst_gpu,p_cos_window_d;
94     cv::cuda::Stream stream;
95 #endif //OPENCV_CUFFT
96
97     //model
98     ComplexMat p_yf;
99     ComplexMat p_model_alphaf;
100     ComplexMat p_model_alphaf_num;
101     ComplexMat p_model_alphaf_den;
102     ComplexMat p_model_xf;
103
104     //helping functions
105     cv::Mat get_subwindow(const cv::Mat & input, int cx, int cy, int size_x, int size_y);
106     cv::Mat gaussian_shaped_labels(double sigma, int dim1, int dim2);
107     ComplexMat gaussian_correlation(const ComplexMat & xf, const ComplexMat & yf, double sigma, bool auto_correlation = false);
108     cv::Mat circshift(const cv::Mat & patch, int x_rot, int y_rot);
109     cv::Mat cosine_window_function(int dim1, int dim2);
110     ComplexMat fft2(const cv::Mat & input);
111     ComplexMat fft2(const std::vector<cv::Mat> & input, const cv::Mat & cos_window);
112
113     cv::Mat ifft2(const ComplexMat & inputf);
114     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.);
115     cv::Point2f sub_pixel_peak(cv::Point & max_loc, cv::Mat & response);
116     double sub_grid_scale(std::vector<double> & responses, int index = -1);
117
118 };
119
120 #endif //KCF_HEADER_6565467831231