How can I detect rectangles and get their coordinates in a binary image
Show older comments
Hi,
I'm looking for a way to extract the rectangles of a binary image and get the coordinates of the corners of the rectangles. The location and size of the rectangles can vary, but it will always be black rectangles on a white background. Also, the black border at the edge of the image will be made up of 4 rectangles, see the image below (the numbers are just there to give you an idea of the numbers of rectangles).

I have tried to use the detectHarrisFeatures function, but the problem is that I only get the coordinates of the rectangles, thus I don't know which coordinates makes up a rectangle.
I have also tried to use the bwconncomp function, as I was suggested in this post. It works, but it is slow for larger images and it is possible to end up with a lot of small row rectangles or a lot of small column rectangles. For example the top border can be seen as 2 connected rows, or 120 connected columns.
So I'm looking for a more efficient way to detect the rectangles and retrive the corner coordinates for each.
I have attached two example image in the question.
Any help is highly appriciated
4 Comments
Matt J
on 15 Sep 2022
Your problem is a bit ill-defined, because there potentially several non-unique ways that the black regions you've shown can be partitioned into rectangles. Or is it really just the corners you are after?
Niklas Persson
on 15 Sep 2022
Even if you could get the fewest number of rectangles, you still have ambiguities. Below is an example showing a shape that can be partitioned into the minimum number of rectangles (2) in two different ways. Does your application care which partition is chosen?

Niklas Persson
on 15 Sep 2022
Accepted Answer
More Answers (0)
Categories
Find more on Display Image in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!