MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply TodayTo resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Asked by Abdul on 29 Dec 2011

http://i1108.photobucket.com/albums/h404/abdulhadi3/3617ca70.png i want to detect square shape only in image. how can i do this using matlab?

*No products are associated with this question.*

Answer by Chandra Kurniawan on 29 Dec 2011

Accepted answer

Hello,

Is your goal only to detect square in this picture?

If so, you don't need to do shape recognition, etc.

You just need to perform some morphologichal operations only.

I = imread('3617ca70.png'); Ibw = ~im2bw(I,graythresh(I));

Ifill = imfill(Ibw,'holes'); Iarea = bwareaopen(Ifill,100); Ifinal = bwlabel(Iarea);

stat = regionprops(Ifinal,'boundingbox'); imshow(I); hold on; for cnt = 1 : numel(stat) bb = stat(cnt).BoundingBox; rectangle('position',bb,'edgecolor','r','linewidth',2); end

And the result :

Is this what you need?

Answer by Sean de Wolski on 29 Dec 2011

Those squares are very well defined.

- Set a less than threshold to make the dark colors true and everything else false. (
`BW = I<thresh`) - Perform a connected components analysis (
`doc bwconncomp`) - Then
`regionprops`with*EulerNumber*specified. - Apply a threshold to the regions matching the
*EulerNumber*(`idx = [stats(:).EulerNumber] == x`) of a sample square. - Build a new matrix of false. (
`BWnew = false(CC.ImageSize)`) - Set the indices in the connected components analysis corresponding to the matching
*EulerNumber*to true. (`BWnew(vertcat(CC.PixelIdxList{idx})) = true`)

In theory you'll have a mask of the boxes.

Answer by Naz on 29 Dec 2011

Edited by John Kelly on 14 Nov 2013

I just thought of an alternative idea. Instead of detecting a square, you should register the image (filled form) of interest with the raw image. Then, since you know the coordinates of each square, you can just check the pixels at those coordinates for the white/black values.

## 0 Comments