Skip to content

Commit 941865c

Browse files
committed
tracking: reduce useless computations in TLD
1 parent 62939e2 commit 941865c

File tree

2 files changed

+51
-17
lines changed

2 files changed

+51
-17
lines changed

modules/tracking/src/tldDetector.cpp

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,27 +65,58 @@ namespace cv
6565
return p;
6666
}
6767

68+
double TLDDetector::computeSminus(const Mat_<uchar>& patch) const
69+
{
70+
double sminus = 0.0;
71+
Mat_<uchar> modelSample(STANDARD_PATCH_SIZE, STANDARD_PATCH_SIZE);
72+
for (int i = 0; i < *negNum; i++)
73+
{
74+
modelSample.data = &(negExp->data[i * 225]);
75+
sminus = std::max(sminus, 0.5 * (tracking_internal::computeNCC(modelSample, patch) + 1.0));
76+
}
77+
return sminus;
78+
}
79+
6880
// Calculate Relative similarity of the patch (NN-Model)
6981
double TLDDetector::Sr(const Mat_<uchar>& patch) const
7082
{
7183
double splus = 0.0, sminus = 0.0;
72-
Mat_<uchar> modelSample(STANDARD_PATCH_SIZE, STANDARD_PATCH_SIZE);
84+
Mat_<uchar> modelSample(STANDARD_PATCH_SIZE, STANDARD_PATCH_SIZE);
7385
for (int i = 0; i < *posNum; i++)
7486
{
7587
modelSample.data = &(posExp->data[i * 225]);
7688
splus = std::max(splus, 0.5 * (tracking_internal::computeNCC(modelSample, patch) + 1.0));
7789
}
78-
for (int i = 0; i < *negNum; i++)
79-
{
80-
modelSample.data = &(negExp->data[i * 225]);
81-
sminus = std::max(sminus, 0.5 * (tracking_internal::computeNCC(modelSample, patch) + 1.0));
82-
}
90+
sminus = computeSminus(patch);
8391

8492
if (splus + sminus == 0.0)
8593
return 0.0;
8694
return splus / (sminus + splus);
8795
}
8896

97+
std::pair<double, double> TLDDetector::SrAndSc(const Mat_<uchar>& patch) const
98+
{
99+
double splusC = 0.0, sminus = 0.0, splus = 0.0;
100+
Mat_<uchar> modelSample(STANDARD_PATCH_SIZE, STANDARD_PATCH_SIZE);
101+
int med = tracking_internal::getMedian((*timeStampsPositive));
102+
for (int i = 0; i < *posNum; i++)
103+
{
104+
modelSample.data = &(posExp->data[i * 225]);
105+
double s = 0.5 * (tracking_internal::computeNCC(modelSample, patch) + 1.0);
106+
107+
if ((int)(*timeStampsPositive)[i] <= med)
108+
splusC = std::max(splusC, s);
109+
110+
splus = std::max(splus, s);
111+
}
112+
sminus = computeSminus(patch);
113+
114+
double sr = (splus + sminus == 0.0) ? 0. : splus / (sminus + splus);
115+
double sc = (splusC + sminus == 0.0) ? 0. : splusC / (sminus + splusC);
116+
117+
return std::pair<double, double>(sr, sc);
118+
}
119+
89120
#ifdef HAVE_OPENCL
90121
double TLDDetector::ocl_Sr(const Mat_<uchar>& patch)
91122
{
@@ -205,11 +236,7 @@ namespace cv
205236
splus = std::max(splus, 0.5 * (tracking_internal::computeNCC(modelSample, patch) + 1.0));
206237
}
207238
}
208-
for (int i = 0; i < *negNum; i++)
209-
{
210-
modelSample.data = &(negExp->data[i * 225]);
211-
sminus = std::max(sminus, 0.5 * (tracking_internal::computeNCC(modelSample, patch) + 1.0));
212-
}
239+
sminus = computeSminus(patch);
213240

214241
if (splus + sminus == 0.0)
215242
return 0.0;
@@ -317,9 +344,9 @@ namespace cv
317344
resample(detectorF->resized_imgs[detectorF->ensScaleIDs[ind]],
318345
Rect2d(detectorF->ensBuffer[ind], initSizeF),
319346
detectorF->standardPatches[ind]);
320-
321-
detectorF->scValues[ind] = detectorF->Sc (detectorF->standardPatches[ind]);
322-
detectorF->srValues[ind] = detectorF->Sr (detectorF->standardPatches[ind]);
347+
std::pair<double, double> values = detectorF->SrAndSc(detectorF->standardPatches[ind]);
348+
detectorF->scValues[ind] = values.second;
349+
detectorF->srValues[ind] = values.first;
323350
}
324351
}
325352

@@ -413,15 +440,17 @@ namespace cv
413440
LabeledPatch labPatch;
414441
double curScale = pow(SCALE_STEP, ensScaleIDs[i]);
415442
labPatch.rect = Rect2d(ensBuffer[i].x*curScale, ensBuffer[i].y*curScale, initSize.width * curScale, initSize.height * curScale);
443+
labPatch.Sc = scValues[i];
444+
//printf("max sc %f\n", labPatch.Sc);
416445

417446
const double srValue = srValues[i];
418447
const double scValue = scValues[i];
419448

420449
////To fix: Check the paper, probably this cause wrong learning
421450
//
422-
labPatch.isObject = srValue > THETA_NN;
451+
labPatch.isObject = srValue > THETA_NN;
423452
labPatch.shouldBeIntegrated = abs(srValue - THETA_NN) < CLASSIFIER_MARGIN;
424-
patches.push_back(labPatch);
453+
patches.push_back(labPatch);
425454
//
426455

427456
if (!labPatch.isObject)
@@ -441,7 +470,7 @@ namespace cv
441470
}
442471
}
443472

444-
if (maxSc < 0)
473+
if (maxSc < 0)
445474
return false;
446475
else
447476
{

modules/tracking/src/tldDetector.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ namespace cv
7878
void prepareClassifiers(int rowstep);
7979
double Sr(const Mat_<uchar>& patch) const;
8080
double Sc(const Mat_<uchar>& patch) const;
81+
std::pair<double, double> SrAndSc(const Mat_<uchar>& patch) const;
8182
#ifdef HAVE_OPENCL
8283
double ocl_Sr(const Mat_<uchar>& patch);
8384
double ocl_Sc(const Mat_<uchar>& patch);
@@ -102,13 +103,17 @@ namespace cv
102103
{
103104
Rect2d rect;
104105
bool isObject, shouldBeIntegrated;
106+
double Sc;
105107
};
106108
bool detect(const Mat& img, const Mat& imgBlurred, Rect2d& res, std::vector<LabeledPatch>& patches, Size initSize);
107109
bool ocl_detect(const Mat& img, const Mat& imgBlurred, Rect2d& res, std::vector<LabeledPatch>& patches, Size initSize);
108110

109111
friend class MyMouseCallbackDEBUG;
110112
static void computeIntegralImages(const Mat& img, Mat_<double>& intImgP, Mat_<double>& intImgP2){ integral(img, intImgP, intImgP2, CV_64F); }
111113
static inline bool patchVariance(Mat_<double>& intImgP, Mat_<double>& intImgP2, double *originalVariance, Point pt, Size size);
114+
115+
protected:
116+
double computeSminus(const Mat_<uchar>& patch) const;
112117
};
113118

114119

0 commit comments

Comments
 (0)