Jalal Aghazadeh
Jalal Aghazadeh

Reputation: 65

Rotating triangle in image - MATLAB

I'm writing a program for detecting basic shape in MATLAB. when I detect the shape I evaluate its Orientation then I rotate the shape to make its Orientation Zero, then I can evaluate its Projection and specify what it is.

The problem is the MATLAB function: regionprops() doesn't evaluate Orientation of Triangle correctly.

I = zeros(256,256);
pos_triangle = [64 64 128 192 128 128];
Is = insertShape(I, 'FilledPolygon', pos_triangle);
imshow(Is)

original = Is;

originalBW = im2bw(original);
figure; imshow(originalBW);
S = regionprops(originalBW,'All');

bwr=imrotate(originalBW,S.Orientation);
S2 = regionprops(bwr,'Centroid','Orientation');

figure;imshow(bwr);

I use imrotate fnc for rotating image, I don't have problem with rotation, imrotate is working well. the problem is in calculating the Orientation [using 'regionprps()' fnc] of the image! for example:I want to turn the triangle from this position

http://postimg.org/image/4un4sc7pn/

Orentation value: -28.9621 So I rotate it 28.9621 degree to change its postion to this

http://postimg.org/image/x68opdrm3/

but the output is this:

http://postimg.org/image/yf15or8y3/

using their Orientation (or other possible property of image)

an other example: changing the position from Up-Left 2nd triangle to Up-Left 1st triangle

Upvotes: 1

Views: 1457

Answers (2)

CroCo
CroCo

Reputation: 5741

I'm not an expert in image processing but to achieve rotation is not hard. The only thing you need to know the center of your image. In Matlab, the center of an image in the upper left corner. To rotate an object around the center, you need to multiply every point by the rotation matrix. Let's rotate the triangle around the center of the image.

clear
clc
close all

I = zeros(256,256);
pos_triangle = [64 64 128 192 128 128];

xt = [64 128 128];
yt = [64 192 128];

a = deg2rad(20);
R = [cos(a)  -sin(a); 
     sin(a)   cos(a)];

p1 = R*[64; 64]
p2 = R*[128; 192]
p3 = R*[128; 128]

% the rotated points
x1 = [p1(1) p2(1) p3(1)];
y1 = [p1(2) p2(2) p3(2)];

imshow(I)
hold on

fill(x1,y1,'r')
fill(xt,yt,'w')
hold off 

The result is now

enter image description here

If you would like to rotate it around itself, then you need to translate the center of the image to the center of the triangle. This is not a complete solution but I hope it helps.

Upvotes: 0

Benoit_11
Benoit_11

Reputation: 13945

Here is a way to work it out. Notice that:

1) I don't have the Computer Vision System Toolbox so I can't use insertShape; instead I used the fill function to create the triangle in the image and then getframe to obtain the actual image. I guess it comes down to the same than using insertShape.

2) I used the 'FilledArea' property of regionprops to detect the triangular shape. That's easy when there is a single shape, but if there were many of them you would have to modify a bit the code.

3) I performed a rotation equal to -1* the orientation given by regionprops to bring it back to 0. You can of course change it. The rotated image is bigger to account for the rotation.

Here is the code:

clear
clc
close all

I = zeros(256,256);
pos_triangle = [64 64 128 192 128 128];

xt = [64 128 128];
yt = [64 192 128];

%// Since I don't have the Computer Vision System Toolbox I use 'fill'.
%//Is = insertShape(I, 'FilledPolygon', pos_triangle);
imshow(I)
hold on

fill(xt,yt,'w')
hold off

The original triangle:

enter image description here

%// Create image using getframe.
hFrame = getframe(gca);
original = hFrame.cdata;

originalBW = im2bw(original(:,:,1));

%// Remove border of the image.
originalBW = imclearborder(originalBW);

figure; 

imshow(originalBW);

S = regionprops(originalBW,'FilledArea','Orientation');

%// Find region filled with the most pixels: that's the shape.
[a,b] =max([S.FilledArea]);

%// Get corresponding orientation
Orientation = S(b).Orientation;

%// Rotate by the inverse of the orientation; I'm not sur that's what you
%// want but it looks OK.
bwr=imrotate(originalBW,-1*Orientation);
S2 = regionprops(bwr,'Centroid','Orientation');

figure;imshow(bwr);

The rotated triangle, which looks like its orientation is 0.:

enter image description here

Hope that helps!

Upvotes: 2

Related Questions