]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blob - src/kcf.h
f748ffd358cef147fe4e4bd4c08c168a863d4265
[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, a;
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 class KCF_Tracker
52 {
53 public:
54     bool m_debug {false};
55         bool m_visual_debug {false};
56     bool m_use_scale {false};
57     bool m_use_angle {true}; //Currently only works when m_use_scale is off and m_use_subpixel_localization too and used on RotatingBox dataset.
58     bool m_use_color {true};
59 #ifdef ASYNC
60     bool m_use_multithreading {true};
61 #else
62     bool m_use_multithreading {false};
63 #endif //ASYNC
64     bool m_use_subpixel_localization {true};
65     bool m_use_subgrid_scale {true};
66     bool m_use_cnfeat {true};
67     bool m_use_linearkernel {false};
68 #ifdef BIG_BATCH
69     bool m_use_big_batch {true};
70 #else
71     bool m_use_big_batch {false};
72 #endif
73 #ifdef BIG_BATCH
74     bool m_use_cuda {true};
75 #else
76     bool m_use_cuda {false};
77 #endif
78
79     /*
80     padding             ... extra area surrounding the target           (1.5)
81     kernel_sigma        ... gaussian kernel bandwidth                   (0.5)
82     lambda              ... regularization                              (1e-4)
83     interp_factor       ... linear interpolation factor for adaptation  (0.02)
84     output_sigma_factor ... spatial bandwidth (proportional to target)  (0.1)
85     cell_size           ... hog cell size                               (4)
86     */
87     KCF_Tracker(double padding, double kernel_sigma, double lambda, double interp_factor, double output_sigma_factor, int cell_size);
88     KCF_Tracker();
89     ~KCF_Tracker();
90
91     // Init/re-init methods
92     void init(cv::Mat & img, const cv::Rect & bbox, int fit_size_x, int fit_size_y);
93     void setTrackerPose(BBox_c & bbox, cv::Mat & img, int fit_size_x, int fit_size_y);
94     void updateTrackerPosition(BBox_c & bbox);
95
96     // frame-to-frame object tracking
97     void track(cv::Mat & img);
98     BBox_c getBBox();
99
100 private:
101     Fft &fft;
102
103     BBox_c p_pose;
104     bool p_resize_image = false;
105     bool p_fit_to_pw2 = false;
106
107     bool first = true;
108
109     const double p_downscale_factor = 0.5;
110     double p_scale_factor_x = 1;
111     double p_scale_factor_y = 1;
112
113     double p_padding = 1.5;
114     double p_output_sigma_factor = 0.1;
115     double p_output_sigma;
116     double p_kernel_sigma = 0.5;    //def = 0.5
117     double p_lambda = 1e-4;         //regularization in learning step
118     double p_interp_factor = 0.02;  //def = 0.02, linear interpolation factor for adaptation
119     int p_cell_size = 4;            //4 for hog (= bin_size)
120     int p_windows_size[2];
121     int p_num_scales {5};
122     double p_scale_step = 1.02;
123     double p_current_scale = 1.;
124     double p_min_max_scale[2];
125     std::vector<double> p_scales;
126     int p_num_angles {5};
127     int p_current_angle = 0;
128     int p_angle_min  = -20, p_angle_max = 20;
129     int p_angle_step = 10;
130     std::vector<double> p_angles;
131
132     //for visual debug
133     int p_debug_image_size = 100;
134     int p_count = 0;
135     std::vector<cv::Mat> p_debug_scale_responses;
136     std::vector<cv::Mat> p_debug_subwindows;
137
138     //for big batch
139     int p_num_of_feats;
140     int p_roi_height, p_roi_width;
141     float *xf_sqr_norm = nullptr, *yf_sqr_norm = nullptr;
142 #ifdef CUFFT
143     float *xf_sqr_norm_d = nullptr, *yf_sqr_norm_d = nullptr, *gauss_corr_res = nullptr;
144 #endif
145
146     //model
147     ComplexMat p_yf;
148     ComplexMat p_model_alphaf;
149     ComplexMat p_model_alphaf_num;
150     ComplexMat p_model_alphaf_den;
151     ComplexMat p_model_xf;
152     //helping functions
153     cv::Mat get_subwindow(const cv::Mat & input, int cx, int cy, int size_x, int size_y);
154     cv::Mat gaussian_shaped_labels(double sigma, int dim1, int dim2);
155     ComplexMat gaussian_correlation(const ComplexMat & xf, const ComplexMat & yf, double sigma, bool auto_correlation = false);
156     cv::Mat circshift(const cv::Mat & patch, int x_rot, int y_rot);
157     cv::Mat cosine_window_function(int dim1, int dim2);
158     std::vector<cv::Mat> get_features(cv::Mat & input_rgb, cv::Mat & input_gray);
159     void geometric_transformations(cv::Mat & patch, int size_x, int size_y, double scale = 1, int angle = 0, bool allow_debug = true);
160     cv::Point2f sub_pixel_peak(cv::Point & max_loc, cv::Mat & response);
161     double sub_grid_scale(std::vector<double> & responses, int index = -1);
162
163 };
164
165 #endif //KCF_HEADER_6565467831231