user1847831
user1847831

Reputation: 65

How to rotate an image 45 degrees twice in matlab and keep the same size at the end?

I want to :

  1. rotate an image (size 512x512) with 45 degrees.
  2. make some processing on it.
  3. rotate the image with -45 degrees.

my problem is when i apply rotation with 45 degrees and -45 degrees, the size of the image changes and i want it to be the same.

Upvotes: 3

Views: 1414

Answers (2)

Spectre
Spectre

Reputation: 684

The usual procedure to rotate images is to scale the image up, rotate the image and scale it down. This way, you can avoid the dark margins that will appear when you rotate.

Matlab does this process automatically. So if you want a particular dimension for the image, you have to choose the appropriate region of the image after you have rotated it.

Suppose we wanted to rotate the image and want to retain the same dimensions as the original image, we can do this:

    img = imread('image.png'); 
    r = numel(img(:,1));
    c = numel(img(:,2));
    nimg = imrotate(img, 45);
    nimg = imrotate(nimg, 45);
    n_R = numel(nimg(:,1));
    n_C = numel(nimg(:,2));
    n_R = n_R+mod(n_R, 2); %to avoid dimensions being in double datatype
    n_C = n_C+mod(n_C, 2);
    oimg = nimg(((n_R/2)-(r/2)):((n_R/2)+(r/2)), ((n_C/2)-(c/2)):((n_C/2)+(c/2)),:);
    imwrite(oimg, 'rot_image.png');

Upvotes: 2

carandraug
carandraug

Reputation: 13091

You can't do it. It doesn't make sense. Simple experiment:

  1. get a square piece a paper and hold it against a white wall
  2. draw the borders of the square on the wall
  3. rotate the piece of paper 45 degrees
  4. draw another square on the wall that encloses the rotated piece of paper
  5. take the piece of paper out of the wall and observe why you can't do that
  6. leave the marks on the wall so you don't forget

Upvotes: 1

Related Questions