【问题标题】:skimage.transform.rescale adds an extra dimension to input imageskimage.transform.rescale 为输入图像添加了一个额外的维度
【发布时间】:2021-02-13 13:44:08
【问题描述】:

当我使用skimage.transform.rescale() 时,我得到一个输出图像数组,当我的输入图像只有 2 维时,它有 3 维。

from skimage import io, color, transform

image = io.imread(r'C:\Users\ParthD\PycharmProjects\pythonProject\test_images\6.png')
image_bw = color.rgb2gray(color.rgba2rgb(image))
image_rescaled = transform.rescale(image, scale=0.5, anti_aliasing=True)

print(image_bw.shape)
print(image_rescaled.shape)

为此,我得到如下输出:

(397, 602)
(198, 301, 2)

值 2 的附加维度在哪里加起来,我不确定。我查看了 rescale 函数文档,但没有任何参数会影响这个额外的维度。

【问题讨论】:

  • 您正在重新调整image,而不是image_bw

标签: python image-processing computer-vision scikit-image rescale


【解决方案1】:

所以问题是channel维度被解释为空间维度。

您应该将transform.rescale 传递给multichannel=True 标志,这样它就不会影响频道:

image_rescaled = transform.rescale(image, scale=0.5, anti_aliasing=True, multichannel=True)
  • 例子:

    q = np.zeros((397, 602, 3))
    x1 = transform.rescale(q, scale=0.5, anti_aliasing=True)
    x2 = transform.rescale(q, scale=0.5, anti_aliasing=True, multichannel=True)
    x1.shape  # (198, 301, 2)
    x2.shape  # (198, 301, 3)
    

所以transform.rescale 将您的数组视为形状为 (397, 602, 3) 的 3D 图像,该图像向下到 (198, 301, 2),并与通道一起插值,就好像它们是另一个空间维度。

如果您的图像是灰色图像,没有通道尺寸,则不需要传递multichannel=True 标志。这将导致最后一个轴被视为通道,并且您会得到不希望的输出。

  • 例子:

    q1 = np.zeros((397, 602))
    x3 = transform.rescale(q1, scale=0.5, anti_aliasing=True, multichannel=True)
    x4 = transform.rescale(q1, scale=0.5, anti_aliasing=True)
    x3.shape  # (198, 602)
    x4.shape  # (198, 301)
    

您可以参考docs

【讨论】:

    猜你喜欢
    • 2019-01-17
    • 2023-03-07
    • 2018-05-22
    • 1970-01-01
    • 2019-11-19
    • 2019-06-18
    • 1970-01-01
    • 2021-03-29
    • 1970-01-01
    相关资源
    最近更新 更多