如何从矢量中删除重复矩形<Rect>?
how to remove repeat rectangle from vector<Rect>?
我在vector<Rect>
中存储了许多Rect rectangle
。但它内部有许多重复的矩形。如何移除它们?例如:
Point Pt1(267, 83);
Point Pt2(487, 167);
Rect rec1(Pt1, Pt2);
GroundTruthSet.push_back(rec1);
Point Pt3(257, 90);
Point Pt4(450, 150);
Rect rec2(Pt3, Pt4);
GroundTruthSet.push_back(rec2);
Point Pt5(267, 83);
Point Pt6(487, 167);
Rect rec3(Pt1, Pt2);
GroundTruthSet.push_back(rec3);
如何删除vector<Rect>
中的重复矩形?
您需要在Rect
上创建一个严格弱排序。对于矩形,比较它们各自的组件就足够了。
auto comp_lt = [](const Rect& lhs, const Rect& rhs) {
// compare each component in the following lexicographical order
return std::tie(lhs.x, lhs.y, lhs.width, lhs.height) <
std::tie(rhs.x, rhs.y, rhs.width, rhs.height);
};
auto comp_eq = [](const Rect& lhs, const Rect& rhs) {
// `std::unique` uses equality-comparison, not less-than-comparison
return std::tie(lhs.x, lhs.y, lhs.width, lhs.height) ==
std::tie(rhs.x, rhs.y, rhs.width, rhs.height);
};
std::sort(std::begin(GroundTruthSet), std::end(GroundTruthSet), comp_lt);
auto pivot = std::unique(std::begin(GroundTruthSet), std::end(GroundTruthSet), comp_eq);
v.erase(pivot, std::end(GroundTruthSet));
std::sort
、std::unique
、std::tie
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- 如何显式调用运算符<<
- OpenCV Rect x 和 y 表示 (column,row) 或 (row,column)
- 模板操作员&lt;未打电话
- C / CUDA中的模板方法是3个角括号(&lt;&lt;&lt;)
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 错误:调用"std::vector<:vector<int>>::p ush_back(std::vector<std::__cxx11::basic_string<
- C 建造者Clang STD :: Sill,找不到超载的操作员&lt;
- 为什么STD :: MAP需要操作员&lt;以及我如何写一个
- 为什么“操作员”需要const但不是为“运营商&lt;”
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- OpenCV断言对RECT ROI失败了
- 使 ShowHint 使用多个不同的 Rect 处理自定义控件,每个 Rect 都有自己的 Hint
- Qt QGraphicsScene::setSceneRect() 实际上并没有设置场景 rect
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 什么是模板&lt;&gt;inline bla bla