【问题标题】:cv2.imshow() giving black screencv2.imshow() 给出黑屏
【发布时间】:2018-05-31 17:39:12
【问题描述】:

我正在将图像(numpy 数组)转换为字符串。然后我将此字符串转换回原始尺寸的 numpy 数组。因此,两个 numpy 数组都是相等的——事实上numpy.array_equals() 也返回True,因为数组相等。

当我在原始 numpy 数组上调用 cv2.imshow() 时,它会打印图像。但是当我在新的 numpy 数组上调用 cv2.imshow() 时,我只得到一个黑屏。

为什么会这样?两个 numpy 数组都是相等的,所以我应该得到相同的输出吧?

import numpy as np
import cv2

frame = cv2.imread( '/home/nirvan/img_two.png' , cv2.IMREAD_GRAYSCALE)
string = ' '.join(map(str,frame.flatten().tolist()))

frameCopy = frame.copy()

x = frame.shape[0]
y = frame.shape[1]

frame = string.strip()
temp = [ int(t) for t in frame.split(' ')]
temp = np.array(temp)
temp = temp.reshape( (x,y) )

print( np.array_equal(frameCopy , temp) )

#gives black screen
cv2.imshow('l' , np.array(temp) )

#gives proper image
#cv2.imshow('l' , np.array(frameCopy) )

cv2.waitKey()

【问题讨论】:

    标签: python arrays numpy cv2


    【解决方案1】:

    您的数组,即您的帧是相等的,但数据类型不是。你的temp 数组是int64 类型,而imshow 需要uint8。以下将修复您的脚本:

    cv2.imshow('l' , np.array(temp, dtype = np.uint8 ) )
    

    【讨论】:

      猜你喜欢
      • 2023-03-21
      • 1970-01-01
      • 2020-11-01
      • 1970-01-01
      • 2020-01-16
      • 2015-09-07
      • 2012-04-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多