]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blob - src/kcf.h
Do not use virtual methods in FFT class
[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 #include "complexmat.hpp"
10 #ifdef CUFFT
11 #include "cuda_error_check.hpp"
12 #include <cuda_runtime.h>
13 #endif
14
15 #include "cnfeat.hpp"
16 #ifdef FFTW
17 #include "fft_fftw.h"
18 #define FFT Fftw
19 #elif defined(CUFFT)
20 #include "fft_cufft.h"
21 #define FFT cuFFT
22 #else
23 #include "fft_opencv.h"
24 #define FFT FftOpencv
25 #endif
26 #include "pragmas.h"
27
28 class Kcf_Tracker_Private;
29 struct ThreadCtx;
30
31 struct BBox_c
32 {
33     double cx, cy, w, h, a;
34
35     inline cv::Point2d center() const { return cv::Point2d(cx, cy); }
36
37     inline void scale(double factor)
38     {
39         cx *= factor;
40         cy *= factor;
41         w  *= factor;
42         h  *= factor;
43     }
44
45     inline cv::Rect get_rect()
46     {
47         return cv::Rect(int(cx-w/2.), int(cy-h/2.), int(w), int(h));
48     }
49
50 };
51
52 class KCF_Tracker
53 {
54     friend ThreadCtx;
55     friend Kcf_Tracker_Private;
56 public:
57     bool m_debug {false};
58     enum class vd {NONE, PATCH, RESPONSE} m_visual_debug {vd::NONE};
59     constexpr static bool m_use_scale {true};
60     constexpr static bool m_use_color {true};
61     constexpr static bool m_use_subpixel_localization {true};
62     constexpr static bool m_use_subgrid_scale {true};
63     constexpr static bool m_use_subgrid_angle {true};
64     constexpr static bool m_use_cnfeat {true};
65     constexpr static bool m_use_linearkernel {false};
66     const int p_cell_size = 4;            //4 for hog (= bin_size)
67
68     /*
69     padding             ... extra area surrounding the target           (1.5)
70     kernel_sigma        ... gaussian kernel bandwidth                   (0.5)
71     lambda              ... regularization                              (1e-4)
72     interp_factor       ... linear interpolation factor for adaptation  (0.02)
73     output_sigma_factor ... spatial bandwidth (proportional to target)  (0.1)
74     cell_size           ... hog cell size                               (4)
75     */
76     KCF_Tracker(double padding, double kernel_sigma, double lambda, double interp_factor, double output_sigma_factor, int cell_size);
77     KCF_Tracker();
78     ~KCF_Tracker();
79
80     // Init/re-init methods
81     void init(cv::Mat & img, const cv::Rect & bbox, int fit_size_x = -1, int fit_size_y = -1);
82     void setTrackerPose(BBox_c & bbox, cv::Mat & img, int fit_size_x = -1, int fit_size_y = -1);
83     void updateTrackerPosition(BBox_c & bbox);
84
85     // frame-to-frame object tracking
86     void track(cv::Mat & img);
87     BBox_c getBBox();
88     double getFilterResponse() const; // Measure of tracking accuracy
89
90 private:
91     FFT &fft;
92
93     // Initial pose of tracked object in internal image coordinates
94     // (scaled by p_downscale_factor if p_resize_image)
95     BBox_c p_init_pose;
96
97     // Information to calculate current pose of the tracked object
98     cv::Point2d p_current_center;
99     double p_current_scale = 1.;
100     double p_current_angle = 0.;
101
102     double max_response = -1.;
103
104     bool p_resize_image = false;
105
106     constexpr static double p_downscale_factor = 0.5;
107     constexpr static double p_floating_error = 0.0001;
108
109     const double p_padding = 1.5;
110     const double p_output_sigma_factor = 0.1;
111     double p_output_sigma;
112     const double p_kernel_sigma = 0.5;    //def = 0.5
113     const double p_lambda = 1e-4;         //regularization in learning step
114     const double p_interp_factor = 0.02;  //def = 0.02, linear interpolation factor for adaptation
115     cv::Size p_windows_size;              // size of the patch to find the tracked object in
116     cv::Size fit_size;                    // size to which rescale the patch for better FFT performance
117
118     constexpr static uint p_num_scales = m_use_scale ? 5 : 1;
119     constexpr static double p_scale_step = 1.03;
120     double p_min_max_scale[2];
121     std::vector<double> p_scales;
122
123     constexpr static uint p_num_angles = 3;
124     constexpr static int p_angle_step = 10;
125     std::vector<double> p_angles;
126
127     constexpr static int p_num_of_feats = 31 + (m_use_color ? 3 : 0) + (m_use_cnfeat ? 10 : 0);
128     cv::Size feature_size;
129
130     std::unique_ptr<Kcf_Tracker_Private> d;
131
132     class Model {
133         cv::Size feature_size;
134         uint height, width, n_feats;
135     public:
136         ComplexMat yf {height, width, 1};
137         ComplexMat model_alphaf {height, width, 1};
138         ComplexMat model_alphaf_num {height, width, 1};
139         ComplexMat model_alphaf_den {height, width, 1};
140         ComplexMat model_xf {height, width, n_feats};
141         ComplexMat xf {height, width, n_feats};
142
143         // Temporary variables for trainig
144         MatScaleFeats patch_feats{1, n_feats, feature_size};
145         MatScaleFeats temp{1, n_feats, feature_size};
146
147
148
149         Model(cv::Size feature_size, uint _n_feats)
150             : feature_size(feature_size)
151             , height(Fft::freq_size(feature_size).height)
152             , width(Fft::freq_size(feature_size).width)
153             , n_feats(_n_feats) {}
154     };
155
156     std::unique_ptr<Model> model;
157
158     class GaussianCorrelation {
159       public:
160         GaussianCorrelation(uint num_scales, uint num_feats, cv::Size size)
161             : xf_sqr_norm(num_scales)
162             , xyf(Fft::freq_size(size), num_feats, num_scales)
163             , ifft_res(num_scales, size)
164             , k(num_scales, size)
165         {}
166         void operator()(ComplexMat &result, const ComplexMat &xf, const ComplexMat &yf, double sigma, bool auto_correlation, const KCF_Tracker &kcf);
167
168       private:
169         DynMem xf_sqr_norm;
170         DynMem yf_sqr_norm{1};
171         ComplexMat xyf;
172         MatScales ifft_res;
173         MatScales k;
174     };
175
176     //helping functions
177     void scale_track(ThreadCtx &vars, cv::Mat &input_rgb, cv::Mat &input_gray);
178     cv::Mat get_subwindow(const cv::Mat &input, int cx, int cy, int size_x, int size_y, double angle) const;
179     cv::Mat gaussian_shaped_labels(double sigma, int dim1, int dim2);
180     std::unique_ptr<GaussianCorrelation> gaussian_correlation;
181     cv::Mat circshift(const cv::Mat &patch, int x_rot, int y_rot) const;
182     cv::Mat cosine_window_function(int dim1, int dim2);
183     cv::Mat get_features(cv::Mat &input_rgb, cv::Mat &input_gray, cv::Mat *dbg_patch, int cx, int cy, int size_x, int size_y, double scale, double angle) const;
184     cv::Point2f sub_pixel_peak(cv::Point &max_loc, cv::Mat &response) const;
185     double sub_grid_scale(uint index);
186     void resizeImgs(cv::Mat &input_rgb, cv::Mat &input_gray);
187     void train(cv::Mat input_rgb, cv::Mat input_gray, double interp_factor);
188     double findMaxReponse(uint &max_idx, cv::Point2d &new_location) const;
189     double sub_grid_angle(uint max_index);
190 };
191
192 #endif //KCF_HEADER_6565467831231