skimage调整大小给出奇怪的输出

时间:2015-12-11 15:51:57

标签: python matplotlib scikit-image

我正在使用skimage.transform.resize调整图像大小,但我得到了一个非常奇怪的输出,我无法找出原因。有人可以帮忙吗?

这是我的代码:

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

enter image description here

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

enter image description here

修改 好吧,如果我改变preserve_range=False,它似乎工作正常。但为什么它不允许我保持目前的范围呢?

修改 我正在使用OpenCV从视频中随机抽样帧。这是从我传递给它的视频路径返回帧的函数。

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数组:

 batch_frames = map(lambda vid: read_random_frames(vid), train_vids_batch)
 frame_tensor = np.asarray(batch_frames)
 y = frame_tensor[0]

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

  1. 正如 @RHankins 所述,plt.imshow可以显示像素范围为[0.0, 1.0]的浮点图像,也可以显示像素范围为[0,255]的整数图像。

  2. skimage.transform.resize调用函数wrap_warps.py#L161),该函数始终将输入图像转换为float64_warps.py#L797)。

这就是为什么您始终需要将输出转换为您喜欢的任何内容的原因。例如 调整图像大小并在您执行的np.uint8中获得[0, 255]的输出图像:

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)

答案 2 :(得分:0)

你能提供一个完整的例子吗?你如何阅读你的图像数据,原始格式是什么? (在.png文件上使用y = scipy.misc.imread("somefile.png"),它在我的机器上完美运行)

您的数据的数据类型(和ndarray.dtype)是什么?

import matplotlib.pyplot as plt
import skimage.transform
from scipy.misc import imread

y = imread("tree.png")

plt.subplot(121)
plt.imshow(y)
h,w,c = y.shape
x = skimage.transform.resize(y, (256, (w*256)//h), preserve_range=True)
plt.subplot(122)
plt.imshow(x)
plt.show()
相关问题