【发布时间】:2016-07-11 19:31:32
【问题描述】:
我使用的代码是:
import cv2
camera = cv2.VideoCapture(0)
im=camera.read()[1]
print im
我将其输出为 None
在某些情况下,它会返回 RGB 值,但不是每次都返回。
在什么情况下它会返回 None??
【问题讨论】:
标签: python python-2.7 opencv rgb video-capture
我使用的代码是:
import cv2
camera = cv2.VideoCapture(0)
im=camera.read()[1]
print im
我将其输出为 None
在某些情况下,它会返回 RGB 值,但不是每次都返回。
在什么情况下它会返回 None??
【问题讨论】:
标签: python python-2.7 opencv rgb video-capture
您的问题是:
什么情况下会返回None??
这可以在VideoCapture 的文档中轻松找到。 对于函数 read 它指出:
这些方法/函数结合了 VideoCapture::grab() 和 VideoCapture::retrieve() 一次调用。这是最方便的 读取视频文件或从解码中捕获数据的方法和 返回刚刚抓取的帧。 If 未抓取任何帧(相机 已断开连接,或视频文件中没有更多帧), 方法返回 false,函数返回 NULL 指针。
所以与您的相机的连接似乎是问题所在。
【讨论】:
import cv2
cv2.namedWindow('webCam')
cap = cv2.VideoCapture(0)
if cap.isOpened():
ret, frame = cap.read()
else:
ret = False
print "problem here"
while True:
#get frames
ret,frame = cap.read()
frame = cv2.flip(frame,1) # flip image
cv2.imshow('webCam', frame) # show cam
# to exit
esc = cv2.waitKey(5) & 0xFF == 27
if esc:
break
cap.release()
cv2.destroyAllWindows()
【讨论】: