定义轮廓是否闭合
我需要一种方法来定义轮廓是否代表直线或闭合形状。在Java中,我有一个对象Shape,它包含再次将其定义为单独对象的所有点。对象Point表示点的坐标。我尝试使用递归来解析形状,但对于较大的形状(超过 150 个点),性能非常差。我附上了我想要解析的形状的图片,以帮助更好地理解这个问题。
我放置一张图像是为了更好地可视化问题。
这只是显示我得到的所有形状。我只想显示两个关闭的。
提前致谢。 瓦西尔·科塞夫
I need a way to define if a contour represents a line or a closed shape. In Java, I have an object Shape which contains all the points which defines it again as separate objects. The object Point represents the coordinates of the point. I tried parsing the shapes with a recursion but for larger shapes, more then 150 points, the performance is very poor. I am attaching a picture of the shapes I want to parse to help better understand the question.
I am putting a image for better visualisation of the problem.
This is just showing all the shapes I got. I want to display just the two closed ones.
Thanks in advance.
Vassil Kossev
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
第一个想法:使用合适的轮廓跟踪算法来获得有序的轮廓。如果你的轮廓是闭合的,你最终会回到第一个点。
第二个想法:使用洪水填充算法:如果你超出了对象的边界框是打开的,否则是关闭的。
第三个想法:使用形态学。删除孤立的像素。找到所有端点和分支点。删除所有分支点。没有端点的连通分量是闭合轮廓(“圆”),有两个端点的连通分量是开放轮廓(“线”)。将没有端点的连接组件重新投影到原始图像,并仅保留与其有公共部分的连接组件。我认为这个可以实时实现,而且是最容易实现的。
First idea: Use a suitable contour tracing algorithm to get an ordered contour. If your contour is closed you will get back to the first point eventually.
Second idea: Use a flood filling algorithm: if you get out of the bounding box of your object it is open, otherwise it is closed.
Third idea: use morphology. Remove lone pixels. Find all endpoints and branchpoints. Remove all branchpoints. Connected components with no endpoints are closed contours ("circles"), connected components with two endpoints are open contours ("lines"). Re-project connected components with no endpoints to the original image and keep only connected components that have common part with them. I think this could be implemented real-time, and the easiest to implement.
如果您的轮廓线宽度为 1 像素,那么您可以计算每个点的邻居数量*。如果给定轮廓的每个点都有 2 个邻居,则轮廓是闭合的。如果有 2 个点,每个点只有 1 个邻居,则轮廓是开放的。
如果您的轮廓较粗,则可以应用骨架化算法将它们精确地减薄 1 像素。一个有趣的情况是当轮廓上有侧分支时,但在这种情况下必须有具有 3 个邻居的分支点,因此可以轻松处理类似的情况。
* 计算邻居很简单:使用原始图像!随机选择轮廓上的一个点,检查邻近的 8 个像素,并计算属于轮廓的部分。然后重复这些邻域检查,依此类推,直到轮廓的所有像素都已检查完毕。
If you have contour lines of 1 pixel width, then you can count the number of neighbors for each point *. If every point of a given contour has 2 neighbors, then the contour is closed. If there are 2 points with only 1 neighbor each, then the contour is open.
If your contours are thicker, then you can apply a skeletonization algorithm to make them exactly 1 pixel thin. An interesting case is when you have side branches on a contour, but in this case there must be branching points with 3 neighbors, so similar situations can be handled easily.
* Counting neighbors is easy: use the original image! Choose one point of the contour randomly, check the neighboring 8 pixels, and count those which are part of the contour. Then repeat the neighbor-checking for these, and so on, until all pixels of the contour have been checked.
如果点按顺序存储,您也许可以比较第一个点和最后一个点。如果它们在空间上相等,则轮廓是闭合的。
If the points are stored in order, you might be able to compare the first and last points. If they're spatially equal, the contour is closed.