如何在 OpenCV 中捕获图像并以 pgm 格式保存?
我对一般编程是全新的,正在开发一个项目,我需要从网络摄像头捕获图像(可能使用 OpenCV),并将图像保存为 pgm 文件。
最简单的方法是什么? Willow Garage 提供了以下用于图像捕获的代码:
http://opencv.willowgarage.com/wiki/CameraCapture
使用此代码作为基础,我如何将其修改为:
- 每 2 秒从实时摄像头捕获图像
- ,将图像以 pgm 格式保存到文件夹中
非常感谢您提供的任何帮助!
I am brand new to programming in general, and am working on a project for which I need to capture images from my webcam (possibly using OpenCV), and save the images as pgm files.
What's the simplest way to do this? Willow Garage provides this code for image capturing:
http://opencv.willowgarage.com/wiki/CameraCapture
Using this code as a base, how might I modify it to:
- capture an image from the live cam every 2 seconds
- save the images to a folder in pgm format
Thanks so much for any help you can provide!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先,请使用较新的网站 - opencv.org。使用过时的引用会导致连锁效应,当新用户看到旧的引用时,会阅读旧的文档并再次发布旧的链接。
实际上没有理由使用旧的 C API。相反,您可以使用较新的 C++ 接口,它可以优雅地处理视频捕获。以下是 VideoCapture 上文档的示例的简短版本:
另外,如果您是编程新手,请考虑使用 OpenCV 的 Python 接口 - cv2 模块。 Python 通常被认为比 C++ 更简单,使用它您可以直接在交互式控制台中使用 OpenCV 函数。使用
cv2
捕获视频看起来像这样(采用的代码来自 此处):First of all, please use newer site - opencv.org. Using outdated references leads to chain effect, when new users see old references, read old docs and post old links again.
There's actually no reason to use old C API. Instead, you can use newer C++ interface, which, among other things, handles capturing video gracefully. Here's shortened version of example from docs on VideoCapture:
Also, if you are new to programming, consider using Python interface to OpenCV - cv2 module. Python is often considered simpler than C++, and using it you can play around with OpenCV functions right in an interactive console. Capturing video with
cv2
looks something like this (adopted code from here):由于 ffriend 的答案仅部分正确,我将添加更多内容(用 C++ 编写)。这个问题的作者明确要求导出为 PGM(以 8 位存储每个像素的 PXM 文件格式)而不是 PNG(正如 ffriend 在他/她的回复中描述的那样)。这里的主要问题是 imwrite 的官方文档 是omho 完全不清楚这个问题:
对于 PPM、PGM 或 PBM,它可以是二进制格式标志 ( CV_IMWRITE_PXM_BINARY )、0 或 1。默认值为 1。
如果我们阅读以下句子普通英语,我们有一个选项列表:CV_IMWRITE_PXM_BINARY, 0 或 1。没有提到这些可以而且实际上应该结合起来!我必须进行一些实验(我还需要为我的项目存储 8 位图像),最终得到了所需的解决方案:
我的调查也受到 这个问题,作者当时(也许仍然)正在努力解决同样的问题,并且还通过有关 PXM 格式的一些基本信息,您可以在此处找到。
结果(仅图像的一部分)如下所示:
这正是本例中所需要的。您可以看到顶部的“P2”标记,并且值也清楚地从 0 到 255,即每像素 8 位。
Since ffriend's answer is only partially true, I'll add some more to it (in C++). The author of this question asks explicitly for exporting to PGM (PXM file format that stores each pixel in 8 bits) and not PNG (as ffriend describes in his/her reply). The main issue here is that the official documentation for imwrite is omho not clear about this matter at all:
For PPM, PGM, or PBM, it can be a binary format flag ( CV_IMWRITE_PXM_BINARY ), 0 or 1. Default value is 1.
If we read the sentence in normal English, we have a list of options: CV_IMWRITE_PXM_BINARY, 0 or 1. There is no mention that those can and actually are supposed to be combined! I had to experiment a little bit (I also needed to store 8-bit images for my project) and finally got to the desired solution:
My investigation was also fueled by this question where the author was (maybe still is) struggling with the very same issue and also by some basic information on the PXM format, which you can find here.
The result (only part of the image) is displayed below:
Which is exactly what is required in this case. You can see the "P2" marking at the top and also the values are clearly from 0 to 255, which is exactly 8 bits per pixel.
阅读了大部分答案,但没有一个能满足我的要求。这是我的实现方式。
该程序将使用网络摄像头作为摄像头,并在您按“c”时单击图片 - 我们可以更改条件,然后使其在一定时间间隔后自动单击图片
Read most of the answers but none of them could satisfy my requirement. Here's how I implemented it.
This program will use webcam as a camera and clicks picture when you press 'c' - we can change the condition, then make it to click pictures automatically after certain interval
如果您不需要超精确的 2 秒,那么只需在 while(1) 循环中放置 sleep(2) 或 Sleep(2000) 即可在每次抓取之前等待 2 秒,
使用 cvSaveImage() 写入图像只需将扩展名 .pgm 放在文件名,它将使用 pgm。
If you don't need superaccurate 2seconds then simply put a sleep(2) or Sleep(2000) in the while(1) loop to wait fro 2seconds before each grab,
Write images with cvSaveImage() just put the extention .pgm on the filename and it will use pgm.
我相信格式是从文件名的扩展名中选择的 - 因此假设您的 opencv 库链接到适当的库,您可以执行以下操作:(这是从记忆中得出的,可能不正确。)
I believe that the format is chosen from the extension of the filename - so assuming your opencv lib's are linked against the appropriate libs you can do something like: (this is from memory, might not be correct.)