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

Please provide me the matlab code to identify shapes on this image and classify them as square, rectangle, circle and triangle.

5 件のコメント

Ashish Uthama
Ashish Uthama 2014 年 2 月 19 日
Romil, look at http://www.mathworks.com/help/images/ref/regionprops.html and the properties it returns. See if anything stands out that you could use to perform the classification. Give it a try, and post back if you have trouble developing it.
ROMIL
ROMIL 2014 年 2 月 20 日
Ashish, I am using boundingbox which fails when the shape is at some angle and for triangle nothing good seems to be working.
phaneendra ch
phaneendra ch 2015 年 12 月 11 日
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?
phaneendra ch
phaneendra ch 2015 年 12 月 11 日
Ur code is working very excellent.
Image Analyst
Image Analyst 2015 年 12 月 12 日
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.

サインインしてコメントする。

 採用された回答

Matt Kindig
Matt Kindig 2014 年 2 月 21 日
編集済み: Matt Kindig 2014 年 2 月 21 日
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 http://www.mathworks.com/matlabcentral/fileexchange/34767-a-suite-of-minimal-bounding-objects/content/MinBoundSuite/minboundrect.m.
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.
Enjoy!
im = imread('http://www.mathworks.com/matlabcentral/answers/uploaded_files/8372/abc.jpg');
%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
end
SquareMetric(k) = aspectRatio; %aspect ratio of box sides
TriangleMetric(k) = Area(k)/boxArea; %filled area vs box area
end
%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});
end

12 件のコメント

Rahul
Rahul 2014 年 2 月 22 日
Mr.Matt , this technique works very well. Thanks for your help!
pradeep vishwakarma
pradeep vishwakarma 2015 年 10 月 19 日
BW = im(:,:,1) < 10; what this instruction actually doing
Image Analyst
Image Analyst 2015 年 10 月 19 日
pradeep, that gives a binary image where it's true, white, 1 where the red channel of im is less than 10, and false, black, 0 elsewhere.
By the way, the first 5 lines inside Matt's for loop, along with the call to bwboundaries, can be eliminated if you simply pass 'BoundingBox' into regionprops(), which will give simpler code.
Luong Tu
Luong Tu 2016 年 5 月 21 日
I have a color image, how to use this code, I have convert to binary image but it doesn't working
Image Analyst
Image Analyst 2016 年 5 月 21 日
Either take one of the color channels, or convert to grayscale, then threshold.
Priya Raj
Priya Raj 2018 年 3 月 1 日
sir how to add this function to the code at the top.I am getting the same error minboundrect. ty
Priya: See this snippet. Adapt as needed:
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
end
Priya Raj
Priya Raj 2018 年 3 月 20 日
thank you sir!!
Priya Raj
Priya Raj 2018 年 3 月 20 日
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
Mark Steven Billones
Mark Steven Billones 2018 年 3 月 21 日
sir can't run in matlab 2015, whats the problem?
Beenish Ishtiaq
Beenish Ishtiaq 2021 年 8 月 5 日
error in minboundrect
Image Analyst
Image Analyst 2021 年 8 月 5 日

サインインしてコメントする。

その他の回答 (11 件)

Image Analyst
Image Analyst 2014 年 2 月 19 日
編集済み: Image Analyst 2016 年 3 月 19 日

1 投票

Look at the perimeter squared to area ratio. Use regionprops as shown in my Image Segmentation Tutorial: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862
[EDIT]
See attached demo.

21 件のコメント

