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