簡體   English   中英

如何刪除向量的重復坐標<Point2f>

[英]how to delete repeating coordinates of vector<Point2f>

我將點的坐標傳遞給向量,並且有一些重復點,所以我想刪除其他重復點,只保留唯一的點。

例如:

vector<Point2f>  points;

points[0]=Point2f(1,1);
points[1]=Point2f(2,3);
points[2]=Point2f(1,1);
points[3]=Point2f(2,3);
points[4]=Point2f(1,1);
points[5]=Point2f(4,1);

我想得到這樣的結果:

points[0]=Point2f(1,1);
points[1]=Point2f(2,3);
points[2]=Point2f(4,1);

PS 元素的順序不變。

我嘗試過的顯示如下:

#include <opencv2/core/core.hpp>

#include <vector>
#include<iostream>

using namespace std;
using namespace cv;

int main()
{
    vector<Point2f>  pointTemp;

    pointTemp[0]=Point2f(1,1);
    pointTemp[1]=Point2f(2,3);
    pointTemp[2]=Point2f(1,1);
    pointTemp[3]=Point2f(2,3);
    pointTemp[4]=Point2f(1,1);
    pointTemp[5]=Point2f(4,1);

    for(vector<Point2f>::iterator it=pointTemp.begin();it!=pointTemp.end();it++)
    {
        for(vector<Point2f>::iterator it1=it+1;it1!=pointTemp.end();)
        {
            if(it->x==it1->x&&it->y==it1->y)
            {
                it1=pointTemp.erase(it1);
            }
            else
            {
                it1++;
            }
        }
    }
    //cout<<pointTemp.size()<<endl;

    return 0;
}

這是我的破解。 它可能需要您將--std=c++11作為參數傳遞--std=c++11 g++。 請注意,保持唯一元素的插入順序。 運行時復雜度也是O(N)

// remove_duplicates: removes all duplicated elements from the vector passed in
void remove_duplicates(std::vector<Point2f>& vec)
{
    std::unordered_set<Point2f> pointset;  // unordered_set is a hash table implementation

    auto itor = vec.begin();
    while (itor != vec.end())
    {
        if (pointset.find(*itor) != pointset.end())   // O(1) lookup time for unordered_set
        {
            itor = vec.erase(itor); // vec.erase returns the next valid iterator
        }
        else
        {
            pointset.insert(*itor);
            itor++;
        }
    }
}

作為使用unordered_set的結果,上述函數需要事先為 Point2f 聲明一個哈希函數。 您可以隨意定義它。 我的簡單實現如下。

您可能還需要為 Point2f 定義==運算符以及適當的構造函數以滿足向量和 unordered_set 語義。

完整代碼清單:

#include <vector>
#include <unordered_set>


struct Point2f
{
    float x;
    float y;
    Point2f(float a, float b) : x(a), y(b) {}
    Point2f() : x(0), y(0) {}
};

bool operator==(const Point2f& pt1, const Point2f& pt2)
{
    return ((pt1.x == pt2.x) && (pt1.y == pt2.y));
}

namespace std
{
    template<>
    struct hash<Point2f>
    {
        size_t operator()(Point2f const& pt) const
        {
            return (size_t)(pt.x*100 + pt.y);
        }
    };
}


void removedupes(std::vector<Point2f> & vec)
{
    std::unordered_set<Point2f> pointset;

    auto itor = vec.begin();
    while (itor != vec.end())
    {
        if (pointset.find(*itor) != pointset.end())
        {
            itor = vec.erase(itor);
        }
        else
        {
            pointset.insert(*itor);
            itor++;
        }
    }
}


int main(int argc, char* argv[])
{
    std::vector<Point2f>  pointTemp;

    pointTemp.resize(6);

    pointTemp[0]=Point2f(1,1);
    pointTemp[1]=Point2f(2,3);
    pointTemp[2]=Point2f(1,1);
    pointTemp[3]=Point2f(2,3);
    pointTemp[4]=Point2f(1,1);
    pointTemp[5]=Point2f(4,1);

    removedupes(pointTemp);

    return 0;
}

這可以通過首先對點進行排序(使用 std::sort)然后消除重復點(使用 std::unique)來完成。 為此,您需要一個函數 compare()

#include <algorithm>

// Lexicographic compare, same as for ordering words in a dictionnary:
// test first 'letter of the word' (x coordinate), if same, test 
// second 'letter' (y coordinate).
bool lexico_compare(const Point2f& p1, const Point2f& p2) {
    if(p1.x < p2.x) { return true; }
    if(p1.x > p2.x) { return false; }
    return (p1.y < p2.y);
}


 bool points_are_equal(const Point2f& p1, const Point2f& p2) {
   return ((p1.x == p2.x) && (p1.y == p2.y));
 }

void remove_duplicates(std::vector<Point2f>& points) {
    // Note: std::unique leaves a 'queue' of duplicated elements
    // at the end of the vector, and returns an iterator that indicates
    // where to stop (and where to 'erase' the queue)
    std::sort(points.begin(), points.end(), lexico_compare);
    points.erase(std::unique(points.begin(), points.end(), points_are_equal), points.end());
}

注意 1:您可以使用 C++0x11 lambdas 代替兩個函數 lexico_compare 和 points_are_equal 來縮短代碼。

注2:如果你需要保持點的順序,你可以做一個間接排序,並跟蹤哪些點是重復的。

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

struct Point2f
{
    float x;
    float y;
};

