0

I was asked to perform an image rotation about an arbitrary point. The framework they provided was in matlab so I had to fill a function called MakeTransformMat that receives the angle of rotation and the point where we want to rotate.

As I've seen in class to do this rotation first we translate the point to the origin, then we rotate and finally we translate back.

The framework asks me to return a Transformation Matrix. Am I right to build that matrix as the multiplication of the translate-rotate-translate matrices? otherwise, what am I forgetting?

function TransformMat = MakeTransformMat(theta,center_y,center_x) 

%Translate image to origin
trans2orig = [1 0 -center_x;
              0 1 -center_y;
              0 0 1];
%Rotate image theta degrees
rotation = [cos(theta) -sin(theta) 0;
            sin(theta) cos(theta)  0;
            0          0           1];
%Translate back to point
trans2pos = [1 0 center_x;
             0 1 center_y;
             0 0 1];

TransformMat = trans2orig * rotation * trans2pos;

end
BRabbit27
  • 6,333
  • 17
  • 90
  • 161
  • 1
    Did you try it? Did it give you back a correctly rotated image? – Dan Oct 09 '13 at 13:12
  • Nope I haven because I'm struggling with other part of the framework just before applying that, I am not use to MATLAB and they use very strange functions that I don't know what they do – BRabbit27 Oct 09 '13 at 13:15
  • Well I would suggest you first build a framework on which to test this (or if you have the image processing toolbox you can test with [`imtransform`](http://www.mathworks.com/help/images/ref/imtransform.html)). – Dan Oct 09 '13 at 13:20
  • Keep in mind that the order of transformations is significant. T2*R*T1 will give you different results than T1*R*T2. Just make sure that your points are being multiplied by your first transformation first, your second transformation second, and so forth. – beaker Oct 09 '13 at 16:26
  • Yes indeed that matrix does what it does. – BRabbit27 Oct 28 '13 at 16:31

2 Answers2

2

This worked for me. Here I is the input image and J is the rotated image

[height, width] = size(I);
rot_deg = 45;       % Or whatever you like (in degrees)
rot_xc = width/2;   % Or whatever you like (in pixels)
rot_yc = height/2;  % Or whatever you like (in pixels)


T1 = maketform('affine',[1 0 0; 0 1 0; -rot_xc -rot_yc 1]);
R1 = maketform('affine',[cosd(rot_deg) sind(rot_deg) 0; -sind(rot_deg) cosd(rot_deg) 0; 0 0 1]);
T2 = maketform('affine',[1 0 0; 0 1 0; width/2 height/2 1]);

tform = maketform('composite', T2, R1, T1);
J = imtransform(I, tform, 'XData', [1 width], 'YData', [1 height]);

Cheers.

Darien Pardinas
  • 5,910
  • 1
  • 41
  • 48
  • Good answer! Only the last matix is fixed to the center point it should be: T2 = maketform('affine',[1 0 0; 0 1 0; rot_xc rot_yc 1]); – Felix May 22 '14 at 13:51
1

I've answered a very similar question elsewhere: Here is the link.

In the code linked to, the point about which you rotate is determined by how the meshgrid is defined.

Does that help? Have you read the Wikipedia page on rotation matrices?

Community
  • 1
  • 1
Frederick
  • 1,271
  • 1
  • 10
  • 29
  • I was skeptical but then I tried it step by step along with paper and yes it actually works like it is supposed to work. – BRabbit27 Oct 28 '13 at 16:32