Commit ceb1187c05048c2d98b27df105bb40849ad8e619
1 parent
95076d7e
Added minNeighbors parameter
Showing
2 changed files
with
17 additions
and
17 deletions
openbr/core/opencvutils.cpp
| @@ -436,7 +436,7 @@ public: | @@ -436,7 +436,7 @@ public: | ||
| 436 | }; | 436 | }; |
| 437 | 437 | ||
| 438 | // TODO: Make sure case where no confidences are inputted works. | 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 | if (rects.isEmpty()) | 441 | if (rects.isEmpty()) |
| 442 | return; | 442 | return; |
| @@ -450,7 +450,7 @@ void OpenCVUtils::group(QList<Rect> &rects, QList<float> &confidences, float con | @@ -450,7 +450,7 @@ void OpenCVUtils::group(QList<Rect> &rects, QList<float> &confidences, float con | ||
| 450 | vector<Rect> rrects(nClasses); | 450 | vector<Rect> rrects(nClasses); |
| 451 | 451 | ||
| 452 | // Total number of rects in each class | 452 | // Total number of rects in each class |
| 453 | - vector<int> rweights(nClasses, 0); | 453 | + vector<int> neighbors(nClasses, 0); |
| 454 | vector<float> rejectWeights(nClasses, -std::numeric_limits<float>::max()); | 454 | vector<float> rejectWeights(nClasses, -std::numeric_limits<float>::max()); |
| 455 | 455 | ||
| 456 | for (size_t i = 0; i < labels.size(); i++) | 456 | for (size_t i = 0; i < labels.size(); i++) |
| @@ -460,7 +460,7 @@ void OpenCVUtils::group(QList<Rect> &rects, QList<float> &confidences, float con | @@ -460,7 +460,7 @@ void OpenCVUtils::group(QList<Rect> &rects, QList<float> &confidences, float con | ||
| 460 | rrects[cls].y += rects[i].y; | 460 | rrects[cls].y += rects[i].y; |
| 461 | rrects[cls].width += rects[i].width; | 461 | rrects[cls].width += rects[i].width; |
| 462 | rrects[cls].height += rects[i].height; | 462 | rrects[cls].height += rects[i].height; |
| 463 | - rweights[cls]++; | 463 | + neighbors[cls]++; |
| 464 | } | 464 | } |
| 465 | 465 | ||
| 466 | if (useConfidences) | 466 | if (useConfidences) |
| @@ -478,7 +478,7 @@ void OpenCVUtils::group(QList<Rect> &rects, QList<float> &confidences, float con | @@ -478,7 +478,7 @@ void OpenCVUtils::group(QList<Rect> &rects, QList<float> &confidences, float con | ||
| 478 | for (int i = 0; i < nClasses; i++) | 478 | for (int i = 0; i < nClasses; i++) |
| 479 | { | 479 | { |
| 480 | Rect r = rrects[i]; | 480 | Rect r = rrects[i]; |
| 481 | - float s = 1.f/rweights[i]; | 481 | + float s = 1.f/neighbors[i]; |
| 482 | rrects[i] = Rect(saturate_cast<int>(r.x*s), | 482 | rrects[i] = Rect(saturate_cast<int>(r.x*s), |
| 483 | saturate_cast<int>(r.y*s), | 483 | saturate_cast<int>(r.y*s), |
| 484 | saturate_cast<int>(r.width*s), | 484 | saturate_cast<int>(r.width*s), |
| @@ -488,7 +488,7 @@ void OpenCVUtils::group(QList<Rect> &rects, QList<float> &confidences, float con | @@ -488,7 +488,7 @@ void OpenCVUtils::group(QList<Rect> &rects, QList<float> &confidences, float con | ||
| 488 | rects.clear(); | 488 | rects.clear(); |
| 489 | confidences.clear(); | 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 | for (int i = 0; i < nClasses; i++) | 492 | for (int i = 0; i < nClasses; i++) |
| 493 | { | 493 | { |
| 494 | // Average rectangle | 494 | // Average rectangle |
| @@ -496,19 +496,22 @@ void OpenCVUtils::group(QList<Rect> &rects, QList<float> &confidences, float con | @@ -496,19 +496,22 @@ void OpenCVUtils::group(QList<Rect> &rects, QList<float> &confidences, float con | ||
| 496 | 496 | ||
| 497 | // Used to eliminate rectangles with few neighbors in the case of no weights | 497 | // Used to eliminate rectangles with few neighbors in the case of no weights |
| 498 | // int n1 = levelWeights ? rejectLevels[i] : rweights[i]; | 498 | // int n1 = levelWeights ? rejectLevels[i] : rweights[i]; |
| 499 | - float w1 = rejectWeights[i]; | 499 | + const float w1 = rejectWeights[i]; |
| 500 | 500 | ||
| 501 | // Eliminate rectangle if it doesn't meet confidence criteria | 501 | // Eliminate rectangle if it doesn't meet confidence criteria |
| 502 | if (w1 <= confidenceThreshold) | 502 | if (w1 <= confidenceThreshold) |
| 503 | continue; | 503 | continue; |
| 504 | 504 | ||
| 505 | + const int n1 = neighbors[i]; | ||
| 506 | + if (n1 < minNeighbors) | ||
| 507 | + continue; | ||
| 508 | + | ||
| 505 | // filter out small face rectangles inside large rectangles | 509 | // filter out small face rectangles inside large rectangles |
| 506 | int j; | 510 | int j; |
| 507 | for (j = 0; j < nClasses; j++) | 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 | continue; | 515 | continue; |
| 513 | 516 | ||
| 514 | Rect r2 = rrects[j]; | 517 | Rect r2 = rrects[j]; |
| @@ -516,13 +519,11 @@ void OpenCVUtils::group(QList<Rect> &rects, QList<float> &confidences, float con | @@ -516,13 +519,11 @@ void OpenCVUtils::group(QList<Rect> &rects, QList<float> &confidences, float con | ||
| 516 | int dx = saturate_cast<int>(r2.width * epsilon); | 519 | int dx = saturate_cast<int>(r2.width * epsilon); |
| 517 | int dy = saturate_cast<int>(r2.height * epsilon); | 520 | int dy = saturate_cast<int>(r2.height * epsilon); |
| 518 | 521 | ||
| 522 | + float w2 = rejectWeights[j]; | ||
| 523 | + | ||
| 519 | // If, r1 is within the r2 AND | 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 | if(r1.x >= r2.x - dx && | 527 | if(r1.x >= r2.x - dx && |
| 527 | r1.y >= r2.y - dy && | 528 | r1.y >= r2.y - dy && |
| 528 | r1.x + r1.width <= r2.x + r2.width + dx && | 529 | r1.x + r1.width <= r2.x + r2.width + dx && |
| @@ -531,7 +532,6 @@ void OpenCVUtils::group(QList<Rect> &rects, QList<float> &confidences, float con | @@ -531,7 +532,6 @@ void OpenCVUtils::group(QList<Rect> &rects, QList<float> &confidences, float con | ||
| 531 | break; | 532 | break; |
| 532 | } | 533 | } |
| 533 | 534 | ||
| 534 | - // Need to return rects and confidences | ||
| 535 | if( j == nClasses ) | 535 | if( j == nClasses ) |
| 536 | { | 536 | { |
| 537 | rects.append(r1); | 537 | rects.append(r1); |
openbr/core/opencvutils.h
| @@ -102,7 +102,7 @@ namespace OpenCVUtils | @@ -102,7 +102,7 @@ namespace OpenCVUtils | ||
| 102 | float overlap(const QRectF &rect1, const QRectF &rect2); | 102 | float overlap(const QRectF &rect1, const QRectF &rect2); |
| 103 | 103 | ||
| 104 | // Misc | 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 | void flip(const br::Template &src, br::Template &dst, int axis, bool flipMat=true, bool flipPoints=true, bool flipRects=true); | 106 | void flip(const br::Template &src, br::Template &dst, int axis, bool flipMat=true, bool flipPoints=true, bool flipRects=true); |
| 107 | void flip(const br::TemplateList &src, br::TemplateList &dst, int axis, bool flipMat=true, bool flipPoints=true, bool flipRects=true); | 107 | void flip(const br::TemplateList &src, br::TemplateList &dst, int axis, bool flipMat=true, bool flipPoints=true, bool flipRects=true); |
| 108 | 108 |