cv::findContours 返回的轮廓是否具有一致的方向? [英] Do contours returned by cv::findContours have a consistent orientation?

查看:83
本文介绍了cv::findContours 返回的轮廓是否具有一致的方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 OpenCV 的 cv::findContours 函数来提取二进制图像中的轮廓,特别是,我正在提取轮廓的层次结构(使用 CV_RETR_CCOMP 标志).在我进一步处理这些轮廓的某个时刻,我需要依赖这些轮廓的一致顶点方向(即逆时针与顺时针).

I am using OpenCV's cv::findContours function to extract contours in a binary image, in particular, I'm extracting a hierarchy of contours (using the CV_RETR_CCOMP flag). At some point in my further processing of those contours I need to rely on a consistent vertex orientation of these contours (i.e. counter-clockwise vs. clockwise).

当然,我可以自己使用轮廓区域的符号来确定方向(由 cv::contourArea(..., true)),但我想知道这是否有必要(除此之外,它甚至不起作用对于面积为 0 的轮廓,即源图像中的细线)或者 cv::findContours 已经保证生成的轮廓的方向一致.我确实检查了一些生成的轮廓,cv::contourArea 确实似乎 返回外轮廓的负值和内轮廓的正值.但是,我在 OpenCV 文档中找不到任何实际的保证.

Of course I can just determine that orientation myself using the sign of the contour's area (as computed by cv::contourArea(..., true)), but I wonder if that is even necessary (besides that, it won't even work for contours with an area of 0, i.e. thin lines in the source image) or if cv::findContours already guarantees a consistent orientation for the generated contours. I did check a few of the generated contours and cv::contourArea does indeed seem to return negative values for outer contours and positive values for inner contours. However, I couldn't find any actual guarantee to this effect in the OpenCV documentation.

那么,是否特别保证cv::findContours 返回的轮廓始终具有一致的方向?这是否在任何地方都有记录?或者它是否因版本而异(我的版本是 2.4.5)?实际关于文档中引用的算法的论文已经对此说些什么了?或者,也许对 OpenCV 的实际实现有更深入了解的人可以比接口文档更详细地说明这一点?

So, is it specifically guaranteed that the contours returned by cv::findContours always have a consistent orientation? Is this documented anywhere? Or does it vary by version (mine is 2.4.5 for that matter)? Does the actual paper on the algorithm referenced in the documentation already say something about this? Or maybe someone with a little more insight into the actual implementation of OpenCV can say a little more about this than the interface documentation can?

推荐答案

cv:findContours 返回的轮廓应该具有一致的方向.外轮廓应逆时针方向,内轮廓应顺时针方向.这直接来自 Suzuki 和 Abe 的论文的附录 1 中描述的算法.

The contours returned from cv:findContours should have a consistent orientation. Outer contours should be oriented counter-clockwise, inner contours clockwise. This follows directly from the algorithm described in Appendix 1 of Suzuki's and Abe's paper.

图像从左上角到右下角逐行扫描.当找到属于边界的像素时,边界之后按逆时针顺序查看第一个像素的邻居(参见算法中的步骤 3.3),直到找到非背景像素.将其添加到轮廓中,然后从该像素继续搜索.

The image is scanned line by line from top left to bottom right. When a pixel belonging to a border is found, the border is followed by looking at the neighbours of the first pixel in counter-clockwise order (see step 3.3 in the algorithm), until a non-background pixel is found. This is added to the contour and the search continues from this pixel.

重要的是,在第一次迭代中,首先查看的邻居取决于它是内边界还是外边界.在外边界的情况下,首先访问右手边的邻居;在内部边界的情况下,它是左边的邻居.在下一个搜索步骤中,搜索从访问的最后一个像素开始.

The important thing is that in the first iteration the neighbour which is first looked at depends on whether it is an inner or an outer border. In case of an outer border the right-hand neighbour is visited first; in case of an inner border it is the left-hand neighbour. In the next search step the search starts from the last pixel visited.

由于从左上角到右下角进行扫描,因此在检测到外边界时,可以确保左侧和边界像素顶部的所有相邻像素都是背景像素.与内边框正好相反,所有左侧和顶部的邻居都是非背景像素.

Due to the scanning happing from top left to bottom right, on detection of an outer border it is assured that all neighbouring pixels to the left and the top of the border pixel are background pixels. With inner border, it is exactly the opposite, all neighbours to the left and the top are non-background pixels.

结合访问相邻像素的不同起始位置,这会导致轮廓的可预测方向.

In combination with the different starting positions for visiting the neighbouring pixels this results in predictable orientations of the contours.

该算法在icvFetchContour 函数,由 cv:findContour 内部使用.从那里可以清楚地看出,像素是按照它们被访问的顺序添加到轮廓多边形中的.

This algorithm is implemented in the icvFetchContour function which is used internally by cv:findContour. From there it is clear that the pixel are added to the contour polygon in the order in which they are visited.

作为 cv::findContours 的文档 特别说明他们实现了 Suzuki 等人的算法.在这篇论文中,访问像素的方向和顺序是明确定义的,我认为可以假设方向是有保证的.

As the documentation for cv::findContours specifically says that they implemented the algorithm by Suzuki et al. and as in this paper the direction and order for visiting the pixels is explicitely defined, I think one can assume that the orientation is kind of guaranteed.

这篇关于cv::findContours 返回的轮廓是否具有一致的方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