Commit 52484e12bdaa25225046367255fd7adeb9ac0a95

Authored by Brendan Klare
1 parent 2f14b079

Reversed multiple template detection approach

Showing 1 changed file with 49 additions and 87 deletions
openbr/plugins/slidingwindow.cpp
... ... @@ -93,44 +93,32 @@ private:
93 93  
94 94 void project(const Template &src, Template &dst) const
95 95 {
96   - (void)src;(void)dst;qFatal("don't do that");
97   - }
98   -
99   - void project(const TemplateList &src, TemplateList &dst) const
100   - {
101   - float scale = src.first().file.get<float>("scale", 1);
102   - projectHelp(src, dst, windowWidth, windowHeight, scale);
103   - }
104   -
105   -protected:
106   - void projectHelp(const TemplateList &src, TemplateList &dst, int windowWidth, int windowHeight, float scale = 1) const
107   - {
  96 + dst = src;
108 97 // no need to slide a window over ground truth data
109   - if (skipProject) {
110   - dst = src;
111   - return;
112   - }
113   -
114   - foreach (const Template &t, src) {
115   - for (float y = 0; y + windowHeight < t.m().rows; y += windowHeight*stepFraction) {
116   - for (float x = 0; x + windowWidth < t.m().cols; x += windowWidth*stepFraction) {
117   - Mat windowMat(t.m(), Rect(x + ignoreBorder, y + ignoreBorder, windowWidth - ignoreBorder * 2, windowHeight - ignoreBorder * 2));
118   - Template detect;
119   - transform->project(Template(t.file, windowMat), detect);
120   -
121   - // the result will be the only value in the Mat
122   - float conf = detect.m().at<float>(0);
123   - if (conf > threshold) {
124   - detect.file.set("Detection", QRectF(x*scale, y*scale, windowWidth*scale, windowHeight*scale));
125   - detect.file.set("Confidence", conf);
126   - detect.file.clearRects();
127   - dst.append(detect);
128   - if (takeFirst)
129   - return;
130   - }
  98 + if (src.file.getBool("Train", false)) return;
  99 +
  100 + dst.file.clearRects();
  101 + float scale = src.file.get<float>("scale", 1);
  102 + Template windowTemplate(src.file, src);
  103 + QList<float> confidences = dst.file.getList<float>("Confidences", QList<float>());
  104 + for (double y = 0; y + windowHeight < src.m().rows; y += stepSize) {
  105 + for (double x = 0; x + windowWidth < src.m().cols; x += stepSize) {
  106 + Mat windowMat(src, Rect(x, y, windowWidth, windowHeight));
  107 + windowTemplate.replace(0,windowMat);
  108 + Template detect;
  109 + transform->project(windowTemplate, detect);
  110 + float conf = detect.m().at<float>(0);
  111 +
  112 + // the result will be in the Label
  113 + if (conf > threshold) {
  114 + dst.file.appendRect(QRectF((float) x * scale, (float) y * scale, (float) windowWidth * scale, (float) windowHeight * scale));
  115 + confidences.append(conf);
  116 + if (takeFirst)
  117 + return;
131 118 }
132 119 }
133 120 }
  121 + dst.file.setList<float>("Confidences", confidences);
134 122 }
135 123 };
136 124  
... ... @@ -219,7 +207,7 @@ static TemplateList cropTrainingSamples(const TemplateList &amp;data, const float as
219 207  
220 208 /*!
221 209 * \ingroup transforms
222   - * \brief Pass along images at different scales.
  210 + * \brief .
223 211 * \author Austin Blanton \cite imaus10
224 212 */
225 213 class BuildScalesTransform : public MetaTransform
... ... @@ -266,38 +254,25 @@ private:
266 254  
267 255 void project(const Template &src, Template &dst) const
268 256 {
269   - (void)src;(void)dst;qFatal("please don't");
270   - }
271   -
272   - void project(const TemplateList &src, TemplateList &dst) const
273   - {
  257 + dst = src;
274 258 // do not scale images during training
275   - if (skipProject) {
276   - dst = src;
277   - return;
278   - }
279   -
280   - foreach(const Template &t, src) {
281   - int rows = t.m().rows;
282   - int cols = t.m().cols;
283   - int windowHeight = (int) qRound((float) windowWidth / aspectRatio);
284   - float startScale;
285   - if ((cols / rows) > aspectRatio)
286   - startScale = qRound((float) rows / (float) windowHeight);
287   - else
288   - startScale = qRound((float) cols / (float) windowWidth);
289   - for (float scale = startScale; scale >= minScale; scale -= (1.0 - scaleFactor)) {
290   - Template scaleImg(t.file, Mat());
291   - scaleImg.file.set("scale", scale);
292   - resize(t.m(), scaleImg.m(), Size(qRound(cols / scale), qRound(rows / scale)));
293   - TemplateList results;
294   - TemplateList input;
295   - input.append(scaleImg);
296   - transform->project(input, results);
297   - dst.append(results);
298   - if (takeLargestScale && !dst.empty())
299   - return;
300   - }
  259 + if (src.file.getBool("Train", false)) return;
  260 +
  261 + int rows = src.m().rows;
  262 + int cols = src.m().cols;
  263 + int windowHeight = (int) qRound((float) windowWidth / aspectRatio);
  264 + float startScale;
  265 + if ((cols / rows) > aspectRatio)
  266 + startScale = qRound((float) rows / (float) windowHeight);
  267 + else
  268 + startScale = qRound((float) cols / (float) windowWidth);
  269 + for (float scale = startScale; scale >= minScale; scale -= (1.0 - scaleFactor)) {
  270 + Template scaleImg(src.file, Mat());
  271 + scaleImg.file.set("scale", scale);
  272 + resize(src, scaleImg, Size(qRound(cols / scale), qRound(rows / scale)));
  273 + transform->project(scaleImg, dst);
  274 + if (takeLargestScale && !dst.file.rects().empty())
  275 + return;
301 276 }
302 277 }
303 278  
... ... @@ -388,21 +363,12 @@ private:
388 363  
389 364 void project(const Template &src, Template &dst) const
390 365 {
391   - (void)src;(void)dst;qFatal("nope");
392   - }
393   -
394   - void project(const TemplateList &src, TemplateList &dst) const
395   - {
396   - QList<Rect> rects;
397   - QList<float> confidences;
398   - foreach (const Template &t, src) {
399   - if (t.file.contains("Detection")) {
400   - rects.append(OpenCVUtils::toRect(t.file.get<QRectF>("Detection")));
401   - confidences.append(t.file.get<float>("Confidence"));
402   - }
403   - }
  366 + dst = src;
  367 + if (!dst.file.contains("Confidences"))
  368 + return;
404 369  
405   - // Compute overlap between rectangles and create discrete Laplacian matrix
  370 + //Compute overlap between rectangles and create discrete Laplacian matrix
  371 + QList<Rect> rects = OpenCVUtils::toRects(src.file.rects());
406 372 int n = rects.size();
407 373 if (n == 0)
408 374 return;
... ... @@ -466,6 +432,7 @@ private:
466 432 cnts[i] = 0;
467 433 }
468 434  
  435 + QList<float> confidences = dst.file.getList<float>("Confidences");
469 436 for (int i = 0; i < n; i++) {
470 437 mx = 0.0;
471 438 mxIdx = -1;
... ... @@ -500,13 +467,6 @@ private:
500 467 }
501 468 }
502 469  
503   - for (int i=0; i<consolidatedRects.size(); i++) {
504   - Template t(src.first().file);
505   - t.file.set("Detection", OpenCVUtils::fromRect(consolidatedRects.at(i)));
506   - t.file.set("Confidence", consolidatedConfidences.at(i));
507   - dst.append(t);
508   - }
509   -
510 470 delete [] midX;
511 471 delete [] midY;
512 472 delete [] avgWidth;
... ... @@ -514,6 +474,8 @@ private:
514 474 delete [] confs;
515 475 delete [] cnts;
516 476  
  477 + dst.file.setRects(consolidatedRects);
  478 + dst.file.setList<float>("Confidences", consolidatedConfidences);
517 479 }
518 480 };
519 481  
... ...