File Exchange

Shape Recognition

version 1.0.0.0 (3.63 KB) by

differentiate Square, Rectangular, Circle from others

Updated

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.

Andrea Sica

Andrea Sica (view profile)

Oguz Deniz Merdin

Ahmed Samieh

Ahmed Samieh (view profile)

Sorry for not being available to answer the questions, it is the life (family and work) :)

The idea is the same, you gust need to identify some unique properties for the shape you are looking for (facts), and extract the information for the shapes in your image, check if the shape properties matches your target properties or not

for example, the ellipse has area = Pi * r1 * r2
and the difference between circle and ellipse is that r1 not equal r2 in the case of the ellipse

So, from properties, if Extent = Pi / 4 (+/- tolerance) we have (Circle or Ellipse)
if BoundingBox.X = BoundingBox.Y it is Circle, else it is Ellipse

I hope the idea is simple to understand and apply to any shape you can represent with mathematical equation

Thanks
Samieh

KARTHI KEYAN

KARTHI KEYAN (view profile)

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
co-ordinate.

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
plot(centroid(1),centroid(2),'wO');
case 2
plot(centroid(1),centroid(2),'wX');
case 3
plot(centroid(1),centroid(2),'wS');
end

thank you.

OGpl

good work man

Lubna Shah

Ali Sami

Ali Sami (view profile)

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

Kaajal Narayan

Kaajal Narayan (view profile)

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.

truong nhat quang

rahul

ghufran

Francisco Angel

huynh an

duc

long phan

Shoumy

Shoumy (view profile)

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

Samar Patel

Samar Patel (view profile)

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

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!

Vishal Raveendranathan

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

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

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

Silviani

Silviani (view profile)

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

rock karo

rock karo (view profile)

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

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

ahmed ismail

omar Alhmouz

omar Alhmouz (view profile)

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

am

am (view profile)

Thank you so much.

Jason

it‘s very good.

Atif Anwer

Atif Anwer (view profile)

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

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

peter veres

peter veres (view profile)

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?

peter veres

peter veres (view profile)

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

Surya Rathore

Surya Rathore (view profile)

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.

yar131 Iaroslav

yar131 Iaroslav (view profile)

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

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

[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

LIM CZE SIANG

LIM CZE SIANG (view profile)

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

Gül Begüm Semis

Gül Begüm Semis (view profile)

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

Alireza Saberi

nice

vsrin2 srin

vsrin2 srin (view profile)

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?

Jun wan

long yi

long yi (view profile)

Christian Stewart

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

bharathi v

nice to see this one .thank you

chen cuicui

Nutthasit Laosuwan

good

Rocky Hurray

aymen sellaouti

Think you very much for your contribution

Aamir langah

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

RAJALAKSHMI ALAGIAMANAVALAN

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

kevin scott basinio

its good

Hrushikesh Kulkarni

extremely good job
and quite inspiring work

thanks

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)

imesha kuruppu

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.

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

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

Diego Barragán

mahesh m

Tariq Javid

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

b c

good

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.0.0 include BSD License
MATLAB Release Compatibility
Created with R14SP1
Compatible with any release
Platform Compatibility
Windows macOS Linux