## How to detect the shape in matlab?

### saravanakumar D (view profile)

on 27 Dec 2013
Latest activity Commented on by Image Analyst

on 26 Dec 2016

### Image Analyst (view profile)

I can't understand the technique how to analyse the shape. So any please help me to understand this concept.

Code is below
figure,
imshow(RGB),
title('Original Image');
GRAY = rgb2gray(RGB);
figure,
imshow(GRAY),
title('Gray Image');
threshold = graythresh(GRAY);
BW = im2bw(GRAY, threshold);
figure,
imshow(BW),
title('Binary Image');
BW = ~ BW;
figure,
imshow(BW),
title('Inverted Binary Image');
[B,L] = bwboundaries(BW, 'noholes');
STATS = regionprops(L, 'all'); % we need 'BoundingBox' and 'Extent'
% Step 7: Classify Shapes according to properties
% Square = 3 = (1 + 2) = (X=Y + Extent = 1)
% Rectangular = 2 = (0 + 2) = (only Extent = 1)
% Circle = 1 = (1 + 0) = (X=Y , Extent < 1)
% UNKNOWN = 0
figure,
imshow(RGB),
title('Results');
hold on
for i = 1 : length(STATS)
W(i) = uint8(abs(STATS(i).BoundingBox(3)-STATS(i).BoundingBox(4)) < 0.1);
W(i) = W(i) + 2 * uint8((STATS(i).Extent - 1) == 0 );
centroid = STATS(i).Centroid;
switch W(i)
case 1
plot(centroid(1),centroid(2),'wO');
case 2
plot(centroid(1),centroid(2),'wX');
case 3
plot(centroid(1),centroid(2),'wS');
end
end
return

### Image Analyst (view profile)

on 27 Dec 2013

What are the kinds of shapes you have there?

1. polygons (everything is a polygon)
3. quadrilaterals, rectangles, polygons, and ellipsoids
4. quadrilaterals, rectangles, polygons, circles, and ellipsoids
5. quadrilaterals, rectangles, squares, polygons, circles, and ellipsoids

You might look at the solidity, area, and perimeter. And the circularity = perimeter.^2 ./ (4*pi*area).

You may also find this useful to determine how many sides a polygon has: http://matlab.wikia.com/wiki/FAQ#How_do_I_find_.22kinks.22_in_a_curve.3F

saravanakumar D

### saravanakumar D (view profile)

on 27 Dec 2013

i want to know why programmer using boundingbox in this programme. what is meaning of BoundingBox(3)and BoundingBox(4)

hold on for i = 1 : length(STATS) W(i) = uint8(abs(STATS(i).BoundingBox(3)-STATS(i).BoundingBox(4)) < 0.1); W(i) = W(i) + 2 * uint8((STATS(i).Extent - 1) == 0 ); centroid = STATS(i).Centroid; switch W(i) case 1 plot(centroid(1),centroid(2),'wO'); case 2 plot(centroid(1),centroid(2),'wX'); case 3 plot(centroid(1),centroid(2),'wS'); end

Marc

### Marc (view profile)

on 27 Dec 2013

Boundingbox is a property from regionprops. It's being stored in the structure STATS.

doc regionprops

I assume you have the image processing toolbox?

Image Analyst

### Image Analyst (view profile)

on 27 Dec 2013

bounding box won't help unless one of the shape classes bounding box size is known in advance and specified as a characteristic of that shape. In other words "if the width of the bounding box is this and the height of the bounding box is that, then the shape must be this (circle or whatever)."

### sss (view profile)

on 26 Dec 2016
Edited by Image Analyst

### Image Analyst (view profile)

on 26 Dec 2016

what is the meaning of this for loop? -----

for i = 1 : length(STATS)
W(i) = uint8(abs(STATS(i).BoundingBox(3)-STATS(i).BoundingBox(4)) < 0.1);
W(i) = W(i) + 2 * uint8((STATS(i).Extent - 1) == 0 );
centroid = STATS(i).Centroid;
switch W(i)
case 1
plot(centroid(1),centroid(2),'wO');
case 2
plot(centroid(1),centroid(2),'wX');
case 3
plot(centroid(1),centroid(2),'wS');
end

Image Analyst

### Image Analyst (view profile)

on 26 Dec 2016

It plots w0, wx, or xS at the centroid of blobs in a binary image. If the blob is roughly square it puts a wS at the centroid. If it's a rectangle it will put up wX. Otherwise it will put up w0 for arbitrarily-shaped blobs that fit in a bounding box that is roughly square. I don't see anything being put up for arbitrarily-shaped blobs that have a rectangular bounding box.