帮我将以下函数翻译成C++,def boxnms(bboxes, scores, threshold): if len(bboxes) == 0: return [] x1 = bboxes[:, 0] y1 = bboxes[:, 1] x2 = bboxes[:, 0] + bboxes[:, 2] y2 = bboxes[:, 1] + bboxes[:, 3] areas = (x2 - x1 + 1) * (y2 - y1 + 1) order = scores.argsort()[::-1] keep = [] while order.size > 0: i = order[0] keep.append(i) xx1 = np.maximum(x1[i], x1[order[1:]]) yy1 = np.maximum(y1[i], y1[order[1:]]) xx2 = np.minimum(x2[i], x2[order[1:]]) yy2 = np.minimum(y2[i], y2[order[1:]]) w = np.maximum(0.0, xx2 - xx1 + 1) h = np.maximum(0.0, yy2 - yy1 + 1) overlap = w * h / areas[order[1:]] inds = np.where(overlap <= threshold)[0] order = order[inds + 1] return keep 其中bboxes输入为std::vector<cv::Rect> bboxes; scores为std::vector<int>
时间: 2024-01-24 08:04:24 浏览: 206
以下是将该函数翻译成 C++ 的代码:
```
std::vector<int> boxnms(std::vector<cv::Rect> bboxes, std::vector<int> scores, float threshold) {
if (bboxes.empty()) {
return std::vector<int>();
}
std::vector<int> x1(bboxes.size());
std::vector<int> y1(bboxes.size());
std::vector<int> x2(bboxes.size());
std::vector<int> y2(bboxes.size());
std::vector<int> areas(bboxes.size());
for (int i = 0; i < bboxes.size(); i++) {
x1[i] = bboxes[i].x;
y1[i] = bboxes[i].y;
x2[i] = bboxes[i].x + bboxes[i].width;
y2[i] = bboxes[i].y + bboxes[i].height;
areas[i] = (x2[i] - x1[i] + 1) * (y2[i] - y1[i] + 1);
}
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]; });
std::vector<int> keep;
while (!order.empty()) {
int i = order[0];
keep.push_back(i);
std::vector<int> xx1(order.size() - 1);
std::vector<int> yy1(order.size() - 1);
std::vector<int> xx2(order.size() - 1);
std::vector<int> yy2(order.size() - 1);
for (int j = 1; j < order.size(); j++) {
xx1[j - 1] = std::max(x1[i], x1[order[j]]);
yy1[j - 1] = std::max(y1[i], y1[order[j]]);
xx2[j - 1] = std::min(x2[i], x2[order[j]]);
yy2[j - 1] = std::min(y2[i], y2[order[j]]);
}
std::vector<int> w(order.size() - 1);
std::vector<int> h(order.size() - 1);
for (int j = 0; j < w.size(); j++) {
w[j] = std::max(0, xx2[j] - xx1[j] + 1);
h[j] = std::max(0, yy2[j] - yy1[j] + 1);
}
std::vector<float> overlap(order.size() - 1);
for (int j = 0; j < overlap.size(); j++) {
overlap[j] = (float)(w[j] * h[j]) / areas[order[j + 1]];
}
std::vector<int> inds;
for (int j = 0; j < overlap.size(); j++) {
if (overlap[j] <= threshold) {
inds.push_back(j);
}
}
order.erase(order.begin());
for (int j = 0; j < inds.size(); j++) {
order.erase(order.begin() + inds[j]);
}
}
return keep;
}
```
阅读全文