MATLAB Answers


How to classify shapes of this image as square, rectangle, triangle and circle?

Asked by ROMIL
on 19 Feb 2014
Latest activity Commented on by Image Analyst
on 28 Dec 2018
Please provide me the matlab code to identify shapes on this image and classify them as square, rectangle, circle and triangle.


Your code is working very excellent but I am getting a problem I.e., if there is any small hole in the object the code is unable to generate matrix for it (or getting an error while exexcution). How can I fill up small enclosed holes in black and white image? And can I get a different subplots for different shapes(or different objects) in a images?
Not sure whose code you're talking about, but glad you finally got it working. If you have any questions in the future, post your image and code in a new question.

Sign in to comment.

10 Answers

Answer by Matt Kindig on 21 Feb 2014
Edited by Matt Kindig on 21 Feb 2014
 Accepted Answer

Another approach is to calculate the best-fit bounding rectangle of each object, such that the bounding rectangle can be oriented at an arbitrary angle. I took the following approach:
1) Identify the boundary (i.e. perimeter) of each object, using bwboundaries()
2) Calculate the smallest rectangular bounding box that contains this perimeter. To do this, I used the minboundrect function available at
3) I calculated the width, height, and area of each bounding rectangle. The aspect ratio (ratio between the width and height) can be used to determine whether it is a square (aspect ratio ~= 1.0) or rectangle.
4) For a rectangle or square, the filled area of the object (from regionprops()) should be almost the same as the area of its bounding rectangle, whereas for a triangle it should be substantially less.
5) For the circularity condition, I used the ratio between perimeter and area like Image Analyst suggested.
im = imread('');
%convert to 2D black and white with colors inverted
BW = im(:,:,1) < 10;
%get outlines of each object
[B,L,N] = bwboundaries(BW);
%get stats
stats= regionprops(L, 'Centroid', 'Area', 'Perimeter');
Centroid = cat(1, stats.Centroid);
Perimeter = cat(1,stats.Perimeter);
Area = cat(1,stats.Area);
CircleMetric = (Perimeter.^2)./(4*pi*Area); %circularity metric
SquareMetric = NaN(N,1);
TriangleMetric = NaN(N,1);
%for each boundary, fit to bounding box, and calculate some parameters
for k=1:N,
boundary = B{k};
[rx,ry,boxArea] = minboundrect( boundary(:,2), boundary(:,1)); %x and y are flipped in images
%get width and height of bounding box
width = sqrt( sum( (rx(2)-rx(1)).^2 + (ry(2)-ry(1)).^2));
height = sqrt( sum( (rx(2)-rx(3)).^2+ (ry(2)-ry(3)).^2));
aspectRatio = width/height;
if aspectRatio > 1,
aspectRatio = height/width; %make aspect ratio less than unity
SquareMetric(k) = aspectRatio; %aspect ratio of box sides
TriangleMetric(k) = Area(k)/boxArea; %filled area vs box area
%define some thresholds for each metric
%do in order of circle, triangle, square, rectangle to avoid assigning the
%same shape to multiple objects
isCircle = (CircleMetric < 1.1);
isTriangle = ~isCircle & (TriangleMetric < 0.6);
isSquare = ~isCircle & ~isTriangle & (SquareMetric > 0.9);
isRectangle= ~isCircle & ~isTriangle & ~isSquare; %rectangle isn't any of these
%assign shape to each object
whichShape = cell(N,1);
whichShape(isCircle) = {'Circle'};
whichShape(isTriangle) = {'Triangle'};
whichShape(isSquare) = {'Square'};
whichShape(isRectangle)= {'Rectangle'};
%now label with results
RGB = label2rgb(L);
imshow(RGB); hold on;
Combined = [CircleMetric, SquareMetric, TriangleMetric];
for k=1:N,
%display metric values and which shape next to object
Txt = sprintf('C=%0.3f S=%0.3f T=%0.3f', Combined(k,:));
text( Centroid(k,1)-20, Centroid(k,2), Txt);
text( Centroid(k,1)-20, Centroid(k,2)+20, whichShape{k});


Sir,the above code is working for the image given in the code but it is not working for the other images, the value of N is not getting correctly and the combined value obtained by the metrics is also not getting in a correct form. ty

Sign in to comment.

Answer by Image Analyst
on 19 Feb 2014
Edited by Image Analyst
on 19 Mar 2016

Look at the perimeter squared to area ratio. Use regionprops as shown in my Image Segmentation Tutorial:
See attached demo.


