简体   繁体   中英

overload std::sort with cv::Point2f type?

I am trying to sort a vector of opencv points and I could do it successfully if the points are of type cv::Point2i. As soon as the points are of type float cv::Point2f, the program does not compile.

The error is

error: no matching function for call to '__set_intersection_points'sort(std::vector >::iterator, stdcv::vectorPoint_ >::iterator, std::vector >::iterator, std::vector >::iterator, stdcv::vectorPoint_ > >::iterator, )' __set_intersection_points std::iterator>(coordinates1.begin(), coordinates1.endsort(), coordinates2frame_stencil_displacement.begin(), coordinates2frame_stencil_displacement.end(), results_coordinates.begin(), &comparePointsIntersection::iterator>comparePointsSort>);

I have my own Compare function, since the default compare function does not support cv::Points ( dereferencing using .x and .y instead of .first and .second )

template <typename T>
bool comparePointsSort(T lhs, T rhs){
    if ( lhs.x == rhs.x )
        return (lhs.y < rhs.y);
    else
        return (lhs.x < rhs.x) ;
}


void points_mine() {

    std::vector<cv::Point2i> coordinates1(MAX_INDEX), coordinates2(MAX_INDEX);

    coordinates1 = {{1, 2}, {9, 10}, {5, 6}, {7, 8}, {5, 4}};
    coordinates2 = {{1, 2}, {0, 0}, {5, 4}, {7, 8}, {9, 10}};

    std::sort(coordinates1.begin(), coordinates1.end(), comparePointsSort<cv::Point2i>);
    std::sort(coordinates2.begin(), coordinates2.end(), comparePointsSort<cv::Point2i>);

    for ( ushort index = 0; index < MAX_INDEX; index++ ) {
        std::cout << "co1 " << coordinates1.at(index).x << " " << coordinates1.at(index).y << std::endl;
    }

    for ( ushort index = 0; index < MAX_INDEX; index++ ) {
        std::cout << "co2 " << coordinates2.at(index).x << " " << coordinates2.at(index).y << std::endl;
    }

    std::vector<cv::Point2i> results_coordinates(10);

    __set_intersection_points<std::vector<cv::Point2i>::iterator>(coordinates1.begin(), coordinates1.end(), coordinates2.begin(), coordinates2.end(), results_coordinates.begin(), &comparePointsIntersection<std::vector<cv::Point2i>::iterator>);
    std::cout << "begin intersection" << std::endl;
    for ( const auto index : results_coordinates )  {
        std::cout << index.x << " " << index.y << std::endl;
    }

}


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

    points_mine();
    std::cout << "-------------------------------" << std::endl;

}

How can I get this working with cv::Point2f type?

You can create a struct with an inline bool operator used for comparing, the pass it to the std::sort() .

The struct which will sort points first by x coordinate and then by y coordinate:

struct pointSort
{
    inline bool operator() (const Point2f& struct1, const Point2f& struct2)
    {
        if(struct1.x == struct2.x)
            return (struct1.y < struct2.y);
        else 
           return struct1.x < struct2.x
    }
};

Then sort a std::vector like this:

vector<Point2f> points;
// Add the values to the vector here
std::sort(points.begin(), points.end(), pointSort());

This works like a charm on Visual Studio 2013 with OpenCV 2.4

#define MAX_INDEX 5

void points_mine() 
{

    std::vector<cv::Point2f> coordinates1(MAX_INDEX), coordinates2(MAX_INDEX);

    coordinates1 = { { 1, 2 }, { 9, 10 }, { 5, 6 }, { 7, 8 }, { 5, 4 } };
    coordinates2 = { { 1, 2 }, { 0, 0 }, { 5, 4 }, { 7, 8 }, { 9, 10 } };

    std::sort(coordinates1.begin(), coordinates1.end(),    comparePointsSort<cv::Point2f>);
    std::sort(coordinates2.begin(), coordinates2.end(),    comparePointsSort<cv::Point2f>);

    for (ushort index = 0; index < MAX_INDEX; index++) {
        std::cout << "co1 " << coordinates1.at(index).x << " " << coordinates1.at(index).y << std::endl;
    }

    for (ushort index = 0; index < MAX_INDEX; index++) {
        std::cout << "co2 " << coordinates2.at(index).x << " " << coordinates2.at(index).y << std::endl;
    }

}


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

    points_mine();
    std::cout << "-------------------------------" << std::endl;

    std::cout << "finished" << std::endl;
    std::cin.get();
}

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