我試圖在相機捕獲的臉上實現人臉識別。爲了製作臉部數據集,我需要存儲單個人的多個圖像。爲此,我需要從相機抓取多個幀並保存它們。如何從相機抓取多幀?
#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覆蓋。 我該如何解決這個問題?
前段時間有人提出了[類似問題](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
不要將第一個參數硬編碼到cvSaveImage。使用sprintf()生成文件名。 – 2012-02-25 20:28:14
@ karlphillip-感謝您的意見和鏈接。 – 2012-02-26 11:42:02