| 46 | } |
| 47 | |
| 48 | std::vector<int> NonMaxSuppression(DetectedObjects& inputDetections, float iouThresh) |
| 49 | { |
| 50 | // Sort indicies of detections by highest score to lowest. |
| 51 | std::vector<unsigned int> sortedIndicies = GenerateRangeK(inputDetections.size()); |
| 52 | std::sort(sortedIndicies.begin(), sortedIndicies.end(), |
| 53 | [&inputDetections](int idx1, int idx2) |
| 54 | { |
| 55 | return inputDetections[idx1].GetScore() > inputDetections[idx2].GetScore(); |
| 56 | }); |
| 57 | |
| 58 | std::vector<bool> visited(inputDetections.size(), false); |
| 59 | std::vector<int> outputIndiciesAfterNMS; |
| 60 | |
| 61 | for (int i=0; i < inputDetections.size(); ++i) |
| 62 | { |
| 63 | // Each new unvisited detect should be kept. |
| 64 | if (!visited[sortedIndicies[i]]) |
| 65 | { |
| 66 | outputIndiciesAfterNMS.emplace_back(sortedIndicies[i]); |
| 67 | visited[sortedIndicies[i]] = true; |
| 68 | } |
| 69 | |
| 70 | // Look for detections to suppress. |
| 71 | for (int j=i+1; j<inputDetections.size(); ++j) |
| 72 | { |
| 73 | // Skip if already kept or suppressed. |
| 74 | if (!visited[sortedIndicies[j]]) |
| 75 | { |
| 76 | // Detects must have the same label to be suppressed. |
| 77 | if (inputDetections[sortedIndicies[j]].GetLabel() == inputDetections[sortedIndicies[i]].GetLabel()) |
| 78 | { |
| 79 | auto iou = IntersectionOverUnion(inputDetections[sortedIndicies[i]], |
| 80 | inputDetections[sortedIndicies[j]]); |
| 81 | if (iou > iouThresh) |
| 82 | { |
| 83 | visited[sortedIndicies[j]] = true; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | return outputIndiciesAfterNMS; |
| 90 | } |
| 91 | |
| 92 | } // namespace od |
no test coverage detected