Commit 941865c9 authored by Vladislav Sovrasov's avatar Vladislav Sovrasov

tracking: reduce useless computations in TLD

parent 62939e29
......@@ -65,27 +65,58 @@ namespace cv
return p;
}
double TLDDetector::computeSminus(const Mat_<uchar>& patch) const
{
double sminus = 0.0;
Mat_<uchar> modelSample(STANDARD_PATCH_SIZE, STANDARD_PATCH_SIZE);
for (int i = 0; i < *negNum; i++)
{
modelSample.data = &(negExp->data[i * 225]);
sminus = std::max(sminus, 0.5 * (tracking_internal::computeNCC(modelSample, patch) + 1.0));
}
return sminus;
}
// Calculate Relative similarity of the patch (NN-Model)
double TLDDetector::Sr(const Mat_<uchar>& patch) const
{
double splus = 0.0, sminus = 0.0;
Mat_<uchar> modelSample(STANDARD_PATCH_SIZE, STANDARD_PATCH_SIZE);
Mat_<uchar> modelSample(STANDARD_PATCH_SIZE, STANDARD_PATCH_SIZE);
for (int i = 0; i < *posNum; i++)
{
modelSample.data = &(posExp->data[i * 225]);
splus = std::max(splus, 0.5 * (tracking_internal::computeNCC(modelSample, patch) + 1.0));
}
for (int i = 0; i < *negNum; i++)
{
modelSample.data = &(negExp->data[i * 225]);
sminus = std::max(sminus, 0.5 * (tracking_internal::computeNCC(modelSample, patch) + 1.0));
}
sminus = computeSminus(patch);
if (splus + sminus == 0.0)
return 0.0;
return splus / (sminus + splus);
}
std::pair<double, double> TLDDetector::SrAndSc(const Mat_<uchar>& patch) const
{
double splusC = 0.0, sminus = 0.0, splus = 0.0;
Mat_<uchar> modelSample(STANDARD_PATCH_SIZE, STANDARD_PATCH_SIZE);
int med = tracking_internal::getMedian((*timeStampsPositive));
for (int i = 0; i < *posNum; i++)
{
modelSample.data = &(posExp->data[i * 225]);
double s = 0.5 * (tracking_internal::computeNCC(modelSample, patch) + 1.0);
if ((int)(*timeStampsPositive)[i] <= med)
splusC = std::max(splusC, s);
splus = std::max(splus, s);
}
sminus = computeSminus(patch);
double sr = (splus + sminus == 0.0) ? 0. : splus / (sminus + splus);
double sc = (splusC + sminus == 0.0) ? 0. : splusC / (sminus + splusC);
return std::pair<double, double>(sr, sc);
}
#ifdef HAVE_OPENCL
double TLDDetector::ocl_Sr(const Mat_<uchar>& patch)
{
......@@ -205,11 +236,7 @@ namespace cv
splus = std::max(splus, 0.5 * (tracking_internal::computeNCC(modelSample, patch) + 1.0));
}
}
for (int i = 0; i < *negNum; i++)
{
modelSample.data = &(negExp->data[i * 225]);
sminus = std::max(sminus, 0.5 * (tracking_internal::computeNCC(modelSample, patch) + 1.0));
}
sminus = computeSminus(patch);
if (splus + sminus == 0.0)
return 0.0;
......@@ -317,9 +344,9 @@ namespace cv
resample(detectorF->resized_imgs[detectorF->ensScaleIDs[ind]],
Rect2d(detectorF->ensBuffer[ind], initSizeF),
detectorF->standardPatches[ind]);
detectorF->scValues[ind] = detectorF->Sc (detectorF->standardPatches[ind]);
detectorF->srValues[ind] = detectorF->Sr (detectorF->standardPatches[ind]);
std::pair<double, double> values = detectorF->SrAndSc(detectorF->standardPatches[ind]);
detectorF->scValues[ind] = values.second;
detectorF->srValues[ind] = values.first;
}
}
......@@ -413,15 +440,17 @@ namespace cv
LabeledPatch labPatch;
double curScale = pow(SCALE_STEP, ensScaleIDs[i]);
labPatch.rect = Rect2d(ensBuffer[i].x*curScale, ensBuffer[i].y*curScale, initSize.width * curScale, initSize.height * curScale);
labPatch.Sc = scValues[i];
//printf("max sc %f\n", labPatch.Sc);
const double srValue = srValues[i];
const double scValue = scValues[i];
////To fix: Check the paper, probably this cause wrong learning
//
labPatch.isObject = srValue > THETA_NN;
labPatch.isObject = srValue > THETA_NN;
labPatch.shouldBeIntegrated = abs(srValue - THETA_NN) < CLASSIFIER_MARGIN;
patches.push_back(labPatch);
patches.push_back(labPatch);
//
if (!labPatch.isObject)
......@@ -441,7 +470,7 @@ namespace cv
}
}
if (maxSc < 0)
if (maxSc < 0)
return false;
else
{
......
......@@ -78,6 +78,7 @@ namespace cv
void prepareClassifiers(int rowstep);
double Sr(const Mat_<uchar>& patch) const;
double Sc(const Mat_<uchar>& patch) const;
std::pair<double, double> SrAndSc(const Mat_<uchar>& patch) const;
#ifdef HAVE_OPENCL
double ocl_Sr(const Mat_<uchar>& patch);
double ocl_Sc(const Mat_<uchar>& patch);
......@@ -102,6 +103,7 @@ namespace cv
{
Rect2d rect;
bool isObject, shouldBeIntegrated;
double Sc;
};
bool detect(const Mat& img, const Mat& imgBlurred, Rect2d& res, std::vector<LabeledPatch>& patches, Size initSize);
bool ocl_detect(const Mat& img, const Mat& imgBlurred, Rect2d& res, std::vector<LabeledPatch>& patches, Size initSize);
......@@ -109,6 +111,9 @@ namespace cv
friend class MyMouseCallbackDEBUG;
static void computeIntegralImages(const Mat& img, Mat_<double>& intImgP, Mat_<double>& intImgP2){ integral(img, intImgP, intImgP2, CV_64F); }
static inline bool patchVariance(Mat_<double>& intImgP, Mat_<double>& intImgP2, double *originalVariance, Point pt, Size size);
protected:
double computeSminus(const Mat_<uchar>& patch) const;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment