使用OpenCV保存摄像头图片时出错
import cv
capture = cv.CaptureFromCAM(0)
img = cv.QueryFrame(capture)
cv.SaveImage("test.JPG", img)
你好,
我想用OpenCv和Python在我的Ubuntu 10上从我的摄像头保存一张图片。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
2 个回答
3
我看到这个错误反复出现: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)
4
为了避免去急救室,建议你使用 SimpleCV。它是一个用Python写的工具,可以帮助你更方便地使用OpenCV这个库,还有一些其他的工具(它使用了Numpy、Scipy和PIL这些库):
from SimpleCV import *
camera = Camera()
image = camera.getImage()
image.save('test.JPG')