1// This file is part of OpenCV project.
2// It is subject to the license terms in the LICENSE file found in the top-level directory
3// of this distribution and at http://opencv.org/license.html.
4
5#include "../../precomp.hpp"
6#include "opencv2/video/detail/tracking.detail.hpp"
7
8namespace cv {
9namespace detail {
10inline namespace tracking {
11
12TrackerSamplerAlgorithm::~TrackerSamplerAlgorithm()
13{
14 // nothing
15}
16
17TrackerSamplerCSC::Params::Params()
18{
19 initInRad = 3;
20 initMaxNegNum = 65;
21 searchWinSize = 25;
22 trackInPosRad = 4;
23 trackMaxNegNum = 65;
24 trackMaxPosNum = 100000;
25}
26
27TrackerSamplerCSC::TrackerSamplerCSC(const TrackerSamplerCSC::Params& parameters)
28 : params(parameters)
29{
30 mode = MODE_INIT_POS;
31 rng = theRNG();
32}
33
34TrackerSamplerCSC::~TrackerSamplerCSC()
35{
36 // nothing
37}
38
39bool TrackerSamplerCSC::sampling(const Mat& image, const Rect& boundingBox, std::vector<Mat>& sample)
40{
41 CV_Assert(!image.empty());
42
43 float inrad = 0;
44 float outrad = 0;
45 int maxnum = 0;
46
47 switch (mode)
48 {
49 case MODE_INIT_POS:
50 inrad = params.initInRad;
51 sample = sampleImage(img: image, x: boundingBox.x, y: boundingBox.y, w: boundingBox.width, h: boundingBox.height, inrad);
52 break;
53 case MODE_INIT_NEG:
54 inrad = 2.0f * params.searchWinSize;
55 outrad = 1.5f * params.initInRad;
56 maxnum = params.initMaxNegNum;
57 sample = sampleImage(img: image, x: boundingBox.x, y: boundingBox.y, w: boundingBox.width, h: boundingBox.height, inrad, outrad, maxnum);
58 break;
59 case MODE_TRACK_POS:
60 inrad = params.trackInPosRad;
61 outrad = 0;
62 maxnum = params.trackMaxPosNum;
63 sample = sampleImage(img: image, x: boundingBox.x, y: boundingBox.y, w: boundingBox.width, h: boundingBox.height, inrad, outrad, maxnum);
64 break;
65 case MODE_TRACK_NEG:
66 inrad = 1.5f * params.searchWinSize;
67 outrad = params.trackInPosRad + 5;
68 maxnum = params.trackMaxNegNum;
69 sample = sampleImage(img: image, x: boundingBox.x, y: boundingBox.y, w: boundingBox.width, h: boundingBox.height, inrad, outrad, maxnum);
70 break;
71 case MODE_DETECT:
72 inrad = params.searchWinSize;
73 sample = sampleImage(img: image, x: boundingBox.x, y: boundingBox.y, w: boundingBox.width, h: boundingBox.height, inrad);
74 break;
75 default:
76 inrad = params.initInRad;
77 sample = sampleImage(img: image, x: boundingBox.x, y: boundingBox.y, w: boundingBox.width, h: boundingBox.height, inrad);
78 break;
79 }
80 return false;
81}
82
83void TrackerSamplerCSC::setMode(int samplingMode)
84{
85 mode = samplingMode;
86}
87
88std::vector<Mat> TrackerSamplerCSC::sampleImage(const Mat& img, int x, int y, int w, int h, float inrad, float outrad, int maxnum)
89{
90 int rowsz = img.rows - h - 1;
91 int colsz = img.cols - w - 1;
92 float inradsq = inrad * inrad;
93 float outradsq = outrad * outrad;
94 int dist;
95
96 uint minrow = max(a: 0, b: (int)y - (int)inrad);
97 uint maxrow = min(a: (int)rowsz - 1, b: (int)y + (int)inrad);
98 uint mincol = max(a: 0, b: (int)x - (int)inrad);
99 uint maxcol = min(a: (int)colsz - 1, b: (int)x + (int)inrad);
100
101 //fprintf(stderr,"inrad=%f minrow=%d maxrow=%d mincol=%d maxcol=%d\n",inrad,minrow,maxrow,mincol,maxcol);
102
103 std::vector<Mat> samples;
104 samples.resize(new_size: (maxrow - minrow + 1) * (maxcol - mincol + 1));
105 int i = 0;
106
107 float prob = ((float)(maxnum)) / samples.size();
108
109 for (int r = minrow; r <= int(maxrow); r++)
110 for (int c = mincol; c <= int(maxcol); c++)
111 {
112 dist = (y - r) * (y - r) + (x - c) * (x - c);
113 if (float(rng.uniform(a: 0.f, b: 1.f)) < prob && dist < inradsq && dist >= outradsq)
114 {
115 samples[i] = img(Rect(c, r, w, h));
116 i++;
117 }
118 }
119
120 samples.resize(new_size: min(a: i, b: maxnum));
121 return samples;
122}
123
124}}} // namespace cv::detail::tracking
125

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of opencv/modules/video/src/tracking/detail/tracker_sampler_algorithm.cpp