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 が返されます。これら 2 つの関数の結果をチェックし、それらが成功することを確認する必要があります (この場合、メッセージを出力しているだけです。もちろん、他のことを行うこともできます)。

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 に答える