为什么 scipy.ndimage zoom 添加新频道?

Why scipy.ndimage zoom add new channel?

当我发现 python scipy.ndimage.zoom 执行缩放时自动添加新频道时,我感到很惊讶:

from scipy.misc import imread
from scipy.ndimage import zoom
img = imread('lena.jpg')
img=imread('lena.jpg')
img.shape
(468, 792, 3)
x = zoom(img, 1.0)
x.shape
(468, 792, 3)
x = zoom(img, 1.5)
x.shape
(702, 1188, 4)
x = zoom(img, 2)
x.shape
(936, 1584, 6)

宽度、高度尺寸正确,我无法理解其他尺寸的来源。

这种奇怪的行为只出现在彩色图像上:

x = zoom(img[:, :, 0], 2)
x.shape
(936, 1584)

ndimage.zoom 对频道(或图像)一无所知。它使用 n 维数组进行操作。如果你给出一个形状为 (9, 5, 3) 的数组并要求将其缩放为 2 倍,它将产生一个形状为 (9*2, 5*2, 3*2) 的数组,即 (18, 10, 6)

当第三轴是颜色通道时,当然这不是你想要的。使用逐轴缩放,将最后一个轴的缩放设置为 1:

zoom(img, (2, 2, 1))