如何从vector <rect>中删除重复矩形?

时间:2015-12-01 16:58:49

标签: c++ opencv3.0

$json = $_POST["jsonfile"]; $Query = $conn->prepare("INSERT INTO table1(somedata) VALUES(:data)"); $Query->bindValue(':data',$json); // In case of not using implode, this line gets error( Error : Notice: Array to string conversion) $Query->execute(); 我有很多Rect rectangle商店。但它里面有很多重复的矩形。如何删除它们?例如:

vector<Rect>

如何删除 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); 中的重复矩形?

1 个答案:

答案 0 :(得分:2)

您需要在Rect上创建Strict Weak Ordering。对于矩形,比较它们各自的组件就足够了。

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::sortstd::uniquestd::tie