]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blob - src/kcf.h
Replace Kcf::m_use_big_batch with a preprocessor symbol
[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 {
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 void scale_x(double factor)
36     {
37         cx *= factor;
38         w  *= factor;
39     }
40
41     inline void scale_y(double factor)
42     {
43         cy *= factor;
44         h  *= factor;
45     }
46
47     inline cv::Rect get_rect()
48     {
49         return cv::Rect(int(cx-w/2.), int(cy-h/2.), int(w), int(h));
50     }
51
52 };
53
54 class KCF_Tracker
55 {
56 public:
57     bool m_debug     {false};
58     bool m_use_scale {true};
59     bool m_use_color {true};
60 #ifdef ASYNC
61     bool m_use_multithreading {true};
62 #else
63     bool m_use_multithreading {false};
64 #endif //ASYNC
65     bool m_use_subpixel_localization {true};
66     bool m_use_subgrid_scale {true};
67     bool m_use_cnfeat {true};
68     bool m_use_linearkernel {false};
69 #ifdef CUFFT
70     bool m_use_cuda {true};
71 #else
72     bool m_use_cuda {false};
73 #endif
74
75     /*
76     padding             ... extra area surrounding the target           (1.5)
77     kernel_sigma        ... gaussian kernel bandwidth                   (0.5)
78     lambda              ... regularization                              (1e-4)
79     interp_factor       ... linear interpolation factor for adaptation  (0.02)
80     output_sigma_factor ... spatial bandwidth (proportional to target)  (0.1)
81     cell_size           ... hog cell size                               (4)
82     */
83     KCF_Tracker(double padding, double kernel_sigma, double lambda, double interp_factor, double output_sigma_factor, int cell_size);
84     KCF_Tracker();
85     ~KCF_Tracker();
86
87     // Init/re-init methods
88     void init(cv::Mat & img, const cv::Rect & bbox, int fit_size_x, int fit_size_y);
89     void setTrackerPose(BBox_c & bbox, cv::Mat & img, int fit_size_x, int fit_size_y);
90     void updateTrackerPosition(BBox_c & bbox);
91
92     // frame-to-frame object tracking
93     void track(cv::Mat & img);
94     BBox_c getBBox();
95     double getFilterResponse() const; // Measure of tracking accuracy
96
97 private:
98     Fft &fft;
99
100     BBox_c p_pose;
101     double max_response = -1.;
102
103     bool p_resize_image = false;
104     bool p_fit_to_pw2 = false;
105
106     const double p_downscale_factor = 0.5;
107     double p_scale_factor_x = 1;
108     double p_scale_factor_y = 1;
109     const double p_floating_error = 0.0001;
110
111     double p_padding = 1.5;
112     double p_output_sigma_factor = 0.1;
113     double p_output_sigma;
114     double p_kernel_sigma = 0.5;    //def = 0.5
115     double p_lambda = 1e-4;         //regularization in learning step
116     double p_interp_factor = 0.02;  //def = 0.02, linear interpolation factor for adaptation
117     int p_cell_size = 4;            //4 for hog (= bin_size)
118     cv::Size p_windows_size;
119     uint p_num_scales {7};
120     double p_scale_step = 1.02;
121     double p_current_scale = 1.;
122     double p_min_max_scale[2];
123     std::vector<double> p_scales;
124
125     //for big batch
126     int p_num_of_feats;
127     cv::Size p_roi;
128
129     std::vector<ThreadCtx> p_threadctxs;
130
131     //CUDA compability
132     cv::Mat p_rot_labels;
133     DynMem p_rot_labels_data;
134
135     //model
136     ComplexMat p_yf;
137     ComplexMat p_model_alphaf;
138     ComplexMat p_model_alphaf_num;
139     ComplexMat p_model_alphaf_den;
140     ComplexMat p_model_xf;
141     ComplexMat p_xf;
142     //helping functions
143     void scale_track(ThreadCtx & vars, cv::Mat & input_rgb, cv::Mat & input_gray);
144     cv::Mat get_subwindow(const cv::Mat & input, int cx, int cy, int size_x, int size_y);
145     cv::Mat gaussian_shaped_labels(double sigma, int dim1, int dim2);
146     void gaussian_correlation(struct ThreadCtx &vars, const ComplexMat & xf, const ComplexMat & yf, double sigma, bool auto_correlation = false);
147     cv::Mat circshift(const cv::Mat & patch, int x_rot, int y_rot);
148     cv::Mat cosine_window_function(int dim1, int dim2);
149     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.);
150     cv::Point2f sub_pixel_peak(cv::Point & max_loc, cv::Mat & response);
151     double sub_grid_scale(uint index);
152
153 };
154
155 #endif //KCF_HEADER_6565467831231