简体   繁体   中英

how to delete repeating coordinates of vector<Point2f>

I passed coordinates of points into vector, and there are some repeating points, so I want to delete other repeating points and just keep the only points.

for example:

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);

I want to get the result like this:

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

PS The order of elements is unchanged.

What I have tried is show as below:

#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;
}

Here's my crack at it. It probably requires that you pass --std=c++11 as a parameter ot g++. Notice that the insertion order of unique elements is maintained. It's also O(N) for runtime complexity.

// 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++;
        }
    }
}

The above function, as a result of using unordered_set , requires a hash function to have been previously declared for Point2f. You can define this however you like. My simple implementation is below.

You'll also likely need to have an == operator defined for Point2f as well as appropriate constructors to satisfy the vector and unordered_set semantics.

Complete code listing:

#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;
}

This can be done by first sorting the points (using std::sort) then eliminating the duplicated points (using std::unique). To do that, you will need a function 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());
}

Note1: you can make the code shorter by using C++0x11 lambdas instead of the two functions lexico_compare and points_are_equal.

Note2: if you need to keep the order of the points, you can do an indirect sort instead, and keep track of which points are duplicated.

#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;
}

Please be careful with the equal function . If we aim to tick out similar enough points, we should use a hash that gives the same hash value for similar points and an approximate-equal that group the similar points as the same. In my case I use the following:

# 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;
}


We can use g++ -std=c++11 -I /usr/include vectorHash.cpp -o vectorHash to compile the example.

If we use an exact equal or exact hash , then we are unfortunately unable to pick up the similar points .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM