Commit ceb1187c05048c2d98b27df105bb40849ad8e619

Authored by Scott Klum
1 parent 95076d7e

Added minNeighbors parameter

openbr/core/opencvutils.cpp
... ... @@ -436,7 +436,7 @@ public:
436 436 };
437 437  
438 438 // TODO: Make sure case where no confidences are inputted works.
439   -void OpenCVUtils::group(QList<Rect> &rects, QList<float> &confidences, float confidenceThreshold, float epsilon)
  439 +void OpenCVUtils::group(QList<Rect> &rects, QList<float> &confidences, float confidenceThreshold, int minNeighbors, float epsilon)
440 440 {
441 441 if (rects.isEmpty())
442 442 return;
... ... @@ -450,7 +450,7 @@ void OpenCVUtils::group(QList&lt;Rect&gt; &amp;rects, QList&lt;float&gt; &amp;confidences, float con
450 450 vector<Rect> rrects(nClasses);
451 451  
452 452 // Total number of rects in each class
453   - vector<int> rweights(nClasses, 0);
  453 + vector<int> neighbors(nClasses, 0);
454 454 vector<float> rejectWeights(nClasses, -std::numeric_limits<float>::max());
455 455  
456 456 for (size_t i = 0; i < labels.size(); i++)
... ... @@ -460,7 +460,7 @@ void OpenCVUtils::group(QList&lt;Rect&gt; &amp;rects, QList&lt;float&gt; &amp;confidences, float con
460 460 rrects[cls].y += rects[i].y;
461 461 rrects[cls].width += rects[i].width;
462 462 rrects[cls].height += rects[i].height;
463   - rweights[cls]++;
  463 + neighbors[cls]++;
464 464 }
465 465  
466 466 if (useConfidences)
... ... @@ -478,7 +478,7 @@ void OpenCVUtils::group(QList&lt;Rect&gt; &amp;rects, QList&lt;float&gt; &amp;confidences, float con
478 478 for (int i = 0; i < nClasses; i++)
479 479 {
480 480 Rect r = rrects[i];
481   - float s = 1.f/rweights[i];
  481 + float s = 1.f/neighbors[i];
482 482 rrects[i] = Rect(saturate_cast<int>(r.x*s),
483 483 saturate_cast<int>(r.y*s),
484 484 saturate_cast<int>(r.width*s),
... ... @@ -488,7 +488,7 @@ void OpenCVUtils::group(QList&lt;Rect&gt; &amp;rects, QList&lt;float&gt; &amp;confidences, float con
488 488 rects.clear();
489 489 confidences.clear();
490 490  
491   - // Aggregate by comparing average rectangles against other average rectangels
  491 + // Aggregate by comparing average rectangles against other average rectangles
492 492 for (int i = 0; i < nClasses; i++)
493 493 {
494 494 // Average rectangle
... ... @@ -496,19 +496,22 @@ void OpenCVUtils::group(QList&lt;Rect&gt; &amp;rects, QList&lt;float&gt; &amp;confidences, float con
496 496  
497 497 // Used to eliminate rectangles with few neighbors in the case of no weights
498 498 // int n1 = levelWeights ? rejectLevels[i] : rweights[i];
499   - float w1 = rejectWeights[i];
  499 + const float w1 = rejectWeights[i];
500 500  
501 501 // Eliminate rectangle if it doesn't meet confidence criteria
502 502 if (w1 <= confidenceThreshold)
503 503 continue;
504 504  
  505 + const int n1 = neighbors[i];
  506 + if (n1 < minNeighbors)
  507 + continue;
  508 +
505 509 // filter out small face rectangles inside large rectangles
506 510 int j;
507 511 for (j = 0; j < nClasses; j++)
508 512 {
509   - float w2 = rejectWeights[j];
510   -
511   - if (j == i)
  513 + const int n2 = neighbors[j];
  514 + if (j == i || n2 < minNeighbors)
512 515 continue;
513 516  
514 517 Rect r2 = rrects[j];
... ... @@ -516,13 +519,11 @@ void OpenCVUtils::group(QList&lt;Rect&gt; &amp;rects, QList&lt;float&gt; &amp;confidences, float con
516 519 int dx = saturate_cast<int>(r2.width * epsilon);
517 520 int dy = saturate_cast<int>(r2.height * epsilon);
518 521  
  522 + float w2 = rejectWeights[j];
  523 +
519 524 // If, r1 is within the r2 AND
520   - // the second rectangle reaches a later stage than the first
521   - // where both the first and the second must have a stage greater than three OR
522   - // the first doens't reach the third stage.
523   - // Changeto: second rectangle has a higher confidence than the first OR
524   - // the first has a low confidence.
525   - // Then, eliminate the first rectangle.
  525 + // r2 has a higher confidence than r1
  526 + // then, eliminate the r1
526 527 if(r1.x >= r2.x - dx &&
527 528 r1.y >= r2.y - dy &&
528 529 r1.x + r1.width <= r2.x + r2.width + dx &&
... ... @@ -531,7 +532,6 @@ void OpenCVUtils::group(QList&lt;Rect&gt; &amp;rects, QList&lt;float&gt; &amp;confidences, float con
531 532 break;
532 533 }
533 534  
534   - // Need to return rects and confidences
535 535 if( j == nClasses )
536 536 {
537 537 rects.append(r1);
... ...
openbr/core/opencvutils.h
... ... @@ -102,7 +102,7 @@ namespace OpenCVUtils
102 102 float overlap(const QRectF &rect1, const QRectF &rect2);
103 103  
104 104 // Misc
105   - void group(QList<cv::Rect> &rects, QList<float> &confidences, float confidenceThreshold, float epsilon);
  105 + void group(QList<cv::Rect> &rects, QList<float> &confidences, float confidenceThreshold, int minNeighbors, float epsilon);
106 106 void flip(const br::Template &src, br::Template &dst, int axis, bool flipMat=true, bool flipPoints=true, bool flipRects=true);
107 107 void flip(const br::TemplateList &src, br::TemplateList &dst, int axis, bool flipMat=true, bool flipPoints=true, bool flipRects=true);
108 108  
... ...