Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

skimage resize giving weird output

I'm resizing an image using skimage.transform.resize but I'm getting a really weird output and I can't figure out why. Can anyone help?

Here is my code:

import matplotlib.pyplot as plt
import skimage.transform
plt.imshow(y)
h,w,c = y.shape
x = skimage.transform.resize(y, (256, (w*256)/h), preserve_range=True)
plt.imshow(x)

Here is my input image y (240, 320, 3):

enter image description here

Here is my output image x (256, 341, 3):

enter image description here

Edit: Okay it seems to work fine if I change preserve_range=False. But why won't it allow me to keep the current range?

Edit: I'm randomly sampling frames from videos using OpenCV. Here's the function that returns a frame from the video path I pass to it.

def read_random_frames(vid_file):

   vid = cv2.VideoCapture(vid_file)
   # get the number of frames    
   num_frames = vid.get(cv2.CAP_PROP_FRAME_COUNT)
   # randomly select frame
   p_frame = random.randint(0, (num_frames-1))
   # get frame
   vid.set(cv2.CAP_PROP_POS_FRAMES, p_frame)
   ret, frame = vid.read()
   # convert from BGR to RGB
   frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

   return frame

I have a list of video paths and I use a map function to retrieve the frames then I convert the outputed list to a numpy array:

 batch_frames = map(lambda vid: read_random_frames(vid), train_vids_batch)
 frame_tensor = np.asarray(batch_frames)
 y = frame_tensor[0]
like image 241
RHankins Avatar asked Dec 11 '15 15:12

RHankins


People also ask

How do I resize an image in a Skimage?

We can resize images with the skimage. transform. resize() function. NumPy array commands, like image[image < 128] = 0 , and be used to manipulate the pixels of an image.


2 Answers

I think it is simply because by preserving the range I end up with a float in the range [0, 255] whereas pyplot.imshow is only capable of displaying MxNx3 float arrays in the range [0.0, 1.0]. When I convert the output to an uint8 using z = np.copy(x).astype('uint8') it displays fine.

like image 166
RHankins Avatar answered Nov 13 '22 23:11

RHankins


  1. As @RHankins mentioned, plt.imshow can display either float images with pixels within range [0.0, 1.0] or integer images with pixels within range [0, 255].

  2. skimage.transform.resize calls a function wrap (_warps.py#L161) which always converts an input image to float64 (_warps.py#L797).

That's why you always need to convert the output to whatever you prefer. For example to resize image and get np.uint8 output image within range in [0, 255] you do:

import matplotlib.pyplot as plt
from skimage import img_as_ubyte
import skimage.transform
x = img_as_ubyte(skimage.transform.resize(y, (100, 100)))
plt.imshow(x)
like image 42
Temak Avatar answered Nov 14 '22 00:11

Temak