c - 如何从相机中抓取多个帧?

标签 c opencv camera face-recognition

我正在尝试对相机捕获的面部进行面部识别。为了制作面部数据集,我需要存储单个人的多个图像。为此,我需要从相机中抓取多个帧并保存它们。

#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 中,我需要为要保存的图像命名。名称应该是唯一的,否则多个帧只会覆盖 1 个图像。 我该如何解决这个问题?

最佳答案

您可以使用整数变量来计数序列

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);
}

getNextName()函数中,您可以进行整数转换和字符串连接.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;
} 

它将自动使用带有数字序列的新名称保存您的图像。

关于c - 如何从相机中抓取多个帧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9446636/

相关文章:

c - lwip ip_addr - 存储大小

c - 为什么 strdup 被认为是邪恶的

python-3.x - 使用 OpenCV 比较两个图像

android - LED闪光灯激活

Cython:使其他 Cython 模块可以访问外部 C 函数

c++ - snprintf 变量列表不会将枚举转换为 char*

javascript - 找不到模块'../build/Release/opencv4nodejs

python-2.7 - OpenCV cv2.matchTemplate崩溃:python.exe中0x74B5E4E4处未处理的异常:0xC0000005

android - 如何使用相机测量物体的高度、宽度和距离?

java - 安卓摄像头翻转