Mr.matt kindig, I am working with your "various shapes detecting" code which is in this page. The code u have written is working very well for the above black and white image(the image which you chosen to write code). But I am using some other images which might having small holes in the inside of the image those are coming while converting rgb to black and white image.I am getting error( error is the matrix cannot be generated). How to fill the small holes in that image. Can I draw subplots for your code for various shapes that are present in the image?
I am getting error Message as Undefined function or variable 'numSidesCircularity' for your code shape_recognition_demo.m.Please help me to solve this problem.
I just copied and pasted my demo from above and it works fine (as usual). Virtually every time someone says my demo didn't work it's because they modified it somehow. For example a common problem is the demo was expecting a gray scale image and the user changed it to feed it a color image. In your case since it was saying that numSidesCircularity is undefined, you may have deleted this line:
[binaryImage, numSidesCircularity] = CreateDemoImage();
and replaced it with an imread() of your own image. The only way I can tell is if you upload the version you are using along with your full error message (ALL the red text).

Sign in to comment.

Answer by phaneendra ch on 22 Nov 2015

While executing the above code I am getting an error in the code I.e.,(undefined function or variable 'minbounderect'). As I am new to MATLAB plz solve this prblm.tq in advance sir


I'm attaching John D'Errico's function, minboundrect().
i am so noob with matlab. sir how to add this function to the codes at the top. which i am getting the same error minboundrect. ty

Sign in to comment.

Answer by Venkatesh A on 12 Dec 2015

Mr.matt kindig, I am working with your "various shapes detecting" code which is in this page. The code u have written is working very well for the above black and white image(the image which you chosen to write code). But I am using some other images( 'shapes.jpg' which is now I am submitting) which might have some holes in the inside of the image and I am getting error( error is the matrix cannot be generated). How to fill the small holes in that image. Can I draw subplots for your code for various shapes that are present in the image?

  1 Comment

Venkatesh A, you forgot to give the link to your question where you attached your code and image.
If you do that, we can go to your question and tell you how to use imfill(binaryImage, 'holes') to fill holes in your image.

Sign in to comment.

Answer by AKHIL RAJAGOPAL on 23 Apr 2016

I am getting an error at isTriangle = ~isCircle & (TriangleMetric < 0.6); as: Error using & Matrix dimensions must agree.
Please help me solve this problem.


i do wonder if you have another demo that can measure rectangular shape in cm. of course with know object dimension in the real life. for example in the image attached. the red rectangular is 3cm X 3cm. how would i be able to find the frame size with knowing the dimensions of the box inside it.

Sign in to comment.

Answer by noor jahan m on 8 Dec 2016

i am a beginner in matlab. I tried the code for rectangle detection. I cud also find the function minboundrect . but i got error in convex hull of this function. Can u please help further? thank u


Sign in to comment.

Answer by Rahul Chauhan on 23 Oct 2017

Ty very much sir for the code but I'm getting error as "undefined function or variable 'minboundract'" So plz help me sir getting this error correct asap.... Again Ty in advance sir


Show 1 older comment
Ty sir but again showing error as convhull and also which values are to be supplied at minboundract.m and when I running it it shows provide values as in run button in maltab like-minded Run :minboundract (x,y, metric) so know which values I should provide here...... Help me sir about this problem...
Again, it's minboundrect(), not minboundract().
Supply your image and code in a new question (not as an Answer here in ROMIL's discussion thread - he probably doesn't care anymore since he posted this three and a half years ago.)
again sir i corrected the function but the code is not working here i am attaching the code and image file know help me.... here is the code :(1)file attached as test.m
here is the image: abc.jpg
ty in advance sir for helping me.....

Sign in to comment.

Answer by Pavel Vilbik on 11 Dec 2017

How to find the qr code( this plastic card) in this photo, as it has a begining job, I need a coordinate and axis, and that I would be marked with a ractangle.

  1 Comment

You should start your own question on this. In that question I'll tell you how to find the blobs, perhaps based on color saturation, and then to take the histogram of each blob looking for a fairly bimodal histogram. The standard deviation of the histogram of the all black and all white objects will be much less than a checkerboard object. If you still can't figure it out, I might post code over in that new question you're going to post.

Sign in to comment.

Answer by Michelle de Bock on 28 Dec 2018

Hi Sir,
Is it also possible to classify the direction of the triangle. e.g. left pointing triangle or right pointing triangle? Also classifying the thrid/fourth object in the picture? This is not really a rectangle, but how to separate this from a real rectangle?
Kind regards,

  1 Comment

Yes, I'm sure you could. Just modify my attached shape recognition demo. Once you have the blob, find its bounding box and centroid with regionprops. Then if the centroid is to the left of the centerline of the bounding box, it's pointing to the left. If it's below, it's pointing up.
For the other object, you'll also have to look for how many vertices it has and then perhaps scale a template to its size and see if enough pixels match to be considered that object. You could also do the template matching method with the triangles if you want. No, I don't have code for that but, being smart engineer, I'm sure you will find it easy to do.

Sign in to comment.