0

I'm rotating a picture the following way:

# Read in image
img = cv2.imread("pic.jpg")
height, width, _ = img.shape
print("height ", height)
print("width ", width)

# Rotate image by 90 degrees
augmentation = iaa.Affine(rotate=90)
img_aug = augmentation(image=img)

height, width, _ = img_aug.shape
print("Height after rotation ", height)
print("Width after rotation ", width

> height  1080
> width  1920
> Height after rotation  1080
> Width after rotation  1920

Why does the shape of the image not change?

Shakesbeer
  • 71
  • 6

1 Answers1

3

Image augmentation does not change the actual or physical shape of your image. Think of the original shape as an window from where you see the image or the outside world. Here all the transformations i.e., rotations, stretchings, translation or in general any homography or non-linear warps are applied to the world outside your window. The opening from where you look at the image, in my analogy the window, stays the same irrespective how the world outside i.e., the image changes.

Now it can obviously bring in some other region, not present in the original view, to the augmented view. Most often, newly introduced pixels will be black or it may depend on what kind of padding is applied.

In short, augmentation operations are not something like matrix transpose where the actual shape may change.

img = cv2.imread("img.png")
augmentation = iaa.Affine(rotate=90)
img_aug = augmentation(image=img)

Let's see the images:

Original Image

enter image description here

Rotated Image

enter image description here

swag2198
  • 2,546
  • 1
  • 7
  • 18
  • Thank you @swag2198 for the detailed explanation. I now just use cv2.ROTATE_90_CLOCKWISE to actually change the shape of the image. – Shakesbeer Nov 02 '20 at 13:45
  • I am curious, why do you want to change the shape of the image! – swag2198 Nov 02 '20 at 13:47
  • 1
    I appreciate your words but please take a look here: https://stackoverflow.com/help/someone-answers. – swag2198 Nov 02 '20 at 14:05
  • 1
    As it can be seen in the rotated stack overflow image, it cuts off parts of the image. By changing the shapes via cv2.ROTATE_90_CLOCKWISE , this shouldn't happen. I'm using it for augmentation in an object detection model. If I don't change the shape, I get negative values for my bounding boxes. – Shakesbeer Nov 02 '20 at 14:53
  • 1
    iaa.Rot90(keep_size=False) will do what I want :-) It makes sense that iaa.Affine(rotate) does not change the shape, because the image can be rotated not only by -90,90 and 180 but by any degree. – Shakesbeer Nov 03 '20 at 08:32