c++ i < -threshold ? 1 : i >是什么意思
时间: 2024-02-18 12:05:24 浏览: 134
这段代码是一个三元运算符,它的形式为:
```
(condition) ? (value_if_true) : (value_if_false)
```
它的作用是根据条件的真假选择两个不同的值。
在这段代码中,条件是 `i < -threshold`,如果这个条件为真,那么表达式的值就是 `1`,否则表达式的值就是 `i >`。这里似乎缺少一个值,可能是一个数字或者变量名。如果您能提供更多的上下文信息,我会尽力帮助您理解这段代码的含义。
相关问题
帮我将以下函数翻译成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>
以下是将该函数翻译成 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;
}
```
在vs2015 c++ .h中加入这段代码会报重定义 namespace cv_dnn { namespace { template <typename T> static inline bool SortScorePairDescend(const std::pair<float, T>& pair1, const std::pair<float, T>& pair2) { return pair1.first > pair2.first; } } // namespace inline void GetMaxScoreIndex(const std::vector<float>& scores, const float threshold, const int top_k, std::vector<std::pair<float, int> >& score_index_vec) { for (size_t i = 0; i < scores.size(); ++i) { if (scores[i] > threshold) { score_index_vec.push_back(std::make_pair(scores[i], i)); } } std::stable_sort(score_index_vec.begin(), score_index_vec.end(), SortScorePairDescend<int>); if (top_k > 0 && top_k < (int)score_index_vec.size()) { score_index_vec.resize(top_k); } } template <typename BoxType> inline void NMSFast_(const std::vector<BoxType>& bboxes, const std::vector<float>& scores, const float score_threshold, const float nms_threshold, const float eta, const int top_k, std::vector<int>& indices, float(*computeOverlap)(const BoxType&, const BoxType&)) { CV_Assert(bboxes.size() == scores.size()); std::vector<std::pair<float, int> > score_index_vec; GetMaxScoreIndex(scores, score_threshold, top_k, score_index_vec); // Do nms. float adaptive_threshold = nms_threshold; indices.clear(); for (size_t i = 0; i < score_index_vec.size(); ++i) { const int idx = score_index_vec[i].second; bool keep = true; for (int k = 0; k < (int)indices.size() && keep; ++k) { const int kept_idx = indices[k]; float overlap = computeOverlap(bboxes[idx], bboxes[kept_idx]); keep = overlap <= adaptive_threshold; } if (keep) indices.push_back(idx); if (keep && eta < 1 && adaptive_threshold > 0.5) { adaptive_threshold *= eta; } } } // copied from opencv 3.4, not exist in 3.0 template<typename Tp> static inline double jaccardDistance_(const Rect_<Tp>& a, const Rect<_Tp>& b) { Tp Aa = a.area(); Tp Ab = b.area(); if ((Aa + Ab) <= std::numeric_limits<Tp>::epsilon()) { // jaccard_index = 1 -> distance = 0 return 0.0; } double Aab = (a & b).area(); // distance = 1 - jaccard_index return 1.0 - Aab / (Aa + Ab - Aab); } template <typename T> static inline float rectOverlap(const T& a, const T& b) { return 1.f - static_cast<float>(jaccardDistance(a, b)); } void NMSBoxes(const std::vector<Rect>& bboxes, const std::vector<float>& scores, const float score_threshold, const float nms_threshold, std::vector<int>& indices, const float eta = 1, const int top_k = 0) { NMSFast(bboxes, scores, score_threshold, nms_threshold, eta, top_k, indices, rectOverlap); } }
在加入这段代码时,如果在同一个命名空间内有相同的函数或变量,则会报重定义的错误。如果你确定已经检查过代码,在其他地方没有定义相同的函数或变量,可以尝试将代码放在一个新的命名空间中。例如:
```cpp
namespace my_cv_dnn {
// 这里放你的代码
}
```
然后在使用时使用新的命名空间即可。
阅读全文