]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blob - src/kcf.h
Gaussian correlation for CUFFT version is now on GPU. Also corrected << operator...
[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 cv::Rect get_rect()
33     {
34         return cv::Rect(cx-w/2., cy-h/2., w, h);
35     }
36
37 };
38
39 class KCF_Tracker
40 {
41 public:
42     bool m_debug     {false};
43     bool m_use_scale {true};
44     bool m_use_color {true};
45 #ifdef ASYNC
46     bool m_use_multithreading {true};
47 #else
48     bool m_use_multithreading {false};
49 #endif //ASYNC
50     bool m_use_subpixel_localization {true};
51     bool m_use_subgrid_scale {true};
52     bool m_use_cnfeat {true};
53     bool m_use_linearkernel {false};
54 #ifdef BIG_BATCH
55     bool m_use_big_batch {true};
56 #else
57     bool m_use_big_batch {false};
58 #endif
59 #ifdef BIG_BATCH
60     bool m_use_cuda {true};
61 #else
62     bool m_use_cuda {false};
63 #endif
64
65     /*
66     padding             ... extra area surrounding the target           (1.5)
67     kernel_sigma        ... gaussian kernel bandwidth                   (0.5)
68     lambda              ... regularization                              (1e-4)
69     interp_factor       ... linear interpolation factor for adaptation  (0.02)
70     output_sigma_factor ... spatial bandwidth (proportional to target)  (0.1)
71     cell_size           ... hog cell size                               (4)
72     */
73     KCF_Tracker(double padding, double kernel_sigma, double lambda, double interp_factor, double output_sigma_factor, int cell_size);
74     KCF_Tracker();
75     ~KCF_Tracker();
76
77     // Init/re-init methods
78     void init(cv::Mat & img, const cv::Rect & bbox);
79     void setTrackerPose(BBox_c & bbox, cv::Mat & img);
80     void updateTrackerPosition(BBox_c & bbox);
81
82     // frame-to-frame object tracking
83     void track(cv::Mat & img);
84     BBox_c getBBox();
85
86 private:
87     Fft &fft;
88
89     BBox_c p_pose;
90     bool p_resize_image = false;
91
92     bool first = true;
93
94     const double p_downscale_factor = 0.5;
95
96     double p_padding = 1.5;
97     double p_output_sigma_factor = 0.1;
98     double p_output_sigma;
99     double p_kernel_sigma = 0.5;    //def = 0.5
100     double p_lambda = 1e-4;         //regularization in learning step
101     double p_interp_factor = 0.02;  //def = 0.02, linear interpolation factor for adaptation
102     int p_cell_size = 4;            //4 for hog (= bin_size)
103     int p_windows_size[2];
104     int p_num_scales {7};
105     double p_scale_step = 1.02;
106     double p_current_scale = 1.;
107     double p_min_max_scale[2];
108     std::vector<double> p_scales;
109
110     //for big batch
111     int p_num_of_feats;
112     int p_roi_height, p_roi_width;
113     float *xf_sqr_norm = nullptr, *yf_sqr_norm = nullptr;
114 #ifdef CUFFT
115     float *xf_sqr_norm_d = nullptr, *yf_sqr_norm_d = nullptr, *gauss_corr_res = nullptr;
116 #endif
117
118     //model
119     ComplexMat p_yf;
120     ComplexMat p_model_alphaf;
121     ComplexMat p_model_alphaf_num;
122     ComplexMat p_model_alphaf_den;
123     ComplexMat p_model_xf;
124     //helping functions
125     cv::Mat get_subwindow(const cv::Mat & input, int cx, int cy, int size_x, int size_y);
126     cv::Mat gaussian_shaped_labels(double sigma, int dim1, int dim2);
127     ComplexMat gaussian_correlation(const ComplexMat & xf, const ComplexMat & yf, double sigma, bool auto_correlation = false);
128     cv::Mat circshift(const cv::Mat & patch, int x_rot, int y_rot);
129     cv::Mat cosine_window_function(int dim1, int dim2);
130     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.);
131     cv::Point2f sub_pixel_peak(cv::Point & max_loc, cv::Mat & response);
132     double sub_grid_scale(std::vector<double> & responses, int index = -1);
133
134 };
135
136 #endif //KCF_HEADER_6565467831231