使用imgaug旋转后图像的形状不会改变

问题描述

我通过以下方式旋转图片

# 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,_ = 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

为什么图像的形状不会改变?

解决方法

图像增强功能不会更改图像的实际shape。将原始shape看作是window,从那里您可以看到图像或外界。在这里,所有转换,即rotationsstretchingstranslation或一般任何homography或非线性warps都应用于窗口外的世界。不管是外面的世界如何变化(无论图像如何变化),在我看来,从窗口看的开口都保持不变。

现在,它显然可以将原始视图中不存在的其他区域引入增强视图。最常见的是,新引入的像素将为黑色,或者取决于所应用的padding类型。

简而言之,增强操作与矩阵transpose类似,实际形状可能会发生变化。

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

让我们看一下图像:

Original Image

enter image description here

Rotated Image

enter image description here