Error while trying to save webcam picture with OpenCV

我只是一个虾纸丫 提交于 2019-11-28 11:17:22

问题


import cv

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

Hi, I just want to save a picture from my webcam with OpenCv and Python on my Ubuntu 10. OpenCv can connect with the webcam.

But I get this error:

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

回答1:


Save yourself a trip to the emergency room and use SimpleCV. It's a Pythonic wrapper for OpenCV's Python bindings and a few more tools (it uses Numpy, Scipy and PIL):

from SimpleCV import *

camera = Camera()
image = camera.getImage()

image.save('test.JPG')



回答2:


I see this mistake over and over and over and over again: the CaptureFromCAM() call is failing, which means that QueryFrame() is failing as a consequence and returning NULL as image, causing SaveImage() to fail as well.

Two things that you need to take into consideration here:

1) your webcam might not be index 0 (try -1, or 1) 2) learn to code safely! Always check the return of the functions that are being called. This practice will save you a lot of time in the future:

 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)


来源:https://stackoverflow.com/questions/8917767/error-while-trying-to-save-webcam-picture-with-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!