2012-12-30 28 views
0

我是Python opencv的新手。任何人都可以請幫我理清錯誤通過Python中的程序捕獲圖像時出錯

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 

回答

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" 
+0

你好,謝謝你的回覆。 CaptureFromCAM函數返回一些十六進制值,但QueryFrame函數不返回任何值。我認爲相機不是這樣的,我用奶酪命令手動切換它,但返回值沒有變化。請讓我知道如何使用內置功能打開相機。即使我嘗試使用函數cv2.VideoCapture.open ,但它以下列錯誤結束:AttributeError:'builtin_function_or_method'對象沒有屬性'打開' – parastenitk22f

0

cv.QueryFrame()可返回None和你沒有處理的可能性。 我發現cv.QueryFrame()有時在開始返回None,所以我簡單地說:

if frame == None: 
    return 

這樣一來,即使你第一次調用失敗,或者呼叫失敗間歇,你的循環將繼續和飼料圖像,因爲它們捕獲。我使用python 2.7在我的Mac Book Pro上遇到了同樣的問題,這爲我解決了這個問題。

相關問題