2013-06-03 245 views
0

我得到了这个问题。我在照片中找到了轮廓。大概他们和所有的东西。现在,我想按照Y轴和X轴对它们进行排序。这是可能的吗?我为此使用了std :: sort两次,并且我总是只为一个轴排序。请尽快回答。谢谢OpenCV轮廓轴排序

bool operator()(vector<Point> c1,vector<Point>c2){ 
    double a=boundingRect(Mat(c1)).y; 
    double b=boundingRect(Mat(c2)).y; 
    return a<b; 
    } 

这是Y轴的一个例子。对X轴使用相同的坐标(y => x)。

+0

我不知道你正在尝试做的(由“大约他们和所有的东西”失去的),但也许使用自定义比较喜欢在这个问题将有助于:http://stackoverflow.com/questions/13495207/opencv-c-sorting-contours-by-their-contourarea? – Bull

+0

我的老师对我说要做到这一点 - 按Y,然后按X轴排列轮廓。我会尝试你发布的链接(或者我会问我的老师)。无论如何感谢您的帮助。 – user2447036

回答

2

也许你的老师想让你用Y首先对它们进行排序,如果伊苏相同,排序X.如果是那样的话,你应该写你比较喜欢

bool operator()(const vector<Point>& c1, const vector<Point>& c2) { 
    // get bounding boxes for c1 and c2 
    cv::Rect bbox1 = boundingRect(cv::Mat(c1)); 
    cv::Rect bbox2 = boundingRect(cv::Mat(c2)); 
    // if c1's top is lower than c2's bottom, order should be c2 c1 
    if (c1.y > c2.y+c2.height-1) { 
    return false; 
    } 
    // if c2's top is lower than c1's bottom, order should be c1 c2 
    if (c2.y > c1.y+c1.height-1) { 
    return true; 
    } 
    // they have overlap in y direction, just sort them by their x 
    return c1.x < c2.x; 
} 
+0

感谢您的所有意见。也许我解释了我的问题错了。我有这张照片:[链接](http://chivethebrigade.files.wordpress.com/2011/05/text-end-920-0.jpg?w=920&h=387),我想读取轮廓不是从顶部以列为底,但从左到右依行。我的老师让我按Y轴和X轴排序。我非常感谢所有的建议。再次感谢。 – user2447036

+0

看起来您想先查找同一文本行中的所有文本,然后按x排序。例如,您希望从示例图像中获得“感谢所有人的耐心等待,我希望这是重新发布的结尾”。让我知道如果是这样的话。 – cxyzs7

+0

我认为就是这样。文本在一行,然后使用Y轴识别,其中一行文本结束并且新行开始。 – user2447036

0

这可能是最如果您有权访问C++ 11功能,则使用std::tie简洁地完成。

bool operator()(vector<Point> c1,vector<Point>c2) 
{ 
    double a=boundingRect(Mat(c1)).y; 
    double b=boundingRect(Mat(c2)).y; 
    return std::tie(a.y, a.x) < std::tie(b.y, b.x); 
} 

然后您只需要拨打std::sort一次。