Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skimage - Weird results of resize function

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.

like image 984
Primoz Avatar asked May 30 '17 09:05

Primoz


People also ask

How does Skimage resize work?

Rescale operation resizes an image by a given scaling factor. The scaling factor can either be a single floating point value, or multiple values - one along each axis. Resize serves the same purpose, but allows to specify an output image shape instead of a scaling factor.

Is Skimage and Scikit image same?

scikit-image (a.k.a. skimage ) is a collection of algorithms for image processing and computer vision.

How do you resize a function in Python?

To resize an image, you call the resize() method on it, passing in a two-integer tuple argument representing the width and height of the resized image. The function doesn't modify the used image; it instead returns another Image with the new dimensions.

How do I open an image in Skimage?

Reading, displaying, and saving images First, we import the io module of skimage ( skimage.io ) so we can read and write images. Then, we use the skimage. io. imread() function to read a JPEG image entitled chair.


1 Answers

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()
    

Update: This method is deprecated, as per scipy.misc.imshow

  1. 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)
like image 127
Jalo Avatar answered Oct 27 '22 10:10

Jalo