How To Identify Shapes Using Affine Transform?

2 visualizaciones (últimos 30 días)
Michael
Michael el 12 de Nov. de 2013
Comentada: Image Analyst el 12 de Nov. de 2013
Hi,
This is the problem I have: Given 4 images ('AllThreeShapes.jpeg','circle.jpeg','square.jpeg','triangle.jpeg'), I need to apply an affine transformation on those images to identify each of the shapes in 'AllThreeShapes.jpeg'. Basically, I just need to confirm in my coding that the left image is a circle, middle image is a triangle, and the right image is a square.
I figured that if I considered the pose of the objects with respect to a few reference points for each of the objects, then I could successfully identify the shapes in 'AllThreeShapes.jpeg' correctly.
I've attached the 4 images.
And the coding I have which reads in the 4 images is below:
if true
% code
end
clc
InputImage='AllThreeShapes.jpeg';
[img,map]=imread(InputImage);
figure
imshow(img,map);
title('Original Image of 3 Objects');
%Read the other images of the 3 seperate objects.
%(circle.jpeg,square.jpeg,& triangle.jpeg)
%and display them.
CircleImage='circle.jpeg';
[img,map]=imread(CircleImage);
figure
imshow(img,map);
title('Circle Object');
SquareImage = 'square.jpeg';
[img,map]=imread(SquareImage);
figure
imshow(img,map);
title('Square Object');
TriangleImage = 'triangle.jpeg';
[img,map]=imread(TriangleImage);
figure
imshow(img,map);
title('Triangle Object');
The problem I have is that I'm not too sure how to apply an affine transformation to the 'circle.jpeg','square.jpeg', and 'triangle.jpeg' images to identify those 3 objects correctly in 'AllThreeShapes.jpeg'.
Any help would be greatly appreciated.

Respuestas (2)

Image Analyst
Image Analyst el 12 de Nov. de 2013
I'm not sure why you want to apply an affine transform and what you think that will get you. Why don't you just use regionprops to calculate the area and perimeter and calculate the circularities, which will be different for each shape
circularities = perimeters .^ 2 / (4*pi*Areas);
Or else use bwboundaries to get the perimeter coordinates and then use this http://matlab.wikia.com/wiki/FAQ#How_do_I_find_.22kinks.22_in_a_curve.3F to find out how many kinks there are in the coordinates.
  2 comentarios
Michael
Michael el 12 de Nov. de 2013
Hmm...I thought about using perimeter and areas of the objects as well to determine the objects.
I've tinkered around with it some more, but I'm still having issues debugging the kinks out of it since the calculations its coming up with is giving me the wrong object as my output.
I basically took a binaryImage out of the 'AllThreeShapes.jpeg' image, removed the objects out of it, and used the regionprops like you suggested.
Thanks for helping me get closer with this problem. I think I'm almost there, but there are still a few kinks that need to be fixed out of it. It may just be a calculation mishap I'm overlooking. Not quite too sure.
My new code is below:
% Clear the workspace.
clc
fontSize=20;
message=sprintf('This Matlab script identifies which objects in AllThreeShapes.jpeg is a circle, square, or triangle');
%Read the original image containing the 3 objects.
InputImage = 'AllThreeShapes.jpeg';
[img,map] = 'AllThreeShapes.jpeg';
figure
imshow(img,map);
title('Original Image of 3 Objects');
%Read the other images of the 3 separate objects
% (circle.jpeg, square.jpeg, & triangle.jpeg)
% and display them. These images will be used to compare
% with the 'AllThreeShapes.jpeg' original image
CircleImage = 'circle.jpeg';
[img,map] = imread(CircleImage);
figure
imshow(img,map);
title('Circle Object');
SquareImage = 'square.jpeg';
[img,map] = imread(SquareImage);
figure
imshow(img,map);
title('Square Object');
TriangleImage = 'triangle.jpeg';
[img,map] = imread(TriangleImage);
figure
imshow(img,map);
title('Triangle Object');
% Read the original image into an array
[rgbImage storedColorMap] = imread('AllThreeShapes.jpeg');
[rows columns ColorBands] = size(rgbImage);
if ColorBands > 1
grayImage = rgbImage(:,:,1);
else
grayImage = rgbImage;
end
%Display the image.
subplot(2, 2, 2);
imshow(grayImage, []);
title('Grayscale Image', 'FontSize', fontSize);
%Remove the objects
binaryImage = bwareaopen(binaryImage, 300);
%Display it.
subplot(2, 2, 4);
imshow(binaryImage, []);
title('Cleaned Binary Image', 'FontSize', fontSize);
[labeledImage numOfObjects] = bwlabel(binaryImage);
ImageMeasure = regionprops(labeledImage, 'Perimeter', 'Area');
% Here I insert your circularity suggestion:
circularity = ImageMeasure.Perimeter .^2 / 4*pi*(ImageMeasure.Area);
%Display a message that tells the user if the object is a
%circle, square, or triangle.
for Number = 1 : numOfObjects
if circularity(Number) < 1.19
message = sprintf('The circularity of the object #%d is %.3f,
so the objects is a circle', Number, circularity(Number));
elseif circularity(Number) < 1.53
message = sprintf('The circularity of the object #%d is %.3f,
so the object is a square', Number, circularity(Number));
else
message = sprintf('The circularity of the object #%d is %.3f,
so the object is a triangle', Number, circularity(Number));
end
uiwait(msgbox(message));
end
The script is giving me the wrong output though. Saying all of the objects are triangles. Again, I'm probably just using my Area max limits wrong, but could you look this over and let me know what you think?
Image Analyst
Image Analyst el 12 de Nov. de 2013
You need to tweak your circularity numbers to get the proper range for each shape. But, yes, this is basically it. Please mark the answer as Accepted. Thanks.

Iniciar sesión para comentar.


Michael
Michael el 12 de Nov. de 2013
Never mind. I figured it out. Thanks for all the help!

Community Treasure Hunt

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

Start Hunting!

Translated by