Detect all the rectangles in image

Hey all,
in the following image, all the rectangles are a little bit deformed due to power leakage effect. I want to detect all the rectangles and obtain the positions of the rectangles.
Is there a way to detect all rectangles?
Thank you

 Accepted Answer

Image Analyst
Image Analyst on 18 Feb 2019
It's trivial. Just use regionprops(). See my Image Segmentation Tutorial

9 Comments

simon xu
simon xu on 19 Feb 2019
Edited: simon xu on 19 Feb 2019
Thank Image Analyst very much!
After some trivial, I found using regionprops() is straightforward and helpful to detect the centroids of the rectangles. If I also want to obtain the corners (or boundaries) of each rectangle, is there a easy way to detect?
I found the property 'BoundingBox' of regionprops() is helpful to detect the boundaries of rectangles.
Thanks a lot !
regionprops() gives you [xLeft, yTop, width, height]. From that you can get row1 (y1), row2 (y2), col1 (x1), and col2 (x2), as well as the centroid (xLeft + width/2, yTop + height/2).
You can also get a lot of other things from regionprops().
Thanks.
I'm wondering whether there is a useful method to remove the 'tails' (caused by power leakage effect) of the rectangles, illustrated in the following image
D7l9W.jpg
A naive way may be graythresh(), but it may be not satisfying, the following image is a binary image after thresholding
BinImg.jpeg
As a result, the rectangles which we detected may be not perfect, as shown below
result.PNG
So, if there is a more powerful method (more powerful than graythresh()) to remove these 'tails' of the rectangles, the detected shape would be more satisfying.
Is there a more powerful method?
Thank Image Analyst very much again!
Thanks is accepting the answer.
You can try imopen().
The following image is the original image
imopen() is helpful to smooth the boundaries and get rid of the 'tails' of these rectangles, as shown below. The following image is the image after imopen()
There are no notable 'tails', and then I use imadjust() to adjust the contrast, and then I use graythresh() and im2bw() to binarize the image and obtain the following binary image.
The top-right rectangle is missing after graythresh(). So, is there a better way of thresholding and binarizing to aviod missing any rectangle?
Thanks a lot!
% here is my code
MainImg = imread('originalImg.jpeg');
se = strel('rectangle',[4,5]);
openImg = imopen(MainImg,se);
enhenImg=imadjust(openImg);
BinImg = im2bw(enhenImg,graythresh(enhenImg));
How about you just compute the centroid, even with the tails, then take a vertical and horizontal line through the centroid and use find() to find the top and bottom lines and left and right columns of the rectangle?
How to binarize the grayscale image in the attached grayscale.mat file?
My goal is binarizing the grayscale image to get the rectangles and get rid of noise.
Thank you very much!

Sign in to comment.

More Answers (1)

I = imread('myImage.jpeg') ;
[y,x] = find(I>50) ;
imshow(I)
hold on
idx = kmeans([x y],4) ;
for i = 1:4
plot(x(idx==i),y(idx==i),'.')
% GEt L and B of rectangles
x0 = min(x(idx==i)) ; x1 = max(x(idx==i)) ;
y0 = min(y(idx==i)) ; y1 = max(y(idx==i)) ;
%
L = x1-x0 ;
B = y1-y0 ;
coor = [x0 y0 ; x0 y1 ; x1 y1 ; x1 y0] ;
patch(coor(:,1),coor(:,2),rand(1,3))
end

3 Comments

Thank KSSV very much!
But if the number of rectangles are arbitrary (maybe unknown), how to detect?
can you please explain in brief

Sign in to comment.

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!