【问题标题】:OpenCV Python : QueryFrame return NoneOpenCV Python:QueryFrame返回无
【发布时间】:2013-09-19 20:17:42
【问题描述】:

我在 macbook 上使用 OpenCV 2.4 和 python 2.7.5。我想使用以下代码显示内置摄像头的实时流:

import cv

cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)
camera_index = 0
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)
    print type(frame)
    #cv.ShowImage("w1", frame)
    c = cv.WaitKey(10)
while True:
    repeat()

但是,QueryFrame 似乎并不总是返回 iplimage,这是我在终端上得到的:

<type 'NoneType'>
<type 'NoneType'>
<type 'NoneType'>
<type 'NoneType'>
<type 'NoneType'>
<type 'NoneType'>
<type 'NoneType'>
<type 'NoneType'>
<type 'NoneType'>
<type 'NoneType'>
<type 'NoneType'>
<type 'NoneType'>
<type 'NoneType'>
<type 'NoneType'>
<type 'cv2.cv.iplimage'>
<type 'NoneType'>
<type 'NoneType'>
<type 'cv2.cv.iplimage'>
<type 'NoneType'>
<type 'NoneType'>
<type 'NoneType'>
<type 'cv2.cv.iplimage'>
<type 'NoneType'>
<type 'NoneType'>
<type 'cv2.cv.iplimage'>
<type 'NoneType'>

有人知道问题出在哪里吗?

谢谢

编辑: 我注意到在我的相机打开之前需要几秒钟,所以我在进入“while”之前放了一些延迟。至于 noneType 问题,我不知道为什么我每 3 帧得到一个正确的图像..无论如何我只是通过设置一个条件来检查我们是否得到一个正确的图像来“修复”它,这里是代码:

import cv

cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)
camera_index = 0
capture = cv.CaptureFromCAM(camera_index)
c = cv.WaitKey(5000)
def repeat():
    global capture #declare as globals since we are assigning to them now
    global camera_index
    frame = cv.QueryFrame(capture)

    if frame:

        cv.ShowImage("w1", frame)
    c = cv.WaitKey(10)
while True:
    repeat()

【问题讨论】:

    标签: python macos opencv camera


    【解决方案1】:

    建议如下:

    • camera_index = -1
    • 删除第一帧以防万一

      firstImage = copy.deepcopy(cv.QueryFrame(capture))

    分享我的代码供你参考:

    import cv2 as cv
    import copy
    camera_index = -1 
    
    capture = cv.CaptureFromCAM(camera_index)
    
    isRunning = True
    firstImage = copy.deepcopy(cv.QueryFrame(capture)) #drop first frame which might empty
    cv.NamedWindow("Webcam",  cv.CV_WINDOW_AUTOSIZE);
    
    def repeat():
        global capture #declare as globals since we are assigning to them now
        global camera_index
        global isRunning
        global firstImage
        currImage = cv.QueryFrame(capture) 
        cv.ShowImage("Webcam",currImage)
        c = cv.WaitKey(10)
        if(c==27):
                isRunning = False
        if (c!=-1):
                print str(c)
    
    while isRunning:
        repeat()
    

    【讨论】:

    • 旧的cv api正在被淘汰,下个版本将无法使用,请移至cv2!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    • 2011-10-08
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    相关资源
    最近更新 更多