4.13043

4.1 | 23 ratings Rate this file 187 Downloads (last 30 days) File Size: 3.63 KB File ID: #15491
image thumbnail

Shape Recognition

by Ahmed Samieh

 

03 Jul 2007 (Updated 04 Jul 2007)

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  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (37)
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

22 Aug 2007 b c

good

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

17 Sep 2007 mahesh m  
20 Sep 2007 Diego Barragán  
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

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

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 Oct 2007 imesha kuruppu  
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)

02 Nov 2007 Hrushikesh Kulkarni

extremely good job
and quite inspiring work

thanks

30 Nov 2007 kevin scott basinio

its good

25 Jan 2008 RAJALAKSHMI ALAGIAMANAVALAN

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

31 Jan 2008 Aamir langah

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

06 Feb 2008 aymen sellaouti

Think you very much for your contribution

31 Mar 2008 Rocky Hurray  
09 Apr 2008 Nutthasit Laosuwan

good

29 Apr 2008 chen cuicui  
14 May 2008 bharathi v

nice to see this one .thank you

23 Jun 2008 Christian Stewart

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

15 May 2009 long yi

很好!

22 Jul 2009 Jun wan  
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?

16 Nov 2009 Alireza Saberi

nice

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

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

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

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

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.

20 Aug 2010 peter veres

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

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?

10 Oct 2010 mohammad salah

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

RGB = imread('test.bmp')

31 Jan 2011 Atif Anwer

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

03 Aug 2011 Jason

it‘s very good.

19 Dec 2011 am

Thank you so much.

15 Jan 2012 omar Alhmouz

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

19 Jan 2012 ahmed ismail  
Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
image analysis Ahmed Samieh 22 Oct 2008 09:18:05
shape properties Ahmed Samieh 22 Oct 2008 09:18:05
analysis Ahmed Samieh 22 Oct 2008 09:18:05
recognition Ahmed Samieh 22 Oct 2008 09:18:05
recognition kuoping 29 Jun 2010 07:13:26
shape properties vinu prasad 12 Oct 2010 01:53:33
recognition vinu prasad 12 Oct 2010 01:53:37
image analysis vinu prasad 12 Oct 2010 01:53:42
analysis vinu prasad 12 Oct 2010 01:53:45
recognition Gustavo Galvan 17 Feb 2011 16:27:30
analysis liu cheng 13 Jun 2011 03:35:46
analysis n vinod n 13 Jul 2011 13:54:57
analysis Nitin bansal 21 Sep 2011 23:28:22
image analysis nguyen 01 Oct 2011 02:46:09
recognition nguyen 01 Oct 2011 02:46:19
analysis Scool 13 Nov 2011 04:49:08
shape properties Sai Gokul 20 Nov 2011 04:02:08
image analysis Quang 12 Dec 2011 21:23:42

Contact us at files@mathworks.com