4.32353

4.3 | 34 ratings Rate this file 177 Downloads (last 30 days) File Size: 3.63 KB File ID: #15491
image thumbnail

Shape Recognition

by

 

03 Jul 2007 (Updated )

differentiate Square, Rectangular, Circle from others

| Watch this File

File Information
Description

Objective
---------
The program should recognize objects like circles, rectangles, and squares from the input image.

Introduction
------------
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.

Input
-----
RGB image have the shapes to recognize.

Output
------
The RGB image with shapes recognized and labeled.

Required Products Image Processing Toolbox
MATLAB release MATLAB 7.0.1 (R14SP1)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (52)
07 Jul 2014 ghufran  
04 Jul 2014 Francisco Angel  
12 Jun 2014 huynh an  
19 May 2014 duc  
17 May 2014 long phan  
03 Mar 2014 Shoumy

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

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

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

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

28 Jul 2013 vishal

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??

22 May 2013 asd

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

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

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- http://www.rbi.org.in/currency/Sec_fea_Rs500.html

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

Thank you so much.

03 Aug 2011 Jason

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')

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?

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.

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);

06 May 2010 abhi m

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.

%STEP 2

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

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

%STEP 3

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

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

%STEP 4

%-------------------------------------------------------------dilation--------------------------------------------------------

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)));
end
end

%Displaying the dilated image

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

%STEP 5

%------------------------------------------------------------Erosion-------------------------------------------------

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)));
end
end

%Displaying eroded image

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

STATS = regionprops(L, 'all');

figure,
imshow(grayImage),
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');
title('circle');
case 2
plot(centroid(1),centroid(2),'wX');
title('rectangle');
case 3
plot(centroid(1),centroid(2),'wS');
title('square');
end
end
return

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?
Thanks

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

16 Nov 2009 Alireza Saberi

nice

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?

22 Jul 2009 Jun wan  
15 May 2009 long yi

很好!

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

29 Apr 2008 chen cuicui  
09 Apr 2008 Nutthasit Laosuwan

good

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

25 Jan 2008 RAJALAKSHMI ALAGIAMANAVALAN

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

30 Nov 2007 kevin scott basinio

its good

02 Nov 2007 Hrushikesh Kulkarni

extremely good job
and quite inspiring work

thanks

30 Oct 2007 Ahmed Samieh

FAQ:
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)
% UNKNOWN = 0
---------------
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)
ok....
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)
ok...
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

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

22 Aug 2007 b c

good

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

Contact us