How to classify shapes of this image as square, rectangle, triangle and circle?
Afficher commentaires plus anciens

Please provide me the matlab code to identify shapes on this image and classify them as square, rectangle, circle and triangle.
5 commentaires
Ashish Uthama
le 19 Fév 2014
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
le 20 Fév 2014
phaneendra ch
le 11 Déc 2015
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
le 11 Déc 2015
Ur code is working very excellent.
Image Analyst
le 12 Déc 2015
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.
Réponse acceptée
Plus de réponses (11)
Image Analyst
le 19 Fév 2014
Modifié(e) : Image Analyst
le 19 Mar 2016
1 vote
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 commentaires
ROMIL
le 20 Fév 2014
Image Analyst
le 20 Fév 2014
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
le 21 Fév 2014
Image Analyst
le 21 Fév 2014
Modifié(e) : Image Analyst
le 21 Fév 2014
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.
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
le 21 Fév 2014

The algorithm is also unaffected by the orientation of the object. I have implemented it on these 2 images
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
le 21 Fév 2014
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
le 12 Déc 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 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
le 5 Jan 2017
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.
Image Analyst
le 5 Jan 2017
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
le 14 Jan 2020
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
le 14 Jan 2020
You forgot to attach your image. Make sure your objects are bright/white and the background is dark/black.
Quratulann Ashraf
le 15 Jan 2020
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
Image Analyst
le 15 Jan 2020
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
le 17 Jan 2020
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
le 17 Jan 2020
You forgot to attach pic.jpeg, and your m-file. I'll look at them after you attach them.
Quratulann Ashraf
le 18 Jan 2020
Modifié(e) : Quratulann Ashraf
le 18 Jan 2020
Image Analyst
le 18 Jan 2020
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
le 6 Oct 2021
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
le 6 Oct 2021
@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
le 22 Nov 2015
0 votes
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 commentaires
Image Analyst
le 22 Nov 2015
I'm attaching John D'Errico's function, minboundrect().
kaz
le 25 Avr 2016
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
le 12 Déc 2015
0 votes
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 commentaire
Image Analyst
le 12 Déc 2015
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
le 14 Déc 2015
0 votes
Sorry sir. here is the image
2 commentaires
Jan
le 18 Fév 2016
No, there is no image.
B.k Sumedha
le 19 Mar 2016
There is no image attached.
AKHIL RAJAGOPAL
le 23 Avr 2016
0 votes
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 commentaires
Image Analyst
le 23 Avr 2016
Try my code instead. I know it works.
AKHIL RAJAGOPAL
le 24 Avr 2016
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
le 24 Avr 2016
Maybe I've updated it since then (over two years ago) - I don't know. I'm attaching my current version.
kaz
le 25 Avr 2016
it still have function error and its not working for me =(
Image Analyst
le 25 Avr 2016
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
le 25 Avr 2016
i got this every time i run the code.


kaz
le 25 Avr 2016
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
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
le 25 Avr 2016
See attached spatial calibration demo.
kaz
le 25 Avr 2016
thank you so much
noor jahan m
le 8 Déc 2016
0 votes
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
le 23 Oct 2017
0 votes
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 commentaires
Image Analyst
le 23 Oct 2017
You misspelled it. The link to it is here: https://www.mathworks.com/matlabcentral/answers/116793-how-to-classify-shapes-of-this-image-as-square-rectangle-triangle-and-circle#comment_324923
Rahul Chauhan
le 24 Oct 2017
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
le 24 Oct 2017
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.)
Rahul Chauhan
le 25 Oct 2017
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
le 11 Déc 2017
0 votes
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 commentaire
Image Analyst
le 11 Déc 2017
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
le 28 Déc 2018
0 votes
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 commentaire
Image Analyst
le 28 Déc 2018
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
le 4 Fév 2020
0 votes
ROMIL can you pls provide the code for SHAPE DETECTION on ahaanakhurana@gmail.com.
3 commentaires
Image Analyst
le 4 Fév 2020
You did notice that I had already uploaded code, didn't you?
Dina Abd El-twab
le 24 Fév 2020
Modifié(e) : Dina Abd El-twab
le 24 Fév 2020
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
le 24 Fév 2020
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
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(:))
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
Catégories
En savoir plus sur Image Arithmetic dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
