Skimage - Weird results of resize function

拜拜、爱过 提交于 2019-12-07 06:55:01

问题


I am trying to resize a .jpg image with skimage.transform.resize function. Function returns me weird result (see image below). I am not sure if it is a bug or just wrong use of the function.

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 image:

Resized image:

I allready checked skimage resize giving weird output, but I think that my bug has different propeties.

Update: Also rgb2lab function has similar bug.


回答1:


The problem is that skimage is converting the pixel data type of your array after resizing the image. The original image has a 8 bits per pixel, of type numpy.uint8, and the resized pixels are numpy.float64 variables.

The resize operation is correct, but the result is not being correctly displayed. For solving this issue, I propose 2 different approaches:

  1. To change the data structure of the resulting image. Prior to changing to uint8 values, the pixels have to be converted to a 0-255 scale, as they are on a 0-1 normalized scale:

    # ...
    # 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. To use another library for displaying the image. Taking a look at the Image library documentation, there isn't any mode supporting 3xfloat64 pixel images. However, the scipy.misc library has the appropriate tools for converting the array format in order to display it correctly:

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


来源:https://stackoverflow.com/questions/44257947/skimage-weird-results-of-resize-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!