skimage 调整大小给出奇怪的输出 [英] skimage resize giving weird output

查看:50
本文介绍了skimage 调整大小给出奇怪的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 skimage.transform.resize 调整图像的大小,但是却得到了一个非常奇怪的输出,我不知道为什么.有人可以帮忙吗?

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?

这是我的代码:

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)

这是我的输入图像 y (240, 320, 3):

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

这是我的输出图像x(256、341、3):

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

好的,如果我更改 preserve_range=False,它似乎可以正常工作.但是为什么不允许我保持当前范围呢?

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

我正在使用OpenCV从视频中随机采样帧.这是从我传递给它的视频路径中返回一帧的函数.

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

我有一个视频路径列表,我使用 map 函数来检索帧,然后我将输出的列表转换为一个 numpy 数组:

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]

推荐答案

我认为这仅仅是因为通过保留范围,我最终得到了范围 [0, 255] 中的浮点数,而 pyplot.imshow仅能显示[0.0,1.0]范围内的MxNx3个浮点数组.当我使用 z = np.copy(x).astype('uint8')将输出转换为uint8时,它显示得很好.

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.

这篇关于skimage 调整大小给出奇怪的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