使用"scipy.ndimage.interpolation.rotate"旋转一批图像



假设X是一个保存一组彩色图像的numpy.ndarray张量。例如,X的形状是(100, 3, 32, 32)的;也就是说,我们有 100 个 32x32 RGB 图像(例如 CIFAR 数据集提供的图像)。

我想在X中旋转图像,我发现这可以使用scipy的scipy.ndimage.interpolation.rotate函数来完成。

但是,我每次只能正确处理单个图像和单个颜色通道,而我想一次对所有图像和所有颜色通道应用该功能。

到目前为止,我尝试过并且它的工作原理如下:

import scipy.ndimage.interpolation
image = X[0,0,:,:]
rotated_image = scipy.ndimage.interpolation.rotate(input=image, angle=45, reshape=False)

但是我想做的,可能看起来像这样

X_rot = scipy.ndimage.interpolation.rotate(input=X, angle=45, reshape=False)

不起作用。

有没有办法对一批图像应用scipy.ndimage.interpolation.rotate,例如numpy.ndarray张量X

无论如何,有没有更好的方法来有效地旋转一批图像(存储在numpy.ndarray中)?

如果你想要一个单行,

import scipy.ndimage.interpolation as intrp
rotated_imgs = [intrp.rotate(input=img, angle=45, reshape=False) for img in X]

假设您的 X 具有更标准的形状(n_images, w, h, color_channels)

如果你想得到矩阵,我建议你用cv2.getRotationMatrix2D生成你的旋转矩阵,沿第一个轴复制/堆叠它,然后将其应用于你的X,但它可能会变得复杂,没有cv2.warpAffine,它需要单个图像作为输入。

最新更新