算法来计算集箱通过离散轮廓边界的边界、轮廓、算法

2023-09-11 06:57:37 作者:空城只有旧梦

在离散基于网格的面(认为:图像的像素),我有一个封闭的轮廓,可以EX pressed无论是:

On a discrete grid-based plane (think: pixels of an image), I have a closed contour that can be expressed either by:

一组二维点(X1,Y1)(X2,Y2);(X3,Y3); ... 或4连弗里曼code ,有一个起点点:(X1,Y1)+ 00001112 ... a set of 2D points (x1,y1);(x2,y2);(x3,y3);... or a 4-connected Freeman code, with a starting point: (x1,y1) + 00001112...

我知道如何从一个切换到另一个这些重presentations的。这将是在输入数据

I know how to switch from one to the other of these representations. This will be the input data.

我想获得一套网格坐标是界的轮廓。 考虑这个例子,其中红的坐标的轮廓,而灰色一个起点:

I want to get the set of grid coordinates that are bounded by the contour. Consider this example, where the red coordinates are the contour, and the gray one the starting point:

如果灰色的坐标,比方说,在(0,0),那么我想一个向量控股: (1,1),(2,1),(3,1),(3,2)

If the gray coordinate is, say, at (0,0), then I want a vector holding: (1,1),(2,1),(3,1),(3,2)

顺序并不重要,并输出向量也可以容纳在轮廓本身

Order is not important, and the output vector can also hold the contour itself.

选择的语言是C ++,但我开到任何现有的code,算法,图书馆,指针,无论...

Language of choice is C++, but I'm open to any existing code, algorithm, library, pointer, whatever...

不过,我觉得,也许 CGAL 会有这样的事情,但我不熟悉它,找不到我的方式,通过手工,所以我甚至不知道。 我也看了朝 opencv的,但我认为它不提供这个算法(但我可能是错的?)。

I though that maybe CGAL would have something like this, but I am unfamiliar with it and couldn't find my way through the manual, so I'm not even sure. I also looked toward Opencv but I think it does not provide this algorithm (but I can be wrong?).

我在想找到边框,然后检查每个在矩形的点,看看他们是的内/外,但这似乎不理想。你知道吗?

I was thinking about finding the bounding rectangle, then checking each of the points in the rectangle to see if they are inside/outside, but this seems suboptimal. Any idea ?

推荐答案

要解决这个问题的方法之一是 drawContours ,你有轮廓点你。

One way to solve this is drawContours, and you have contours points with you.

创建空白垫,并绘制轮廓厚度= 1(边界)。 创建另一张空白垫,并绘制轮廓与厚度= CV_FILLED(整个地区,包括边界)。 现在, bitwise_and 上述两个(你得到的填充的区域不包括边界)。 最后检查非零像素。 Create blank Mat and draw contour with thickness = 1(boundary). Create another blank Mat and draw contour with thickness = CV_FILLED(whole area including boundary). Now bitwise_and between above two(you got filled area excluding boundary). Finally check for non-zero pixel.