首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >存储在OpenCV向量中的Point2f排序点

存储在OpenCV向量中的Point2f排序点
EN

Stack Overflow用户
提问于 2015-06-14 18:31:03
回答 1查看 4.8K关注 0票数 2

我试图使用minAreaRect算法对the following返回的结果进行排序:

这是我现在的代码:

代码语言:javascript
运行
复制
void sortPoints(Point2f* unsorted) {

    Point2f sorted[4];

    for (int i = 0; i < 4; i++) sorted[i] = Point(0, 0);

    float middleX = (unsorted[0].x + unsorted[1].x + unsorted[2].x + unsorted[3].x) / 4;
    float middleY = (unsorted[0].y + unsorted[1].y + unsorted[2].y + unsorted[3].y) / 4;

    for (int i = 0; i < 4; i++) {
        if (unsorted[i].x < middleX && unsorted[i].y < middleY) sorted[0] = unsorted[i];
        if (unsorted[i].x > middleX && unsorted[i].y < middleY) sorted[1] = unsorted[i];
        if (unsorted[i].x < middleX && unsorted[i].y > middleY) sorted[2] = unsorted[i];
        if (unsorted[i].x > middleX && unsorted[i].y > middleY) sorted[3] = unsorted[i];
    }

    unsorted = sorted;

}

...

vector<RotatedRect> minRect( contours.size() );

for( int i = 0; i < contours.size(); i++ ) { 
     minRect[i] = minAreaRect( Mat(contours[i]) );
}

Point2f rect_points[4]; 

for( int i = 0; i < contours.size(); i++ ) {
     minRect[i].points( rect_points );
     sortPoints( rect_points ); /* ...they are not sorted after calling sortPoints?!? */
}

但是它不起作用,没有编译错误,但是没有排序。我认为数据类型有问题。

EN

Stack Overflow用户

回答已采纳

发布于 2015-06-14 19:42:01

您提供的算法只有在4个点属于与x轴平行的矩形时才能工作。此外,您试图返回结果的方式将不能正常工作。尝试将sorted数组复制回unsorted。像这样的for ( int i=0;i<4;++i ) unsorted[i] = sorted[i];

但是有一些方法你可以用

代码语言:javascript
运行
复制
#include <algorithm>
struct str{
    bool operator() ( Point2f a, Point2f b ){
        if ( a.y != b.y ) 
            return a.y < b.y;
        return a.x <= b.x ;
    }
} comp;

int main()
{
Point2f v[4];
v[0] = Point2f(0,1);
v[1] = Point2f(-1,1);
v[2] = Point2f(2,1);
v[3] = Point2f(4,1);

sort(v,v+4,comp);
}
票数 4
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30833039

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档