How can I detect rectangles and get their coordinates in a binary image
10 views (last 30 days)
Niklas Persson on 15 Sep 2022
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
Image Analyst on 15 Sep 2022
What I'd do first is to get the four perimeter blobs and store those, then fill in that border. Then I'd threshold for black then label the image. Then use ismember to get each blob in turn. Then I'd scan down each image until I hit the blob. Then use find() to find the first and last point in the blob and note it's width and top row. Then scan down until the width changes, which means you're now in a new rectangle so the last one had its bottom row on the prior line. I believe this should work. Give it a try. If you're still unable to do it, post the original image (with no graphics or JPG blurring) in a .PNG file.