0

I have an image with size of M*N. Each pixel can be selected or not selected.

image selection

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?

enter image description here

5
  • If your example the selection mask contains only connected lines that are already polygons. What exactly is the question? Commented Apr 9, 2021 at 12:55
  • Edited the question. The selection mask contains selection value for each pixel, not lines or polygons. Commented Apr 9, 2021 at 13:01
  • @razor3x "Given selection values for each pixel (selected or not)" you mean that you will provide MxN Binary Array with 1, 0 values 1 selected, 0 Non selected? or just the corner points, or the bordering Lines? Commented Apr 9, 2021 at 14:58
  • @Bilal It is MxN binary array with 1, 0 values for each pixel Commented Apr 9, 2021 at 17:31
  • @razor3x you need two simple steps: 1. cv2.findContours to find contours, 2. cv2.approxPolyDP to approximate them into polygons, see this tutorial for more details. Commented Apr 10, 2021 at 4:09

2 Answers 2

1

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:

  1. connect selected neighbours of selected pixels to horizontal lines

  2. connect horizontal lines to rectangles

  3. connect overlapping/touching rectangles to a polygon

Sign up to request clarification or add additional context in comments.

Comments

1

I think any filling algorithm can be suitable:

  • get a selected pixel not already used
  • start from it and fill the surface defined by all its connected pixels
  • this will define a polygon (just retain horizontal/vertical extremas)
  • restart with any selected pixel already not considered to get another polygon

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.