I have many Rect rectangle
store in vector<Rect>
. But it has many repeat rectangle inside. How to remove them? For example:
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);
How to remove the repeat rectangle in vector<Rect>
?
You need to create a Strict Weak Ordering on Rect
. For rectangles, comparing their individual components is enough.
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));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.