繁体   English   中英

用cv :: Point2f类型重载std :: sort?

[英]overload std::sort with cv::Point2f type?

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

错误是

错误:没有匹配函数调用'__set_intersection_points'sort(std :: vector> :: iterator,stdcv :: vectorPoint_> :: iterator,std :: vector> :: iterator,std :: vector> :: iterator,stdcv > :迭代> 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类型呢?

您可以使用用于比较的inline bool operator创建struct ,然后将其传递给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());

在带有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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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