Determine if the circuit is closed or not.

I need a way to determine if the outline represents a line or a closed shape. In Java, I have a Shape object that contains all the points that again define it as separate objects. A Point object represents the coordinates of a point. I tried to parse shapes with recursion, but for larger shapes, over 150 points, performance is very poor. I am attaching an image of the figures that I want to analyze in order to better understand the issue.

I put an image to better visualize the problem.

enter image description here

It just shows all the forms I received. I want to show only two closed ones.

Thanks in advance. Vasily Kosev

+6
source share
3 answers

If you have 1 pixel wide contour lines, then you can calculate the number of neighbors for each point * . If each point of a given circuit has 2 neighborhoods, then the circuit is closed. If there are 2 points with only one neighbor, then the circuit is open.

If your contours are thicker, you can apply the skeletonization algorithm to make them exactly 1 pixel. An interesting case is when there are side branches on the contour, but in this case there should be branch points with 3 neighbors, so these situations can be easily handled.

* Counting neighbors is simple: use the original image! Select one contour point randomly, check the adjacent 8 pixels and count those parts that are part of the contour. Then repeat the neighbor check for them, etc., until all the pixels in the path are checked.

+2
source

First idea: use a suitable path trace algorithm to get an ordered path. If your circuit is closed, you will eventually return to the first item.

Second idea: use the flood filling algorithm : if you go beyond the bounding box of your object, it is open, otherwise it is closed.

Third idea: use morphology. Remove single pixels. Find all endpoints and branch points. Remove all branch points. Connected components without endpoints are closed loops ("circles"), connected components with two endpoints are open loops ("lines"). Redesign the connected components without endpoints to the original image and save only the connected components that have a common part with them. I think this can be implemented in real time and the easiest way to implement it.

+7
source

If the points are stored in order, you can compare the first and last points. If they are spatially equal, the circuit is closed.

+1
source

Source: https://habr.com/ru/post/903692/


All Articles