please how to rotate a matrix $5\times4$ by 45° around the origin $(0,0)$? using matlab

778 Views Asked by At

Suppose I have a matrix $M$ of $5\times4$ dimension (this is represent an image) :

M =  [3 4 8 9; 
      1 6 7 3;
      9 8 3 1;
      1 2 2 0;
      7 2 3 5];

I would like to rotate it around the origin $(0,0)$ with an angle of 45°.

From what I have found on the net , I have to multiply $M$ by Rotation Matrix $R$ is as follow :

R = [ cosd(45) -sind(45); 
        sind(45) cosd(45)]

Now do I simply multiply M by R and get a rotated matrix ??? like this :

rotM = R*M

I think I will have a problem of dimensions ? please how to rotate a matrix $5\times4$ by 45° around the origin $(0,0)$?


UPDATED

Here is an example of an Image (like M) that is rotated 45° around the center of the image : enter image description here

What I want to be specific is to get a rotation around the origin (0,0) of 45° (or different angle).

PS: imrotate, rot90, flip* and rotate is not suitable here, thank you in advance.

2

There are 2 best solutions below

5
On

Okey, I am answering myself since I was not 'clear' about what I want to get.

When I used a M matrix , I make reference to an Image Matrix.

So by rotation I mean create a real rotation by 45° in order to get something like the on in the following figure.

Any way here is a snippet that I succeeded to do and its result in the picture :

I = imread('cameraman.tif');

figure(1);
subplot(2,2,1);
imagesc(I);


T = [cosd(45), sind(45), 0;...
      -sind(45), cosd(45), 0;...
      0, 0, 1];
tform = maketform('affine', T);
I2 = imtransform(I,tform);

subplot(2,2,2);
imagesc(I2);



I3 = imrotate(I,-45);
subplot(2,2,3);
imagesc(I3);

Image :

enter image description here

0
On

You can use the rotation matrix to rotate the pixel coordinates, but then you still need to interpolate the image onto the new coordinates. Suppose your coordinates are in the matrices x and y, so that pixel $(m,n)$ is at location (x(m,n), y(m,n)). If your rotation matrix is R = [ cos(theta), -sin(theta); sin(theta), cos(theta) ], then you can get the new coordinates by converting the coordinates to a $2 \times P$ matrix where the $i^{th}$ column is the location of the $i^{th}$ pxiel and then multiplying it by the rotation matrix:

xy_rot = R * [ x(:)'; y(:') ];
x_rot  = reshape( xy_rot(1,:), size(x) );
y_rot  = reshape( xy_rot(2,:), size(y) );

This will apply the rotation to each column (i.e., each pixel location). We then separated the new $x$ and $y$ for convenience. Now that you know the new location of each pixel, you can interpolate the image to them:

img_rot = interp2( x, y, img, x_rot, y_rot );