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