尝试使用 OpenCV 保存网络摄像头图片时出错


import cv
capture = cv.CaptureFromCAM(0)
img = cv.QueryFrame(capture)
cv.SaveImage("test.JPG", img)

你好我只想在我的 Ubuntu 10 上使用 OpenCv 和 Python 保存网络摄像头中的图片。OpenCv 可以与网络摄像头连接。

但是我收到此错误:

OpenCV Error: Null pointer (NULL array pointer is passed) in cvGetMat, file /build/buildd/opencv-2.1.0/src/cxcore/cxarray.cpp, line 2376
Traceback (most recent call last):
  File "video.py", line 5, in <module>
    cv.SaveImage("test.JPG", img)
cv.error: NULL array pointer is passed

省去急诊室的旅行,并使用SimpleCV。它是OpenCV的Python绑定和更多工具的Pythonic包装器(它使用Numpy,Scipy和PIL(:

from SimpleCV import *
camera = Camera()
image = camera.getImage()
image.save('test.JPG')

我一遍又一遍地看到这个错误:CaptureFromCAM()调用失败,这意味着QueryFrame()因此失败并将 NULL 作为图像返回,导致SaveImage()也失败。

这里需要考虑两件事:

1( 您的网络摄像头可能不是索引 0(尝试 -1 或 1(2(学会安全编码!始终检查正在调用的函数的返回。这种做法将在未来为您节省大量时间:

 capture = cv.CaptureFromCAM(0)
 if not capture:
     // deal with error, return, print a msg or something else.
 img = cv.QueryFrame(capture)
 if not img:
     // deal with error again, return, print a msg or something else entirely.
 cv.SaveImage("test.JPG", img)

最新更新