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
Just use findContours() in your image, then decide whether the contour is closed or not by examining the hierarchy passed to the findContours() function.
Characteristics of Contour Lines: All points on a contour line have the same elevation. Contour lines do not cross or divide. Contour lines are always closed or endless lines.
A series of concentric contour lines that are closed onto themselves indicate mountains or hills. Closed contour lines look like misshapen circles or loops. The smallest circle shows the peak. Depression contours show where the land goes into a depression or an indentation on the Earth.
An open contour or “path” is whenever a shape does not connect all the way around. Open contours can be easy to miss and frequently cause issues while designing in vector programs, such as Adobe Illustrator.
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.
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 you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With