ROMIL
ROMIL 2014 年 2 月 20 日
I am already using some parts of your code. but still couldn't get it to work properly. For shapes I am using boundingbox which is working fine unless the shape is at some angle.
Image Analyst
Image Analyst 2014 年 2 月 20 日
I'm really surprised the circularity measure I suggested didn't work. Are you telling me that with all of those shapes there is no correlation between shape type and circularity = perimeter^2/(4*pi*area)? I find that hard to believe and I'd need to verify that. I'll do some experiments with your image later today to prove what you say.
ROMIL
ROMIL 2014 年 2 月 21 日
Please try and if something works, do share. It would be really helpful.
Image Analyst
Image Analyst 2014 年 2 月 21 日
編集済み: Image Analyst 2014 年 2 月 21 日
I thought I asked you to adapt my segmentation demo . I could have looked over your code. Anyway, I do have a prior demo that uses the standard MATLAB demo image. I attached it (below in blue). See if you can adapt it to your image.
Rahul
Rahul 2014 年 2 月 21 日
編集済み: Rahul 2014 年 2 月 21 日
Sir, first of all thanks for your worthy suggestion.The circularity measure is working pretty well.I have implented it on the following image. But the values of circularity measure only differ by .1 to .2 for different shapes.Would it be actually proper to use this algorithm, if we are thinking of designing a versatile and more efficient system.
<<
>>
Rahul
Rahul 2014 年 2 月 21 日
The algorithm is also unaffected by the orientation of the object. I have implemented it on these 2 images
Rahul
Rahul 2014 年 2 月 21 日
編集済み: Rahul 2014 年 2 月 21 日
Sir , we are actually working on 3-D shape detection of shapes like cube,cuboid,pyramid and sphere. And we are considering the top view of the objects. The shapes viewed by the camera in top view are not exactly circle,square or triangle. The edges of these 3-D shapes are creating little bit distortion and problem in shape detection.Can you suggest some good image pre-processing techniques to avoid edge distortion.
Image Analyst
Image Analyst 2014 年 2 月 21 日
You could count the number of "kinks" in the boundary. See the FAQ: http://matlab.wikia.com/wiki/FAQ#How_do_I_find_.22kinks.22_in_a_curve.3F
Venkatesh A
Venkatesh A 2015 年 12 月 12 日
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?
then mozhi
then mozhi 2017 年 1 月 5 日
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).
Quratulann Ashraf
Quratulann Ashraf 2020 年 1 月 14 日
Sir your code run perfectly but when i attach other image, it classifies its whole outer boundary as square . That is it considers it as 1 object.
Image Analyst
Image Analyst 2020 年 1 月 14 日
You forgot to attach your image. Make sure your objects are bright/white and the background is dark/black.
Quratulann Ashraf
Quratulann Ashraf 2020 年 1 月 15 日
okay!!!Thank you so much
sir instead of demo image, i want to attach my own image...can you please guide what should i delete and add from your code to do so
Replace this
% Now create a demo image.
[binaryImage, numSidesCircularity] = CreateDemoImage();
with a call to imread() to read in your image, or just comment it out if you have code above that to create your own binary image.
Quratulann Ashraf
Quratulann Ashraf 2020 年 1 月 17 日
sir i created my own code for binary image as given below, but code gives error for "numSidesCircularity" which is not defined in my case.
i=imread('pic.jpeg');
a=rgb2gray(i);
binaryImage=edge(a,'canny');
binaryImage = bwareaopen(binaryImage,30);
se = strel('disk',2);
binaryImage = imclose(binaryImage,se);
binaryImage = imfill(binaryImage,'holes');
Image Analyst
Image Analyst 2020 年 1 月 17 日
You forgot to attach pic.jpeg, and your m-file. I'll look at them after you attach them.
Quratulann Ashraf
Quratulann Ashraf 2020 年 1 月 18 日
編集済み: Quratulann Ashraf 2020 年 1 月 18 日
Sir these are the two images i am trying for this code...
There is NO reason for you to use edge detection on those images. I don't know why all beginners think edge detection is the first step in any image processing problem just because an image has sharp edges in it. It is almost always NOT the best first step. What you need to do is first get a binary image of the shapes and for those computer graphic images you simply want to threshold.
For the monochrome image:
binaryImage = grayScaleImage < 128;
For the color image
% Find out where any pixel is not white.
binaryImage = min(rgbImage, [], 3) < 255;
aliya
aliya 2021 年 10 月 6 日
hye sir, i use your shape_recognition_demo.m. code. but i want to use my own image, i already use your method which replace the
% Now create a demo image.
[binaryImage, numSidesCircularity] = CreateDemoImage();
with imread() but i got an error 'unrecognized function or variable 'binaryImage' , how to solve this? thankyou in advanced!
Image Analyst
Image Analyst 2021 年 10 月 6 日
@aliya I just downloaded it and it works fine. You modified it somehow but didn't attach your code so I can't fix it.
I'm attaching the lastest version I have of the demo (not sure if it's changed over the last 7 years, but probably).

サインインしてコメントする。

phaneendra ch
phaneendra ch 2015 年 11 月 22 日

0 投票

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

2 件のコメント

Image Analyst
Image Analyst 2015 年 11 月 22 日
I'm attaching John D'Errico's function, minboundrect().
kaz
kaz 2016 年 4 月 25 日
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

サインインしてコメントする。

Venkatesh A
Venkatesh A 2015 年 12 月 12 日

0 投票

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 件のコメント

Image Analyst
Image Analyst 2015 年 12 月 12 日
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.

サインインしてコメントする。

Venkatesh A
Venkatesh A 2015 年 12 月 14 日

0 投票

Sorry sir. here is the image

2 件のコメント

Jan
Jan 2016 年 2 月 18 日
No, there is no image.
B.k Sumedha
B.k Sumedha 2016 年 3 月 19 日
There is no image attached.

サインインしてコメントする。

AKHIL RAJAGOPAL
AKHIL RAJAGOPAL 2016 年 4 月 23 日

0 投票

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

10 件のコメント

