2012-02-25 76 views
0

我試圖在相機捕獲的臉上實現人臉識別。爲了製作臉部數據集,我需要存儲單個人的多個圖像。爲此,我需要從相機抓取多個幀並保存它們。如何從相機抓取多幀?

#include"cv.h" 
#include"highgui.h" 
#include <iostream> 
using namespace cv; 
// Grab the next camera frame. Waits until the next frame is ready, and 
// provides direct access to it, so do NOT modify or free the returned image! 
// Will automatically initialize the camera on the first frame. 
#include"highgui.h" 
#include<iostream> 

using namespace cv; 
IplImage* getCameraFrame(CvCapture* &camera); 
int main() 
{ 
    CvCapture* camera = 0; // The camera device. 
    while (cvWaitKey(10) != 27) 
    { // Quit on "Escape" key. 
     IplImage *frame = getCameraFrame(camera); 
     cvSaveImage("Frame:",frame); 
    } 
    return 0; 
} 
IplImage* getCameraFrame(CvCapture* &camera) 
{ 
    IplImage *frame; 
    int w, h; 

    // If the camera hasn't been initialized, then open it. 
    if (!camera) { 
     printf("Acessing the camera ...\n"); 
     camera = cvCreateCameraCapture(0); 
     if (!camera) { 
      printf("Couldn't access the camera.\n"); 
      exit(1); 
     } 
     // Try to set the camera resolution to 320 x 240. 
     cvSetCaptureProperty(camera, CV_CAP_PROP_FRAME_WIDTH, 320); 
     cvSetCaptureProperty(camera, CV_CAP_PROP_FRAME_HEIGHT, 240); 
     // Get the first frame, to make sure the camera is initialized. 
     frame = cvQueryFrame(camera); 
     if (frame) { 
      w = frame->width; 
      h = frame->height; 
      printf("Got the camera at %dx%d resolution.\n", w, h); 
     } 
     // Wait a little, so that the camera can auto-adjust its brightness. 
     Sleep(1000); // (in milliseconds) 
    } 

    // Wait until the next camera frame is ready, then grab it. 
    frame = cvQueryFrame(camera); 
    if (!frame) { 
     printf("Couldn't grab a camera frame.\n"); 
     exit(1); 
    } 
    return frame; 
} 

但cvSaveImage我需要給一個名稱圖像是saved.the名稱應該是唯一的,否則多幀只1image覆蓋。 我該如何解決這個問題?

+0

前段時間有人提出了[類似問題](http://stackoverflow.com/q/4350698/176769)。然後,我分享了[如何通過添加一個計數器變量並將它的值附加到文件的名稱](http://stackoverflow.com/a/4353483/176769)在磁盤中存儲一系列幀。例如:frame_1.jpg,frame_2.jpg,frame_3.jpg等 – karlphillip 2012-02-25 20:23:10

+0

不要將第一個參數硬編碼到cvSaveImage。使用sprintf()生成文件名。 – 2012-02-25 20:28:14

+0

@ karlphillip-感謝您的意見和鏈接。 – 2012-02-26 11:42:02

回答

1

你可以使用一個整型變量在getNextName()功能計數序列

int counter = 0; 
char *filename = "Photo" 

while (cvWaitKey(10) != 27) 
{ // Quit on "Escape" key. 
    IplImage *frame = getCameraFrame(camera); 

    counter++; 
    filename = getNextName(counter); 
    cvSaveImage(filename ,frame); 
} 

你可以做一個整數轉換和字符串連接.jpg

char* getNextName(int counter) 
{ 
    char buf[5]; 
    char *filename = "Photo"; 

    // convert 123 to string [buf] 
    itoa(counter, buf, 10); // int to char 
    strcat(filename, buf);  // concat "Photo" + "1" = "Photo1" 
    strcat(filename, ".jpg"); // concat "Photo1" + ".jpg" = "Photo1.jpg" 

    return filename; 
} 

它會自動保存您的圖像使用一個新的名稱與序列號。

+0

@ Nikson-thanks.will試試這段代碼並讓你知道。 – 2012-02-26 11:43:05

+0

@ Nikson-Thanks.But有一些問題。它給予未處理的內存異常。我認爲文件名應該是一個數組,因此它有空間拼接.N然後我無法修改您的getNextName函數。 – 2012-03-20 18:23:45

+0

@Abhishekkumar爲我工作。你應該檢查你的幀是否爲空。當你試圖保存一些空幀/指針時,你可能會得到這個錯誤 – 2012-04-11 06:10:11

0

你可以簡單地爲我們ffmpeg。 下面的代碼將變成一個視頻X圖像

ffmpeg -i video.mpg image%d.jpg 
+0

我有一種感覺,他的問題是特定於OpenCV和Webcams。 – karlphillip 2012-02-26 01:25:27