View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Shape Recognition

3.9 | 42 ratings Rate this file 119 Downloads (last 30 days) File Size: 3.63 KB File ID: #15491 Version: 1.0
image thumbnail

Shape Recognition


Ahmed Samieh (view profile)


03 Jul 2007 (Updated )

differentiate Square, Rectangular, Circle from others

| Watch this File

File Information

The program should recognize objects like circles, rectangles, and squares from the input image.
This is a shapes classifier based on the properties of each shape, like roundness, ratio of dimensions, centroid,?etc

In this classifier we will recognize only shapes like circles, rectangles, and squares from the input image.
So, we will concentrate on the steps we will follow to recognize those shapes from any input image.

We have seven steps:
1 - Read the RGB (colored) image in from user.
2 - Convert image from (RGB) colored to gray image.
3 - Threshold the image (convert gray image to binary image).
4 - Invert the binary image (in order to speed up the time of processing).
5 - Find the boundaries concentrate.
6 - Determine shapes properties (ratio of dimensions, roundness).
7 - Classify shapes according to its properties.

RGB image have the shapes to recognize.

The RGB image with shapes recognized and labeled.

Required Products Image Processing Toolbox
MATLAB release MATLAB 7.0.1 (R14SP1)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (62)
02 Feb 2017 KARTHI KEYAN

It's not working
this code provides the following error
Error in shape_class (line 66)
x1 = Mx(2,1); %each corner has an x and y

26 Dec 2016 sss

sss (view profile)

I have a question
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
case 2
case 3

thank you.

Comment only
21 Nov 2016 akram adnane

05 Nov 2016 OGpl

OGpl (view profile)

20 Oct 2016 Sadoon U

good work man

08 May 2016 Lubna Shah

02 Mar 2016 Ali Sami

I shared these new codes splendor but does not have a possible triangle Code Add On Triangle

24 Dec 2015 Kaajal Narayan

I have applied the following processes as above. However I'm struggling to automatically loop or read in the individual images after the shape classifier process, without manually importing the images.

Comment only
19 Jul 2015 truong nhat quang

17 Sep 2014 rahul

rahul (view profile)

07 Jul 2014 ghufran

04 Jul 2014 Francisco Angel

12 Jun 2014 huynh an

19 May 2014 duc

duc (view profile)

17 May 2014 long phan

03 Mar 2014 Shoumy

Shoumy (view profile)

I also would like to know how to detect oval shapes. May I get the codes for it? Thank you.

Comment only
11 Feb 2014 Samar Patel

i want to detect oval shape from the input image can i get code for it

29 Dec 2013 Nichol

Nichol (view profile)

It seems to work best with perfect shapes (i.e. it doesn't detect photos of hand-drawn shapes very well) but it set me on the right track, thank you so much!

21 Dec 2013 Vishal Raveendranathan

28 Jul 2013 Ooi

Ooi (view profile)

This is the function file, not the script, if want to run like a script, just delete the first line and the last line

Comment only
28 Jul 2013 vishal

vishal (view profile)

i ma new to matlab so someone plz tell me i m having an error in
function W = Classify(<ImageFile>)
when i am replacing imagefile with my imagefile name an error is coming
??? Error: File: Classify.m Line: 22 Column: 23
Unexpected MATLAB expression.
so anyone can tell me the solution??

Comment only
22 May 2013 asd

asd (view profile)

Can someone explain these part of code:
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 );
Thanks in advance

Comment only
06 Apr 2013 Silviani

Its great!! But how if we put this classification using NN?

17 Feb 2012 rock karo

i ma new user of matlab ....sir plzz provide me tha algorithm for detecting square ,rectangle,circle and alphabets in matlab.....plzz

16 Feb 2012 krupa

krupa (view profile)

We want to use this code of urs for recognizing black colored symbol at the left side of the Indian currency. So we cropped that part of the currency and used ur code on those images. We were able to recognize square(50 rs) n rectangle(20 rs) but we are not able to recognize the shape of 'circle' from 500 rs note. So can u help us out by telling us wat changes we need to do to ur code so dat it works for us. Or if u hav some other code which can work for us. Also we want to recognize triangle(100 rs) n diamond(1000 rs). can u provide the code for that also. for ur ref of the kind of shapes we want to recognize, here the site-

19 Jan 2012 ahmed ismail

15 Jan 2012 omar Alhmouz

Hi, nice work Ahamd, is triangle shapes included in the code?

19 Dec 2011 am

am (view profile)

Thank you so much.

03 Aug 2011 Jason

Jason (view profile)

it‘s very good.

31 Jan 2011 Atif Anwer

helpful for starters on image processing in matlab. Great work!

10 Oct 2010 mohammad salah

this response is for "yar131 Iaroslav". you type:

RGB = imread('test.bmp')

Comment only
06 Sep 2010 peter veres

Ok, I am sorry for the first comment... It works for still images, does anybody knows how to take snapshot over web camera? Of course and use with this program?

Comment only
20 Aug 2010 peter veres

Problems on starting problam , and no help after questioning...

20 Aug 2010 Surya Rathore

could you please tell me how to write 1st line of the program.that means how to pass sample,training,group of the image file. it will be better if you explain it through an example.

Comment only
12 Jul 2010 yar131 Iaroslav

Please help me. I am beginner at Matlab. How can I start this programm. I have the following error.

??? Input argument "test" is undefined.

Error in ==> Classify at 24
RGB = imread(test);

Comment only
06 May 2010 abhi m

abhi m (view profile)

I should recognise circle square rectangle but am struck... the code below is effecient in recognising circle but it is failing to recognise square and recatngle.. plz help me in improvising my code plz....

