重塑 skimage.imread return 读取的 png 图像 ValueError

Reshaping png image read by skimage.imread return ValueError

在以下代码中使用 616x346 png 图像作为输入效果很好:

from skimage import io

image = io.imread('img.png')
image = image.reshape(image.shape[0] * image.shape[1], 3)

...但是如果我将图像尺寸更改为 640x451,则会出现错误

ValueError: cannot reshape array of size 1154560 into shape (288640,3)

有什么想法吗?

您尝试整形的 640x451 图片的形状是 (640, 451, 4) 而不是 (640, 451, 3)。这就是您无法将其转换为 (640*451, 3) 的原因。查看 616x346 和 640x451 情况下 image.shape 的输出。一种解决方法是先将其从 rgba 转换为 rgb -

from skimage import io, color
image = io.imread('img.png')
image = color.rgba2rgb(image)
image = image.reshape(image.shape[0] * image.shape[1], 3)