TODO: Make sure case where no confidences are inputted works.
| 437 | |
| 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) |
| 440 | { |
| 441 | if (rects.isEmpty()) |
| 442 | return; |
| 443 | |
| 444 | const bool useConfidences = !confidences.isEmpty(); |
| 445 | |
| 446 | vector<int> labels; |
| 447 | int nClasses = cv::partition(rects.toVector().toStdVector(), labels, SimilarRects(epsilon)); |
| 448 | |
| 449 | // Rect for each class (class meaning identity assigned by partition) |
| 450 | vector<Rect> rrects(nClasses); |
| 451 | |
| 452 | // Total number of rects in each class |
| 453 | vector<int> rweights(nClasses, 0); |
| 454 | vector<float> rejectWeights(nClasses, -std::numeric_limits<float>::max()); |
| 455 | |
| 456 | for (size_t i = 0; i < labels.size(); i++) |
| 457 | { |
| 458 | int cls = labels[i]; |
| 459 | rrects[cls].x += rects[i].x; |
| 460 | rrects[cls].y += rects[i].y; |
| 461 | rrects[cls].width += rects[i].width; |
| 462 | rrects[cls].height += rects[i].height; |
| 463 | rweights[cls]++; |
| 464 | } |
| 465 | |
| 466 | if (useConfidences) |
| 467 | { |
| 468 | // For each class, find maximum confidence |
| 469 | for (size_t i = 0; i < labels.size(); i++) |
| 470 | { |
| 471 | int cls = labels[i]; |
| 472 | if (confidences[i] > rejectWeights[cls]) |
| 473 | rejectWeights[cls] = confidences[i]; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | // Find average rectangle for all classes |
| 478 | for (int i = 0; i < nClasses; i++) |
| 479 | { |
| 480 | Rect r = rrects[i]; |
| 481 | float s = 1.f/rweights[i]; |
| 482 | rrects[i] = Rect(saturate_cast<int>(r.x*s), |
| 483 | saturate_cast<int>(r.y*s), |
| 484 | saturate_cast<int>(r.width*s), |
| 485 | saturate_cast<int>(r.height*s)); |
| 486 | } |
| 487 | |
| 488 | rects.clear(); |
| 489 | confidences.clear(); |
| 490 | |
| 491 | // Aggregate by comparing average rectangles against other average rectangels |
| 492 | for (int i = 0; i < nClasses; i++) |
| 493 | { |
| 494 | // Average rectangle |
| 495 | Rect r1 = rrects[i]; |
| 496 |
no test coverage detected