2010-06-25 553 views
2

我是一般的编程新手,我正在从事一个需要从网络摄像头捕捉图像(可能使用OpenCV)的项目,并将图像保存为pgm文件。如何在OpenCV中捕捉图像并保存为pgm格式?

这样做最简单的方法是什么?柳树车库提供了图像拍摄这段代码:

http://opencv.willowgarage.com/wiki/CameraCapture

使用此代码作为基础,怎么可能我将它修改为:从活动凸轮

  1. 捕获图像每2秒
  2. 将图像保存为pgm格式的文件夹

非常感谢您提供的任何帮助!

回答

-1

我相信格式从文件名的扩展选择 - 所以假设你的OpenCV的lib的是针对相应的库,你可以这样做链接:(这是从内存,可能不正确。)

CvCapture* capture = cvCaptureFromCam(0); 
IplImage* frame = cvQueryFrame(capture); 
while (1) { 
    frame = cvQueryFrame(capture); 
    cvSaveImage("foo.pgm", &frame); 
    sleep(2); 
} 
cvReleaseImage(&frame); 
cvReleaseCapture(&capture); 
-1

如果您不需要superaccurate2秒然后简单地把睡眠(2),或在一段时间(1)循环每个抓取之前等待来回2秒睡眠(2000年),

写图像与cvSaveImage()只需将扩展名.pgm放在文件名上,它就会使用pgm。

5

首先,请使用较新的网站 - opencv.org。当新用户看到旧引用,阅读旧文档并重新发布旧链接时,使用过时引用会产生链式效应。

实际上没有理由使用旧的C API。相反,您可以使用较新的C++界面,其中包括优雅地处理视频捕捉。这里的缩短例如从文档的版本上VideoCapture

#include "opencv2/opencv.hpp" 

using namespace cv; 

int main(int, char**) 
{ 
    VideoCapture cap(0); // open the default camera 
    if(!cap.isOpened()) // check if we succeeded 
     return -1; 

    for(;;) 
    { 
     Mat frame; 
     cap >> frame; // get a new frame from camera 
     // do any processing 
     imwrite("path/to/image.png", frame); 
     if(waitKey(30) >= 0) break; // you can increase delay to 2 seconds here 
    } 
    // the camera will be deinitialized automatically in VideoCapture destructor 
    return 0; 
} 

另外,如果您是新的节目,可以考虑使用Python接口到OpenCV的 - CV2模块。 Python通常被认为比C++更简单,使用它可以在交互式控制台中使用OpenCV功能。拍摄有cv2看起来是这样的(从here采用的代码):

import numpy as np 
import cv2 

cap = cv2.VideoCapture(0) 

while(True): 
    # Capture frame-by-frame 
    ret, frame = cap.read() 
    # do what you want with frame 
    # and then save to file 
    cv2.imwrite('path/to/image.png', frame) 
    if cv2.waitKey(30) & 0xFF == ord('q'): # you can increase delay to 2 seconds here 
     break 

# When everything done, release the capture 
cap.release() 
cv2.destroyAllWindows() 
2

由于ffriend的答案只有部分正确的,我会多一些添加到它(在C++)。此问题的作者明确要求导出为PGM(PXM文件格式,每个像素存储8位),而不是PNG(如朋友在他/她的回复中所述)。这里的主要问题是,official documentation for imwrite是omho不清楚这件事情在所有:

对于PPM,PGM,或PBM,它可以是一个二进制格式标志(CV_IMWRITE_PXM_BINARY),0或1默认值为1 。

如果我们读普通的英语句子,我们有一个选项列表:CV_IMWRITE_PXM_BINARY,0或1。没有提及那些可以并且实际上应该被组合!我曾尝试一点点(我也需要存储8位图像为我的项目),最后到了所需的解决方案:

std::vector<int> compression_params; // Stores the compression parameters 

compression_params.push_back(CV_IMWRITE_PXM_BINARY); // Set to PXM compression 
compression_params.push_back(0); // Set type of PXM in our case PGM 
const std::string imageFilename = "myPGM.pgm"; // Some file name - C++ requires an std::string 

cv::imwrite(imageFilename, myImageMatrix, compression_params); // Write matrix to file 

我的调查也被this问题在这里笔者为(也许燃料仍然是)在同一个问题上苦苦挣扎,同时还有一些关于PXM格式的基本信息,您可以在这里找到here

结果(仅图像的一部分)显示如下:

P2 
32 100 
255 
121 237 26 102 88 143 67 224 160 164 238 8 119 195 138 16 176 244 72 106 72 211 168 45 250 161 37 1 96 130 74 8 
126 122 227 86 106 120 102 150 185 218 164 232 111 230 207 191 39 222 236 78 137 71 174 96 146 122 117 175 34 245 6 125 
124 121 241 67 225 203 118 209 227 168 175 40 90 19 197 190 40 254 68 90 88 242 136 32 123 201 37 35 99 179 198 163 
97 161 129 35 48 140 234 237 98 73 105 77 211 234 88 176 152 12 68 93 159 184 238 5 172 252 38 68 200 130 194 216 
160 188 21 53 16 79 71 54 124 234 34 92 246 49 0 17 145 102 72 42 105 252 81 63 161 146 81 16 72 104 66 41 
189 100 252 37 13 91 71 40 123 246 33 157 67 96 71 59 17 196 96 110 109 116 253 30 42 203 69 53 97 188 90 68 
101 36 84 5 41 59 80 8 107 160 168 9 194 8 71 55 152 132 232 102 12 96 213 24 134 208 1 55 64 43 74 22 
92 77 30 44 139 96 70 152 160 146 142 8 87 243 11 91 49 196 104 250 72 67 159 44 240 225 69 29 34 115 42 2 
109 176 145 90 137 172 65 25 162 57 169 92 214 211 72 94 149 20 104 56 27 67 218 17 203 182 5 124 138 2 130 48 
121 225 25 106 89 76 69 189 34 25 173 8 114 83 72 52 145 154 64 40 91 2 251 53 251 237 20 124 82 2 194 42 ... 

这恰恰是在这种情况下需要。您可以在顶部看到“P2”标记,值也清楚地显示在0到255之间,这正好是每个像素8位。

+0

我该如何改变它以保存为PPM图像?我似乎无法找到第二个int推送到您的阵列的文档代表 – 2014-07-23 19:26:07

+0

尝试组合文档中提到的参数。除此之外,唯一可以找到OpenCV解决方案的其他方法是直接查看OpenCV的源代码,但我真的希望事情不会那么严重。您也可以编写自己的imwrite()版本并将其包含在源代码中。正如你所看到的,PGM非常简单直观,如果有必要,我认为编写PPM导出功能并不那么困难。还要注意,文件扩展名在OpenCV中很重要。 – rbaleksandar 2014-07-24 08:00:34

相关问题