From b573c3bb23b565df56144329f077355ed2071829 Mon Sep 17 00:00:00 2001 From: Austin Blanton Date: Thu, 14 Nov 2013 16:33:17 -0500 Subject: [PATCH] Store each detection as its own Template, using the file metadata Detection and Confidence --- openbr/plugins/slidingwindow.cpp | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------- 1 file changed, 77 insertions(+), 49 deletions(-) diff --git a/openbr/plugins/slidingwindow.cpp b/openbr/plugins/slidingwindow.cpp index e5135c1..2fbe4c2 100644 --- a/openbr/plugins/slidingwindow.cpp +++ b/openbr/plugins/slidingwindow.cpp @@ -68,32 +68,37 @@ private: void project(const Template &src, Template &dst) const { - dst = src; + (void)src;(void)dst;qFatal("don't do that"); + } + + void project(const TemplateList &src, TemplateList &dst) const + { // no need to slide a window over ground truth data - 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; + if (src.first().file.getBool("Train", false)) { + dst = src; + return; + } + + foreach (const Template &t, src) { + float scale = t.file.get("scale", 1); + for (double y = 0; y + windowHeight < t.m().rows; y += stepSize) { + for (double x = 0; x + windowWidth < t.m().cols; x += stepSize) { + Mat windowMat(t.m(), Rect(x, y, windowWidth, windowHeight)); + 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((float) x * scale, (float) y * scale, (float) windowWidth * scale, (float) windowHeight * scale)); + detect.file.set("Confidence", conf); + dst.append(detect); + if (takeFirst) + return; + } } } } - dst.file.setList("Confidences", confidences); } void store(QDataStream &stream) const @@ -174,7 +179,7 @@ static TemplateList cropTrainingSamples(const TemplateList &data, const float as /*! * \ingroup transforms - * \brief . + * \brief Pass along images at different scales. * \author Austin Blanton \cite imaus10 */ class BuildScalesTransform : public MetaTransform @@ -219,25 +224,38 @@ private: void project(const Template &src, Template &dst) const { - dst = src; + (void)src;(void)dst;qFatal("please don't"); + } + + void project(const TemplateList &src, TemplateList &dst) const + { // do not scale images during training - 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; + if (src.first().file.getBool("Train", false)) { + 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; + } } } @@ -328,12 +346,19 @@ private: void project(const Template &src, Template &dst) const { - dst = src; - if (!dst.file.contains("Confidences")) - return; + (void)src;(void)dst;qFatal("nope"); + } - //Compute overlap between rectangles and create discrete Laplacian matrix - QList rects = OpenCVUtils::toRects(src.file.rects()); + void project(const TemplateList &src, TemplateList &dst) const + { + QList rects; + QList confidences; + foreach (const Template &t, src) { + rects.append(OpenCVUtils::toRect(t.file.get("Detection"))); + confidences.append(t.file.get("Confidence")); + } + + // Compute overlap between rectangles and create discrete Laplacian matrix int n = rects.size(); if (n == 0) return; @@ -397,7 +422,6 @@ private: cnts[i] = 0; } - QList confidences = dst.file.getList("Confidences"); for (int i = 0; i < n; i++) { mx = 0.0; mxIdx = -1; @@ -432,8 +456,12 @@ private: } } - dst.file.setRects(consolidatedRects); - dst.file.setList("Confidences", consolidatedConfidences); + for (int i=0; i