skimage resize giving weird output

RHankins picture RHankins · Dec 11, 2015 · Viewed 10.8k times · Source

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]

Answer

RHankins picture RHankins · Dec 14, 2015

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.