From 52484e12bdaa25225046367255fd7adeb9ac0a95 Mon Sep 17 00:00:00 2001 From: Brendan Klare Date: Tue, 10 Dec 2013 16:45:10 -0500 Subject: [PATCH] Reversed multiple template detection approach --- openbr/plugins/slidingwindow.cpp | 136 +++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------------------------------------------------------------- 1 file changed, 49 insertions(+), 87 deletions(-) diff --git a/openbr/plugins/slidingwindow.cpp b/openbr/plugins/slidingwindow.cpp index b53272c..a86b97d 100644 --- a/openbr/plugins/slidingwindow.cpp +++ b/openbr/plugins/slidingwindow.cpp @@ -93,44 +93,32 @@ private: void project(const Template &src, Template &dst) const { - (void)src;(void)dst;qFatal("don't do that"); - } - - void project(const TemplateList &src, TemplateList &dst) const - { - float scale = src.first().file.get("scale", 1); - projectHelp(src, dst, windowWidth, windowHeight, scale); - } - -protected: - void projectHelp(const TemplateList &src, TemplateList &dst, int windowWidth, int windowHeight, float scale = 1) const - { + dst = src; // no need to slide a window over ground truth data - if (skipProject) { - dst = src; - return; - } - - foreach (const Template &t, src) { - for (float y = 0; y + windowHeight < t.m().rows; y += windowHeight*stepFraction) { - for (float x = 0; x + windowWidth < t.m().cols; x += windowWidth*stepFraction) { - Mat windowMat(t.m(), Rect(x + ignoreBorder, y + ignoreBorder, windowWidth - ignoreBorder * 2, windowHeight - ignoreBorder * 2)); - Template detect; - transform->project(Template(t.file, windowMat), detect); - - // the result will be the only value in the Mat - float conf = detect.m().at(0); - if (conf > threshold) { - detect.file.set("Detection", QRectF(x*scale, y*scale, windowWidth*scale, windowHeight*scale)); - detect.file.set("Confidence", conf); - detect.file.clearRects(); - dst.append(detect); - if (takeFirst) - return; - } + if (src.file.getBool("Train", false)) return; + + dst.file.clearRects(); + float scale = src.file.get("scale", 1); + Template windowTemplate(src.file, src); + QList confidences = dst.file.getList("Confidences", QList()); + for (double y = 0; y + windowHeight < src.m().rows; y += stepSize) { + for (double x = 0; x + windowWidth < src.m().cols; x += stepSize) { + Mat windowMat(src, Rect(x, y, windowWidth, windowHeight)); + windowTemplate.replace(0,windowMat); + Template detect; + transform->project(windowTemplate, detect); + float conf = detect.m().at(0); + + // the result will be in the Label + if (conf > threshold) { + dst.file.appendRect(QRectF((float) x * scale, (float) y * scale, (float) windowWidth * scale, (float) windowHeight * scale)); + confidences.append(conf); + if (takeFirst) + return; } } } + dst.file.setList("Confidences", confidences); } }; @@ -219,7 +207,7 @@ static TemplateList cropTrainingSamples(const TemplateList &data, const float as /*! * \ingroup transforms - * \brief Pass along images at different scales. + * \brief . * \author Austin Blanton \cite imaus10 */ class BuildScalesTransform : public MetaTransform @@ -266,38 +254,25 @@ private: void project(const Template &src, Template &dst) const { - (void)src;(void)dst;qFatal("please don't"); - } - - void project(const TemplateList &src, TemplateList &dst) const - { + dst = src; // do not scale images during training - if (skipProject) { - dst = src; - return; - } - - foreach(const Template &t, src) { - int rows = t.m().rows; - int cols = t.m().cols; - int windowHeight = (int) qRound((float) windowWidth / aspectRatio); - float startScale; - if ((cols / rows) > aspectRatio) - startScale = qRound((float) rows / (float) windowHeight); - else - startScale = qRound((float) cols / (float) windowWidth); - for (float scale = startScale; scale >= minScale; scale -= (1.0 - scaleFactor)) { - Template scaleImg(t.file, Mat()); - scaleImg.file.set("scale", scale); - resize(t.m(), scaleImg.m(), Size(qRound(cols / scale), qRound(rows / scale))); - TemplateList results; - TemplateList input; - input.append(scaleImg); - transform->project(input, results); - dst.append(results); - if (takeLargestScale && !dst.empty()) - return; - } + if (src.file.getBool("Train", false)) return; + + int rows = src.m().rows; + int cols = src.m().cols; + int windowHeight = (int) qRound((float) windowWidth / aspectRatio); + float startScale; + if ((cols / rows) > aspectRatio) + startScale = qRound((float) rows / (float) windowHeight); + else + startScale = qRound((float) cols / (float) windowWidth); + for (float scale = startScale; scale >= minScale; scale -= (1.0 - scaleFactor)) { + Template scaleImg(src.file, Mat()); + scaleImg.file.set("scale", scale); + resize(src, scaleImg, Size(qRound(cols / scale), qRound(rows / scale))); + transform->project(scaleImg, dst); + if (takeLargestScale && !dst.file.rects().empty()) + return; } } @@ -388,21 +363,12 @@ private: void project(const Template &src, Template &dst) const { - (void)src;(void)dst;qFatal("nope"); - } - - void project(const TemplateList &src, TemplateList &dst) const - { - QList rects; - QList confidences; - foreach (const Template &t, src) { - if (t.file.contains("Detection")) { - rects.append(OpenCVUtils::toRect(t.file.get("Detection"))); - confidences.append(t.file.get("Confidence")); - } - } + dst = src; + if (!dst.file.contains("Confidences")) + return; - // Compute overlap between rectangles and create discrete Laplacian matrix + //Compute overlap between rectangles and create discrete Laplacian matrix + QList rects = OpenCVUtils::toRects(src.file.rects()); int n = rects.size(); if (n == 0) return; @@ -466,6 +432,7 @@ private: cnts[i] = 0; } + QList confidences = dst.file.getList("Confidences"); for (int i = 0; i < n; i++) { mx = 0.0; mxIdx = -1; @@ -500,13 +467,6 @@ private: } } - for (int i=0; i("Confidences", consolidatedConfidences); } }; -- libgit2 0.21.4