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