int main(int argc, char const *argv[])
{
    std::vector<Point2f> points =
    {
        {1, 1}, {2, 3}, {1, 1}, {2, 3}, {1, 1}, {4, 1}
    };

    auto print = [&]()
    {
        for (const auto &point : points)
        {
            std::cout << "(" << point.x << " " << point.y << ") ";
        }
        std::cout << std::endl;
    };

    // first sort
    std::sort(points.begin(), points.end(), [](const Point2f & lhs, const Point2f & rhs)
    {
        return lhs.x < rhs.x && lhs.y < rhs.y;
    });

    // print
    print();

    // remove duplicated element
    auto it = std::unique(points.begin(), points.end(), [](const Point2f & lhs, const Point2f & rhs)
    {
        return lhs.x == rhs.x && lhs.y == rhs.y;
    });
    points.resize(std::distance(points.begin(), it));

    // print
    print();

    return 0;
}

請小心使用equal function 如果我們的目標是剔除足夠similar的點,我們應該使用一個散列,為similar點提供相同的散列值,並使用一個approximate-equalsimilar points分組為similar points 在我的情況下,我使用以下內容:

# include <iostream>
# include <vector>
# include <unordered_set>
# include <utility>

# include <Eigen/Dense>


const std::string red("\033[0;31m");
const std::string green("\033[1;32m");
const std::string yellow("\033[1;33m");
const std::string cyan("\033[0;36m");
const std::string magenta("\033[0;35m");
const std::string reset("\033[0m");

struct ApproxHash 
{
 std::size_t operator() (Eigen::Vector2d const& pt) const
 {


   size_t score = (size_t)(pt.x()*100) + (size_t)(pt.y()*10);
   std::cerr <<"Point: "<< pt.transpose()<< " has score: "<<score<<std::endl;
   return score; 
 }

};

struct ApproxEqual{
    // This is used to guarantee that no duplicates should happen when the hash collision happens. 
public:
 bool operator()(const Eigen::Vector2d & pt1, const Eigen::Vector2d & pt2) const {
    double threshold = 0.00001;
    bool xdiff = fabs(pt1.x() - pt2.x())<threshold;
    bool ydiff = fabs(pt1.y() - pt2.y())<threshold;

    bool result = (fabs(pt1.x() - pt2.x())<threshold) && (fabs(pt1.y() - pt2.y())<threshold);

    std::cerr<<cyan<<"Equal is called for: "<< pt1.transpose()<<" and "<<pt2.transpose()<<" which are " << result<<" equal. "<<" xdiff"<< xdiff<<", ydiff"<<ydiff<<reset<<std::endl;
    return result; 
}
};

void removeDuplicates(std::vector<Eigen::Vector2d>& vec)
{

    // If we would like to store values, we should use std::unordered_map.
    std::unordered_set<Eigen::Vector2d, ApproxHash, ApproxEqual> pointset;  

    auto ii = vec.begin();
    while (ii != vec.end())
    {
    std::cerr<<"Processing: "<<ii->transpose()<<std::endl;
        if (pointset.find(*ii) != pointset.end())   // O(1) lookup time for unordered_set
        {

        std::cerr<<red<<"Found duplicate: "<<ii->transpose()<<reset<<std::endl;
            vec.erase(ii); // vec.erase returns the next valid iterator
        }
        else
        {
            pointset.insert(*ii);
        std::cerr<<"Inserted: "<<ii->transpose()<<std::endl;
        ii++;
        }
    }
} // end of removeDuplicates

int main(int argc, char* argv[])
{


    std::vector<Eigen::Vector2d>  pointTemp;

    pointTemp.resize(15);
    pointTemp[0]=Eigen::Vector2d(1.0011121213,1);
    pointTemp[1]=Eigen::Vector2d(2.0,3.121);
    pointTemp[2]=Eigen::Vector2d(4.004,1.0);
    pointTemp[3]=Eigen::Vector2d(2.0,3.121);
    pointTemp[4]=Eigen::Vector2d(1.001112121,1);
    pointTemp[5]=Eigen::Vector2d(4.004,1.0);
    pointTemp[6]=Eigen::Vector2d(1.2,1);
    pointTemp[7]=Eigen::Vector2d(0.028297902,  0.302034);
    pointTemp[8]=Eigen::Vector2d(0.028297901,  0.302034);
    pointTemp[9]=Eigen::Vector2d(0.249941, 0.227669);
    pointTemp[10]=Eigen::Vector2d(0.249941, 0.227669);
    pointTemp[11]=Eigen::Vector2d(0.0206403,  0.304258);
    pointTemp[12]=Eigen::Vector2d(0.0206403,  0.304258);
    pointTemp[13]=Eigen::Vector2d(0.0206403,  0.304258);
    pointTemp[14]=Eigen::Vector2d(0.0282979,  0.302034);

    for (auto & point:pointTemp)
    {
      std::cout<<point.x()<<", "<< point.y()<<std::endl;
    }

    removeDuplicates(pointTemp);
    std::cerr<<green<<"Cleaned vector: "<<reset<<std::endl;

    for (auto & point:pointTemp)
    {
      std::cout<<point.x()<<", "<< point.y()<<std::endl;
    }

    return 0;
}


我們可以使用g++ -std=c++11 -I /usr/include vectorHash.cpp -o vectorHash來編譯示例。

如果我們使用exact equalexact hash ,那么不幸的是我們無法找到similar points

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM