Shape recognition from user input and obtain XYZ coordinates
20 views (last 30 days)
Show older comments
I would like to identify the shape i need from the attached image according to the user input. After the shape has been recognised I want to be able to obtain the XYZ coordinates of the shape.
I thought it would be a simple use of the bwboundaries function or regionprops() but I can't seem to do so.
The attached images contains the shapes in white, so not including the black rectangle box that contains them.
0 Comments
Answers (1)
Matt J
on 14 Jun 2021
Edited: Matt J
on 15 Jun 2021
I thought it would be a simple use of the bwboundaries function or regionprops() but I can't seem to do so.
Assuming the shapes are all going to be circles and convex polygons, then one way would be to use pgonCorners (which must be Downloaded).
load Image; yourImage=BW;
yourImage=bwareaopen(yourImage,50);
S=regionprops(yourImage,'Centroid','ConvexImage','Circularity'); N=numel(S);
names={'','','Triangle','Quadrilateral','Pentagon','Hexagon','Unknown'};
for i=1:N
if S(i).Circularity>0.9, S(i).shape='Circle'; continue; end
ctr=getfield(regionprops(S(i).ConvexImage,'Centroid'),'Centroid'); %centroid in bounding box
k=3;
while 1
corners= pgonCorners(S(i).ConvexImage,k);
s = size( uniquetol( corners ,10,'ByRows',true,'DataScale',1) ,1);
if s==k-1 || s>=numel(names),
break;
end
S(i).corners=fliplr(corners)-ctr+S(i).Centroid;
k=k+1;
end
S(i).shape=names{s};
end
imshow(yourImage)
hold on
for i=1:N
drawpoint('Position',S(i).Centroid,'Label', S(i).shape,'LabelTextColor','y');
if ~isempty(S(i).corners)
scatter(S(i).corners(:,1), S(i).corners(:,2),'MarkerFaceColor','r','MarkerEdgeColor','y')
end
end
hold off
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!