]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blob - src/threadctx.hpp
Extend to support not only multiple scales but also multiple angles
[hercules2020/kcf.git] / src / threadctx.hpp
1 #ifndef SCALE_VARS_HPP
2 #define SCALE_VARS_HPP
3
4 #include <future>
5 #include "dynmem.hpp"
6 #include "kcf.h"
7 #include "complexmat.hpp"
8 #include <vector>
9
10 class KCF_Tracker;
11
12 template <typename T>
13 class ScaleRotVector : public std::vector<T> {
14 public:
15     ScaleRotVector(const std::vector<double> &scales, const std::vector<double> &angles)
16         : scales(scales)
17         , angles(angles)
18     {}
19
20     uint getIdx(uint scale_idx, uint angle_idx) const { return angles.size() * scale_idx + angle_idx; }
21     uint getScaleIdx(uint idx) const { return idx / angles.size(); }
22     uint getAngleIdx(uint idx) const { return idx % angles.size(); }
23     T& operator()(uint scale_idx, uint angle_idx) { return std::vector<T>::at(getIdx(scale_idx, angle_idx)); }
24     double scale(uint idx) const { return scales[getScaleIdx(idx)]; }
25     double angle(uint idx) const { return angles[getAngleIdx(idx)]; }
26 private:
27     const std::vector<double> scales, angles;
28 };
29
30 struct ThreadCtx {
31   public:
32     ThreadCtx(cv::Size roi, uint num_features
33 #ifdef BIG_BATCH
34               , const std::vector<double> &scales
35               , const std::vector<double> &angles
36 #else
37               , double scale
38               , double angle
39 #endif
40              )
41         : roi(roi)
42         , num_features(num_features)
43         , num_scales(IF_BIG_BATCH(scales.size(), 1))
44         , num_angles(IF_BIG_BATCH(angles.size(), 1))
45 #ifdef BIG_BATCH
46         , max(scales, angles)
47         , dbg_patch(scales, angles)
48         {
49             max.resize(scales.size() * angles.size());
50             dbg_patch.resize(scales.size() * angles.size());
51         }
52 #else
53         , scale(scale)
54         , angle(angle)
55         {}
56 #endif
57
58
59     ThreadCtx(ThreadCtx &&) = default;
60
61     void track(const KCF_Tracker &kcf, cv::Mat &input_rgb, cv::Mat &input_gray);
62 private:
63     cv::Size roi;
64     uint num_features;
65     uint num_scales;
66     uint num_angles;
67     cv::Size freq_size = Fft::freq_size(roi);
68
69     MatScaleFeats patch_feats{num_scales * num_angles, num_features, roi};
70     MatScaleFeats temp{num_scales * num_angles, num_features, roi};
71
72     KCF_Tracker::GaussianCorrelation gaussian_correlation{num_scales * num_angles, num_features, roi};
73
74     MatScales ifft2_res{num_scales * num_angles, roi};
75
76     ComplexMat zf{uint(freq_size.height), uint(freq_size.width), num_features, num_scales * num_angles};
77     ComplexMat kzf{uint(freq_size.height), uint(freq_size.width), 1, num_scales * num_angles};
78
79 public:
80 #ifdef ASYNC
81     std::future<void> async_res;
82 #endif
83
84     MatScales response{num_scales * num_angles, roi};
85
86     struct Max {
87         cv::Point2i loc;
88         double response;
89     };
90
91 #ifdef BIG_BATCH
92     ScaleRotVector<Max> max;
93     ScaleRotVector<cv::Mat> dbg_patch; // images for visual debugging
94 #else
95     Max max;
96     const double scale, angle;
97     cv::Mat dbg_patch; // image for visual debugging
98 #endif
99 };
100
101 #endif // SCALE_VARS_HPP