rgbImage = imread('test.jpg');
[rows columns numberOfColorBands] = size(rgbImage);
subplot(3, 2, 1);
imshow(rgbImage, []);
title('Original color Image');
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.


%---------------------------------------------------Convert rgb image to grayscale-----------------------------------------

grayImage = rgb2gray(rgbImage);
subplot(3, 2, 2);
imshow(grayImage, []);
title('Converted to gray scale');


%------------------------------------------------------Detection of edge by canny-------------------------------------------------------

cannyImage = edge(grayImage,'canny');
subplot(3, 2, 3);
imshow(cannyImage, []);
title('Canny Edge Image');



windowSize = 5;
halfWidth = floor(windowSize/2);
dilatedImage = zeros(rows, columns);
for col = (halfWidth+1) : columns - halfWidth
x1 = col - halfWidth;
x2= col + halfWidth;
for row = (halfWidth+1) : rows - halfWidth
y1 = row - halfWidth;
y2 = row + halfWidth;
dilatedImage(row, col) = max(max(cannyImage(y1:y2, x1:x2)));

%Displaying the dilated image

subplot(3, 2, 4);
imshow(dilatedImage, []);
caption = sprintf('Dilated with a window size of %d',windowSize);



windowSize = 5;
halfWidth = floor(windowSize/2);
erodedImage = zeros(rows, columns);
for col = (halfWidth+1) : columns - halfWidth
x1 = col - halfWidth;
x2= col + halfWidth;
for row = (halfWidth+1) : rows - halfWidth
y1 = row - halfWidth;
y2 = row + halfWidth;
erodedImage(row, col) = min(min(dilatedImage(y1:y2, x1:x2)));

%Displaying eroded image

subplot(3, 2, 5);
imshow(erodedImage, []);
caption = sprintf('Eroded image with a window size of %d',windowSize);
[B,L] = bwboundaries(grayImage, 'noholes');

STATS = regionprops(L, 'all');

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
case 2
case 3

23 Jan 2010 LIM CZE SIANG

How to mark outer circle such as square box or outer line for those shape detected?
Can the triangle formula be posted up?

Comment only
11 Jan 2010 Gül Begüm Semis

this is a really great example.
I am working on an image processing / shape recognition project right now. the aim is to group some objects on conveyor where the object shapes are square,circle and rectangle.
i have 2 questions . the first one is that how can i write the code for a tringle recognition.
the second question is i am going to be doing the image processing from webcam shots. the code only workd with bmp files. how can i reduce noise in the shots ?

thank you very much

Comment only
16 Nov 2009 Alireza Saberi


Comment only
03 Nov 2009 vsrin2 srin

Good program. I would like to output the black and white images (intermediate step) to a data file showing only 0 and 1's. how can i do it?

Comment only
22 Jul 2009 Jun wan

15 May 2009 long yi


Comment only
23 Jun 2008 Christian Stewart

Its very useful for me. Can you tell me how Triangle can be recognized in similar way??

14 May 2008 bharathi v

nice to see this one .thank you

Comment only
29 Apr 2008 chen cuicui

09 Apr 2008 Nutthasit Laosuwan


31 Mar 2008 Rocky Hurray

06 Feb 2008 aymen sellaouti

Think you very much for your contribution

31 Jan 2008 Aamir langah

thank you very much for your contribution which is helpful for the most of students of image processing field


Hi,I have interest to view this file.Thankyou.

Comment only
30 Nov 2007 kevin scott basinio

its good

02 Nov 2007 Hrushikesh Kulkarni

extremely good job
and quite inspiring work


30 Oct 2007 Ahmed Samieh

Q1 - In your program, what do you mean by
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)
A1 -
the answer of (X == Y) ---> true or false ---> 1 or 0
X is the X-dimension and Y is the Y-dimension
so i check if X-dimension equal Y-dimension of the object
if true it will give 1 else it will give 0
in 8-bit binary integer it will be 00000001 or 00000000 (equ 1)
again with (Extent = 1) ---> 1 or 0
multiply the result by 2 will give 2 or 0
in 8-bit binary integer it will be 00000010 or 00000000 (equ 2)
adding the result of (equ 1) and (equ 2) give

00000011 (3) ---> 00000010 (2) (Extent equal 1) + 00000001 (1) (X-dimension equal Y-dimension)
or 00000010 (2) ---> 00000010 (2) (Extent equal 1) + 00000000 (0) (X-dimension not equal Y-dimension)
or 00000001 (1) ---> 00000000 (0) (Extent not equal 1) + 00000001 (1) (X-dimension equal Y-dimension)
or 00000000 (0) ---> 00000000 (0) (Extent not equal 1) + 00000000 (0) (X-dimension not equal Y-dimension)

28 Oct 2007 imesha kuruppu

26 Oct 2007 Tariq Javid

Good work! This has inspired me a lot. I found it useful for error/noise free autonomous inspection. Another application area may be atifically created images.

28 Sep 2007 Ahmed Samieh

do you know what is the problem ?
the problem is, camera images has noise, so you need to reduce the noise (image enhancement)befor the recognation

Comment only
27 Sep 2007 Hammad Arshad

Does not work with images taken by a camera
Gives a lot of squares and circles where it is not needed

20 Sep 2007 Diego Barragán

17 Sep 2007 mahesh m

10 Sep 2007 Tariq Javid

Excellent, as circle shape classification approach inspired me to work on hexagon generation from an identified circle in an image

Comment only
22 Aug 2007 b c


Comment only
15 Aug 2007 michael scheinfeild

this is very good example how to classify shapes
i think to add also elipse detection
i need to think about it
thw width not equal height
and also it not fill the bounding box but it is true to the other not detected shape so i want to add more criterions

31 Mar 2016 1.0

include BSD License

Contact us