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
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
2014년 2월 20일
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
2015년 12월 11일
Ur code is working very excellent.
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.
채택된 답변
추가 답변 (11개)
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
2014년 2월 20일
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
2014년 2월 21일
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.
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
2014년 2월 21일

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
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
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
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.
Image Analyst
2017년 1월 5일
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
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
2020년 1월 14일
You forgot to attach your image. Make sure your objects are bright/white and the background is dark/black.
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
Image Analyst
2020년 1월 15일
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
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
2020년 1월 17일
You forgot to attach pic.jpeg, and your m-file. I'll look at them after you attach them.
Quratulann Ashraf
2020년 1월 18일
편집: Quratulann Ashraf
2020년 1월 18일
Image Analyst
2020년 1월 18일
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
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
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
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
2015년 11월 22일
I'm attaching John D'Errico's function, minboundrect().
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
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
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
2015년 12월 14일
0 개 추천
Sorry sir. here is the image
댓글 수: 2
Jan
2016년 2월 18일
No, there is no image.
B.k Sumedha
2016년 3월 19일
There is no image attached.
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
2016년 4월 23일
Try my code instead. I know it works.
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
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
2016년 4월 25일
it still have function error and its not working for me =(
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
2016년 4월 25일
i got this every time i run the code.


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
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
2016년 4월 25일
See attached spatial calibration demo.
kaz
2016년 4월 25일
thank you so much
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
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
Image Analyst
2017년 10월 23일
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
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
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.)
Rahul Chauhan
2017년 10월 25일
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
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
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
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
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
2020년 2월 4일
0 개 추천
ROMIL can you pls provide the code for SHAPE DETECTION on ahaanakhurana@gmail.com.
댓글 수: 3
Image Analyst
2020년 2월 4일
You did notice that I had already uploaded code, didn't you?
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
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
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
카테고리
도움말 센터 및 File Exchange에서 Image Arithmetic에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
