0

I have image rotation code that rotates a rectangle but currently the rectangle remains in the center of the rotated image. Currently I have image that needs to be rotated so that rectangles corner point is at upper left corner so that it can be easily cropped in the future. Rectangles corner points are known.

ang = rot*pi/180;

A = [ cos(ang) -sin(ang)  0;
  sin(ang)  cos(ang)  0;
  0  0  1];

T = maketform('affine',A);

OutputImage = imtransform(I,T);

imshow(OutputImage)

Image: enter image description here

Shai
  • 111,146
  • 38
  • 238
  • 371
M Smirth
  • 1
  • 1

1 Answers1

0

imtransform has a peculiar behavior:

The imtransform function automatically shifts the origin of your output image to make as much of the transformed image visible as possible.

This "automatic shift" is what makes your output move to undesirable locations.

In order to have more control over the transformation, I suggest using tformarray:

OutputImage = tformarray( I, maketform('affine',A), ...
                          makeresampler('cubic','fill'),
                          [2 1], [2 1], size(I(:,:,1)), [], 0 );

You might also consider using imrotate that rotates the image around its center. See this answer for an example.


Possibly relevant thread How to crop and rotate an image to bounding box?.

Community
  • 1
  • 1
Shai
  • 111,146
  • 38
  • 238
  • 371