cv::dnn::NMSBoxes, 代码精简
时间: 2023-06-22 14:35:03 浏览: 355
以下是一个简洁的实现cv::dnn::NMSBoxes函数的代码:
```
void NMSBoxes(const std::vector<cv::Rect>& boxes, const std::vector<float>& scores, float score_threshold, float nms_threshold, std::vector<int>& indices) {
std::vector<int> order(scores.size());
std::iota(order.begin(), order.end(), 0);
std::sort(order.begin(), order.end(), [&](int i, int j) { return scores[i] > scores[j]; });
indices.clear();
for (int i = 0; i < order.size(); ++i) {
int idx = order[i];
if (scores[idx] < score_threshold) break;
bool keep = true;
for (int j : indices) {
if (keep) {
float overlap = static_cast<float>((boxes[idx] & boxes[j]).area()) / static_cast<float>((boxes[idx] | boxes[j]).area());
keep = overlap <= nms_threshold;
} else break;
}
if (keep) indices.push_back(idx);
}
}
```
这个实现使用了C++11的特性,包括lambda表达式、STL算法和std::iota函数。它的原理与之前的实现相同,但是更加简洁易懂。
阅读全文