Skimage - Weird results of resize function

Primoz picture Primoz · May 30, 2017 · Viewed 9.2k times · Source

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:

original

Resized image:

resized

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

Update: Also rgb2lab function has similar bug.

Answer

Jalo picture Jalo · May 30, 2017

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)