略读-resize函数的奇怪结果

2024-05-12 21:38:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图用skimage.transform.resize function调整一个.jpg图像的大小。函数返回奇怪的结果(见下图)。我不确定这是一个bug还是只是错误地使用了这个函数。

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,但是我认为我的bug有不同的属性。

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


Tags: 函数fromio图像imageimportimgshow
1条回答
网友
1楼 · 发布于 2024-05-12 21:38:24

问题是,在调整图像大小后,skipage正在转换数组的像素数据类型。原始图像每像素有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()
    
  2. 使用另一个库来显示图像。看看Image library documentation,没有任何模式支持3xfloat64像素图像。但是,scipy.misc库有适当的工具来转换数组格式,以便正确显示它:

    from scipy import misc
    # ...
    # Do OP operations
    misc.imshow(resized_image)
    

相关问题 更多 >