Skimage - resize函数的结果奇怪

13

我正在尝试使用skimage.transform.resize函数调整.jpg图像的大小。函数返回给我奇怪的结果(请参见下面的图片)。我不确定是否是一个错误或只是错误地使用了该函数。

import numpy as np
from skimage import io, color
from skimage.transform import resize

rgb = io.imread("../../small_dataset/" + file)
# show original image
img = Image.fromarray(rgb, 'RGB')
img.show()

rgb = resize(rgb, (256, 256))
# show resized image
img = Image.fromarray(rgb, 'RGB')
img.show()

原始图像:

original

调整大小后的图像:

resized

我已经查看过skimage resize giving weird output,但我认为我的错误具有不同的特性。

更新:rgb2lab函数也有类似的错误。

1个回答

23
问题在于skimage在调整图像大小后会改变数组的像素数据类型。原始图像每个像素有8位,类型为numpy.uint8,而调整大小后的像素是numpy.float64变量。

调整大小操作是正确的,但结果显示不正确。为解决这个问题,我提出了两种不同的方法:

  1. 更改生成图像的数据结构。在转换为uint8值之前,像素必须先转换为0-255比例,因为它们在0-1规范化比例上:

     # ...
     # Do the OP operations ...
     resized_image = resize(rgb, (256, 256))
     # Convert the image to a 0-255 scale.
     rescaled_image = 255 * resized_image
     # Convert to integer data type pixels.
     final_image = rescaled_image.astype(np.uint8)
     # show resized image
     img = Image.fromarray(final_image, 'RGB')
     img.show()
    

更新: 此方法已被弃用,请参考scipy.misc.imshow

  1. 使用另一个库来显示图像。查看Image库文档,没有支持3xfloat64像素图像的模式。但是,scipy.misc库具有适当的工具,可以将数组格式转换为正确显示它所需的格式:
from scipy import misc
# ...
# Do OP operations
misc.imshow(resized_image)

1
谢谢,那解决了我的问题。使用 matplotlib 也可以。 - Primoz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接