Thijser
Thijser

Reputation: 2633

How do I fit rectangles to an image in python and obtain their coordinates

I'm looking for a way to split a number of images into proper rectangles. These rectangles are ideally shaped such that each of them take on the largest possible size without containing a lot of white.

So let's say that we have the following image input

I would like to get an output such as this: output

Note the overlapping rectangles, the hole and the non axis aligned rectangle, all of these are likely scenario's I have to deal with.

I'm aiming to get the coordinates describing the corner pieces of the rectangles so something like

[[(73,13),(269,13),(269,47)(73,47)],
 [(73,13),(73,210),(109,210),(109,13)]
...]

In order to do this I have already looked at the cv2.findContours but I couldn't get it to work with overlapping rectangles (though I could use the hierarchy model to deal with holes as that causes the contours to be merged into one.

Note that although not shown holes can be nested.

Upvotes: 3

Views: 1656

Answers (1)

yapws87
yapws87

Reputation: 1839

A algorithm that works roughly as follow should be able to give you the result you seek.

  1. Get all the corner points in the image.
  2. Randomly select 3 points to create a rectangle
  3. Count the ratio of yellow pixels within the rectangle, accept if the ratio satisfy a threshold.
  4. Repeat 2 to 4 until : a) every single combination of point is complete or b) all yellow pixel are accounted for or c) after n number of iteration

The difficult part of this algorithm lies in step 2, creating rectangle from 3 points.

If all the rectangles were right angle, you can simply find the minimum x and y to correspond for topLeft corner and maximum x and y to correspond for bottomRight corner of your new rectangle.

But since you have off axis rectangle, you will need to check if the two vector created from the 3 points have a 90 degree angle between them before generating the rectangle.

Upvotes: 2

Related Questions