【问题标题】:How to grab multiple frames from camera?如何从相机中抓取多个帧?
【发布时间】:2012-03-15 20:10:51
【问题描述】:

我正在尝试在相机捕获的人脸上实现人脸识别。为了制作人脸数据集,我需要存储多个单人图像。为此,我需要从相机中抓取多个帧并保存。

#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 个图像。 我该如何解决这个问题?

【问题讨论】:

  • 前段时间有人提出了similar problem。然后,我分享了@98​​7654322@。例如:frame_1.jpg、frame_2.jpg、frame_3.jpg 等
  • 不要将第一个参数硬编码到 cvSaveImage。使用 sprintf() 生成文件名。
  • @karlphillip-感谢您的 cmets 和链接。
  • @karlphillip-谢谢你的回答。

标签: c opencv camera face-recognition


【解决方案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()函数中可以进行整数转换和字符串concat.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;
} 

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

【讨论】:

  • @Nikson-Thanks.But 有一些问题。它给出了未处理的内存异常。我认为文件名应该是一个数组,以便它有连接空间。N 然后我无法修改你的getNextName 函数。
  • @Abhishekkumar 它为我工作。你应该检查你的框架不为空。当您尝试保存一些空帧/指针时,您可能会收到此错误
【解决方案2】:

您可以简单地使用 ffmpeg。 以下代码会将视频转换为 X 图像

ffmpeg -i video.mpg image%d.jpg

【讨论】:

  • 我感觉他的问题是针对 OpenCV 和网络摄像头的。
猜你喜欢
  • 1970-01-01
  • 2013-09-29
  • 2023-04-06
  • 2014-06-02
  • 1970-01-01
  • 2022-06-12
  • 1970-01-01
  • 1970-01-01
  • 2011-09-17
相关资源
最近更新 更多