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

发布于 2024-12-27 20:36:55 字数 537 浏览 2 评论 0原文

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
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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橙幽之幻 2025-01-03 20:36:55

使用 SimpleCV,省去急诊室的麻烦。它是 OpenCV 的 Python 绑定和其他一些工具的 Pythonic 包装器(它使用 Numpy、Scipy 和 PIL):

from SimpleCV import *

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

image.save('test.JPG')

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')
情绪失控 2025-01-03 20:36:55

我一遍又一遍地看到这个错误: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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文