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

tracking: reduce useless computations in TLD

parent 62939e29
...@@ -65,6 +65,18 @@ namespace cv ...@@ -65,6 +65,18 @@ namespace cv
return p; 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) // Calculate Relative similarity of the patch (NN-Model)
double TLDDetector::Sr(const Mat_<uchar>& patch) const double TLDDetector::Sr(const Mat_<uchar>& patch) const
{ {
...@@ -75,17 +87,36 @@ namespace cv ...@@ -75,17 +87,36 @@ namespace cv
modelSample.data = &(posExp->data[i * 225]); modelSample.data = &(posExp->data[i * 225]);
splus = std::max(splus, 0.5 * (tracking_internal::computeNCC(modelSample, patch) + 1.0)); splus = std::max(splus, 0.5 * (tracking_internal::computeNCC(modelSample, patch) + 1.0));
} }
for (int i = 0; i < *negNum; i++) sminus = computeSminus(patch);
{
modelSample.data = &(negExp->data[i * 225]);
sminus = std::max(sminus, 0.5 * (tracking_internal::computeNCC(modelSample, patch) + 1.0));
}
if (splus + sminus == 0.0) if (splus + sminus == 0.0)
return 0.0; return 0.0;
return splus / (sminus + splus); 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 #ifdef HAVE_OPENCL
double TLDDetector::ocl_Sr(const Mat_<uchar>& patch) double TLDDetector::ocl_Sr(const Mat_<uchar>& patch)
{ {
...@@ -205,11 +236,7 @@ namespace cv ...@@ -205,11 +236,7 @@ namespace cv
splus = std::max(splus, 0.5 * (tracking_internal::computeNCC(modelSample, patch) + 1.0)); splus = std::max(splus, 0.5 * (tracking_internal::computeNCC(modelSample, patch) + 1.0));
} }
} }
for (int i = 0; i < *negNum; i++) sminus = computeSminus(patch);
{
modelSample.data = &(negExp->data[i * 225]);
sminus = std::max(sminus, 0.5 * (tracking_internal::computeNCC(modelSample, patch) + 1.0));
}
if (splus + sminus == 0.0) if (splus + sminus == 0.0)
return 0.0; return 0.0;
...@@ -317,9 +344,9 @@ namespace cv ...@@ -317,9 +344,9 @@ namespace cv
resample(detectorF->resized_imgs[detectorF->ensScaleIDs[ind]], resample(detectorF->resized_imgs[detectorF->ensScaleIDs[ind]],
Rect2d(detectorF->ensBuffer[ind], initSizeF), Rect2d(detectorF->ensBuffer[ind], initSizeF),
detectorF->standardPatches[ind]); detectorF->standardPatches[ind]);
std::pair<double, double> values = detectorF->SrAndSc(detectorF->standardPatches[ind]);
detectorF->scValues[ind] = detectorF->Sc (detectorF->standardPatches[ind]); detectorF->scValues[ind] = values.second;
detectorF->srValues[ind] = detectorF->Sr (detectorF->standardPatches[ind]); detectorF->srValues[ind] = values.first;
} }
} }
...@@ -413,6 +440,8 @@ namespace cv ...@@ -413,6 +440,8 @@ namespace cv
LabeledPatch labPatch; LabeledPatch labPatch;
double curScale = pow(SCALE_STEP, ensScaleIDs[i]); 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.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 srValue = srValues[i];
const double scValue = scValues[i]; const double scValue = scValues[i];
......
...@@ -78,6 +78,7 @@ namespace cv ...@@ -78,6 +78,7 @@ namespace cv
void prepareClassifiers(int rowstep); void prepareClassifiers(int rowstep);
double Sr(const Mat_<uchar>& patch) const; double Sr(const Mat_<uchar>& patch) const;
double Sc(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 #ifdef HAVE_OPENCL
double ocl_Sr(const Mat_<uchar>& patch); double ocl_Sr(const Mat_<uchar>& patch);
double ocl_Sc(const Mat_<uchar>& patch); double ocl_Sc(const Mat_<uchar>& patch);
...@@ -102,6 +103,7 @@ namespace cv ...@@ -102,6 +103,7 @@ namespace cv
{ {
Rect2d rect; Rect2d rect;
bool isObject, shouldBeIntegrated; bool isObject, shouldBeIntegrated;
double Sc;
}; };
bool detect(const Mat& img, const Mat& imgBlurred, Rect2d& res, std::vector<LabeledPatch>& patches, Size initSize); 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); bool ocl_detect(const Mat& img, const Mat& imgBlurred, Rect2d& res, std::vector<LabeledPatch>& patches, Size initSize);
...@@ -109,6 +111,9 @@ namespace cv ...@@ -109,6 +111,9 @@ namespace cv
friend class MyMouseCallbackDEBUG; friend class MyMouseCallbackDEBUG;
static void computeIntegralImages(const Mat& img, Mat_<double>& intImgP, Mat_<double>& intImgP2){ integral(img, intImgP, intImgP2, CV_64F); } 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); 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