Image Analyst
Image Analyst 2016 年 4 月 23 日
Try my code instead. I know it works.
AKHIL RAJAGOPAL
AKHIL RAJAGOPAL 2016 年 4 月 24 日
Image Analyst, is shape_recognition_demo.m the file you are talking about? This code is showing a lot of errors and I think I need to debug from the beginning.If not please send me the right file.
Image Analyst
Image Analyst 2016 年 4 月 24 日
Maybe I've updated it since then (over two years ago) - I don't know. I'm attaching my current version.
kaz
kaz 2016 年 4 月 25 日
it still have function error and its not working for me =(
Image Analyst
Image Analyst 2016 年 4 月 25 日
I just ran it and it ran fine. Are you sure you're not mistaking normal information popup message that explain what you're seeing with error messages? Do you see any red text in the command window? If so, paste all the red text back here.
kaz
kaz 2016 年 4 月 25 日
i got this every time i run the code.
kaz
kaz 2016 年 4 月 25 日
oh i did find my mistake sir. thank you very much for the codes. very helpful. i think my other mate did the same mistake which i didnt include the first two lines of the code because i though they were the title of this demo .. thanks again sir
kaz
kaz 2016 年 4 月 25 日
編集済み: kaz 2016 年 4 月 25 日
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.
Image Analyst
Image Analyst 2016 年 4 月 25 日
See attached spatial calibration demo.
kaz
kaz 2016 年 4 月 25 日
thank you so much

サインインしてコメントする。

noor jahan m
noor jahan m 2016 年 12 月 8 日

0 投票

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
Rahul Chauhan
Rahul Chauhan 2017 年 10 月 23 日

0 投票

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

4 件のコメント

Rahul Chauhan
Rahul Chauhan 2017 年 10 月 24 日
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...
Image Analyst
Image Analyst 2017 年 10 月 24 日
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
(2)minboundrect.m
here is the image: abc.jpg
ty in advance sir for helping me.....

サインインしてコメントする。

Pavel Vilbik
Pavel Vilbik 2017 年 12 月 11 日

0 投票

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 件のコメント

Image Analyst
Image Analyst 2017 年 12 月 11 日
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.

サインインしてコメントする。

Michelle de Bock
Michelle de Bock 2018 年 12 月 28 日

0 投票

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,
Michelle

1 件のコメント

Image Analyst
Image Analyst 2018 年 12 月 28 日
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.

サインインしてコメントする。

Ahaana Khurana
Ahaana Khurana 2020 年 2 月 4 日

0 投票

ROMIL can you pls provide the code for SHAPE DETECTION on ahaanakhurana@gmail.com.

3 件のコメント

Image Analyst
Image Analyst 2020 年 2 月 4 日
You did notice that I had already uploaded code, didn't you?
Dina Abd El-twab
Dina Abd El-twab 2020 年 2 月 24 日
編集済み: Dina Abd El-twab 2020 年 2 月 24 日
pp=alexnet;
ppl=pp.Layers;
pp=pp.Layers(1:19);
ppp=[pp
fullyConnectedLayer(2)
softmaxLayer()
classificationLayer()]
options = trainingOptions('sgdm',...
'InitialLearnRate',1e-3,...
'MaxEpochs',10,...
'CheckpointPath',tempdir);
train1 = trainFasterRCNNObjectDetector(gTruth,ppp,options, ...
'NegativeOverlapRange',[0 0.1], ...
'PositiveOverlapRange',[0.5 1], ...
'SmallestImageDimension',300);
a = imread('US0018_0131.png');
a = imresize(a,[227 227]);
[bbox,score,label] = detect(train1,a);
detect= insertShape(a,'rectangle',bbox);
figure
imshow(detect)
@Image Analyst
Image Analyst
Dina Abd El-twab
Dina Abd El-twab 2020 年 2 月 24 日
I applied this code to draw a rectangle on the region of interest that i want after taining using faster RCNN .I want to convert the drawn rectangle to be circle in the next step , could you help me please ?
@Image Analyst
Image Analyst

サインインしてコメントする。

Matt J
Matt J 2025 年 8 月 2 日
編集済み: Matt J 2025 年 8 月 5 日
Fit a polyshape to each of the objects by downloading bwlpolyshape()
Then, you can basically just count the number of sides in each of the polyshapes.
load Image;
pgons=bwlpolyshape(~BW, Visualize=true);
shapes=arrayfun(@classify,pgons(:))
shapes = 8×1 string array
"Square" "Triangle" "Rectangle" "Rectangle" "Circle" "Square" "Triangle" "Square"
function shape=classify(pgon)
Lengths=vecnorm(diff(pgon.Vertices([end,1:end],:)),2,2); %Side lengths
Lengths(Lengths<max(Lengths)/20)=[]; %Tolerance on shortest side length
N=numel(Lengths); %Number of sides (after tolerance)
if N==3
shape="Triangle";
elseif N==4
shape="Rectangle";
if max(Lengths)-min(Lengths)<3 %Tolerance on side length equality
shape="Square";
end
elseif N>10
shape="Circle";
end
[cx,cy]=centroid(pgon);
drawpoint(Position=[cx,cy],Label=shape,LabelText="white");
end

カテゴリ

ヘルプ センター および File ExchangeImage Processing Toolbox についてさらに検索

質問済み:

2014 年 2 月 19 日

編集済み:

2025 年 8 月 5 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by