Finding closed polygon?

A bit of an abstract question, but how do I go from this , which is a bunch of lines i.e. generated by cv2.createLineSegmentDetector

to this, a filled shape where a closed polygon was detected

your problem is probably easier than this. the abstraction and “processing” complicates it. please start telling it from the beginning.

as it stands, that looks like computational geometry.

I have this image and i want to ‘extract’ the large hexagon shape

In the actual usage scenario, the hexagon will be placed against a simpler background , such as a blank wall or the sky

does the hexagon always appear an even light matte surrounded by a dark bevel?

then I would suggest thresholding and contour finding. the hexagon should be one of the largest contours by area, and it’s compact (judged against the area of its convex hull), and it has six corners (approxPolyDP result is a six-point contour).

that gives you a contour and/or mask for the hexagon.

I’ll assume you don’t need the exact corners of the hexagon. both approxPolyDP and approxPolyN have their downsides.

the image has been digitally sharpened one or two times. it would be better if that weren’t the case.

a close-up of the leftmost inner corner, with a sobel map. what should have been a clean edge there has what we call “ringing”.

1 Like

yes i forgot to turn off the sharpening when i took the picture, it was just for a test

the reason I am interested in LineSegmentDetector is because it might be more robust in certain situations, like uneven lighting conditions

ideally there is something similar to the ‘bucket fill tool’ in Paint etc, where it fills an area of the image bounded by some defined constraints, such as a sharp transition in colour (such as in Paint) or the presence of a line

well if you have a bunch of lines, finding the intersection points of all of them is a geometry problem with known algorithms. the simplest (testing all of them against each other) quickly becomes very costly. others (sweep line algorithms) have much better time complexity.

How expensive is the Flood Fill feature in OpenCV? I am thinking of using it after Line Segment Detector has made an image of the lines it has detected in the image

cheap. but then you’ll still have to find a point to start the floodfill from. it’s not all that useful to think in terms of “paint” operations.

One thing I forgot to mention is that the hexagon is always going to be roughly in the center

If not I will use the OpenCV mouse callback to manually select the start point