0

我是 Python opencv 的新手。谁能帮我解决错误

import cv

cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)
camera_index = 1
capture = cv.CaptureFromCAM(camera_index)

def repeat():
global capture #declare as globals since we are assigning to them now    global camera_index
frame = cv.QueryFrame(capture)
cv.ShowImage("w1", frame)
c = cv.WaitKey(100)
if(c=="n"): #in "n" key is pressed while the popup window is in focus
    camera_index += 1 #try the next camera index
    capture = cv.CaptureFromCAM(camera_index)
    if not capture: #if the next camera index didn't work, reset to 0.
        camera_index =1
        capture = cv.CaptureFromCAM(camera_index)

while True:
repeat()

这是我得到的错误 -

OpenCV Error: Null pointer (NULL array pointer is passed) in cvGetMat, file /home/paraste/OpenCV-2.3.1/modules/core/src/array.cpp, line 2382
Traceback (most recent call last):
  File "dualcamara.py", line 10, in <module>
img = cv.GetMat(cv.QueryFrame(capture), 500)
cv2.error: NULL array pointer is passed
4

2 回答 2

1

似乎要么失败,cv.CaptureFromCAM()要么cv.QueryFrame()失败(也许camera_index是错误的?),因此你得到一个frame导致该错误的 NULL 。您应该检查这两个函数的结果并确保它们成功(在这种情况下我只是打印一条消息,您当然可以做其他事情):

capture = cv.CaptureFromCAM(camera_index)
if not capture:
     print "Failed to initialize capture"

frame = cv.QueryFrame(capture)
if not frame:
     print "Failed to get frame"
于 2012-12-31T03:26:59.490 回答
0

cv.QueryFrame()可能返回 None 并且您没有处理这种可能性。我发现cv.QueryFrame()有时会在开头返回 None ,所以我简单地说:

if frame == None:
    return

这样,即使您的第一次调用失败,或者调用间歇性失败,您的循环也会继续并在捕获图像时提供图像。我在使用 python 2.7 的 Mac Book Pro 上遇到了同样的问题,这为我解决了。

于 2013-09-08T01:25:59.953 回答