1、用向量vector ,添加的时候判断,相同名字的元素,置信度最大的框进行保存,如果有了就替换相关值,或者全部push 后遍历一遍删除相同的框子中,置信度最低的那个框
struct Detect
{
std::string name;
float prob;
cv::Rect rect;
};
void add_det(std::vector<Detect>& dets)
{
bool found = false;
for (auto& det : dets) {
if (det.name == label) {
if (conf > det.prob) {
det.prob = conf;
det.rect = bbox;
}
found = true;
break;
}
}
if (!found) {
dets.push_back({label, conf, bbox});
}
}
std::vector<Detect> dets;
std::string label = "狗";
cv::Rect bbox(363, 388, 69, 57);
float conf = 0.797;
dets.push_back({label, conf, bbox});
bbox = cv::Rect(366, 209, 101, 91);
dets.push_back({"猫", 0.702, bbox});
bbox = cv::Rect(113, 94, 533, 453);
dets.push_back({"猫", 0.669, bbox});
2、用字典,添加自定义结构体变量框数据
struct Detect
{
std::string name;
float prob;
cv::Rect rect;
};
std::map<std::string, Detect> detectMap;
std::string label = "猫";
cv::Rect bbox(363, 388, 69, 57);
float conf = 0.797;
auto it = detectMap.find(label);
if (it != detectMap.end()) {
if (conf > it->second.prob) {
it->second.prob = conf;
it->second.rect = bbox;
}
} else {
detectMap.insert({label, {label, conf, bbox}});
}
3、 重载操作符 opreate 写法
cpp
struct Detect {
std::string name;
float prob;
cv::Rect rect;
bool operator<(const Detect& other) const {
return prob < other.prob;
}
};
cpp
std::vector<Detect> dets;
std::sort(dets.begin(), dets.end());
这将使用重载的operator<来对dets中的元素进行排序,按照置信度从低到高的顺序进行排列。
希望这对你有帮助!👍🏻