具有cv :: Point2f类型的重载std :: sort?

时间:2018-07-30 08:50:08

标签: c++ opencv

我正在尝试对opencv点的向量进行排序,如果点的类型为cv :: Point2i,则可以成功完成。一旦这些点的类型为float cv :: Point2f,该程序就不会编译。

错误是

错误:没有匹配函数调用'__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(),resultscoordinate。 begin(),&comparePointsIntersection :: iterator> comparePointsSort>);

我有自己的Compare函数,因为默认的compare函数不支持cv :: Points(使用.x和.y而不是.first和.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;

}

我该如何使用cv :: Point2f类型?

2 个答案:

答案 0 :(得分:1)

您可以创建一个struct和一个inline bool operator进行比较,并将其传递给std::sort()

首先按x坐标对点进行排序,然后按y坐标对点进行排序的结构:

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

然后像这样对std::vector进行排序:

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

答案 1 :(得分:0)

在带有OpenCV 2.4的Visual Studio 2013上,这就像一个魅力

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