I have an image with size of M*N. Each pixel can be selected or not selected.
Given selection values for each pixel (selected or not), what is the most efficient algorithm to get the set of polygons which represents a selection?
Diagonals might be tricky, because of pixel-exact rounding errors, so I would go only for rectangles with horizontal and vertical lines (overlapping ones can be merges to a polygon)
I would perform the following steps:
connect selected neighbours of selected pixels to horizontal lines
connect horizontal lines to rectangles
connect overlapping/touching rectangles to a polygon
I think any filling algorithm can be suitable:
1, 0values1selected,0Non selected? or just the corner points, or the bordering Lines?1, 0values for each pixelcv2.findContoursto find contours, 2.cv2.approxPolyDPto approximate them into polygons, see this tutorial for more details.