How can I differentiate different polygon shapes in image

5 views (last 30 days)
I want to write some code which can help me to classify images according to the polygon shape it contains, such as an image contains a triangle, then system should return "Triangle", if it contains a square then it should return "Square", one image contains only one polygon shape. I have a basic idea of coding, but I am not sure how can I implement those functions, this is the coding structure I came up with:
containSquare = false;
containTriangle = false;
containPentagon = false;
function checkForSquare ()
%if there is square
containSquare = true;
end
function checkForTriangle ()
%if there is Triangle
containTriangle = true;
end
function checkForPentagon ()
%if there is Pentagon
containPentagon = true;
end
return result
and this is a sample image:
any help would be appreciated.

Answers (1)

Image Analyst
Image Analyst on 23 Apr 2017
That's exactly what my demos do. Basically you find the centroid and make a profile of the radius as you go around the shape. Then count the number of peaks. The number of peaks will be the number of vertices and from that you know the polygon type.
  2 Comments
Alex kew
Alex kew on 23 Apr 2017
thanks for your reply. I noticed that you used circularities of blobs to determine the shape, here is your code:
% Determine the shape.
if circularities(blobNumber) < 1.2
% Theoretical value for a circle is 1.
message = sprintf('For object #%d,\nthe perimeter = %.3f,\nthe area = %.3f,\nthe circularity = %.3f,\nso the object is a circle',...
blobNumber, perimeters(blobNumber), areas(blobNumber), circularities(blobNumber));
shape = 'circle';
elseif circularities(blobNumber) < 1.5
% Theoretical value for a square is (4d)^2 / (4*pi*d^2) = 4/pi = 1.273
message = sprintf('For object #%d,\nthe perimeter = %.3f,\nthe area = %.3f,\nthe circularity = %.3f,\nso the object is a square',...
blobNumber, perimeters(blobNumber), areas(blobNumber), circularities(blobNumber));
shape = 'square';
elseif circularities(blobNumber) > 1.5 && circularities(blobNumber) < 1.8
% Theoretical value for an isosceles triangle is (3d)^2 / (4 * pi * 0.5 * d * d * sind(60)) = 9/(4 * pi * 0.5*sind(60)) = 1.6539
message = sprintf('For object #%d,\nthe perimeter = %.3f,\nthe area = %.3f,\nthe circularity = %.3f,\nso the object is an isosceles triangle',...
blobNumber, perimeters(blobNumber), areas(blobNumber), circularities(blobNumber));
shape = 'triangle';
else
message = sprintf('The circularity of object #%d is %.3f,\nso the object is something else.',...
blobNumber, circularities(blobNumber));
shape = 'something else';
end
if I want to find pentagon and hexagon, what circularities values should I use?
Image Analyst
Image Analyst on 23 Apr 2017
Circularity is not such a good method when the polygons start to become more rounded. I suggest you use the peak finding method instead.

Sign in to comment.

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!