【问题标题】:Skimage - Weird results of resize functionSkimage - 调整大小功能的奇怪结果
【发布时间】:2017-10-30 16:12:18
【问题描述】:

我正在尝试使用skimage.transform.resize function 调整.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()

原图:

调整大小的图像:

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

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

【问题讨论】:

    标签: python image image-resizing scikit-image


    【解决方案1】:

    问题是 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 library documentation,没有任何模式支持 3xfloat64 像素图像。但是,scipy.misc 库有适当的工具来转换数组格式以便正确显示:
    from scipy import misc
    # ...
    # Do OP operations
    misc.imshow(resized_image)
    

    【讨论】:

    • 谢谢你解决了我的问题。使用matplotlib 也可以。
    猜你喜欢
    • 2016-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-17
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多