Question

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
Was it helpful?

Solution 2

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?

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top