سؤال

أحاول تنفيذ التعرف على الوجوه على الوجوه التي تم التقاطها بالكاميرا. من أجل إنشاء مجموعة بيانات للوجه، أحتاج إلى تخزين صور متعددة لشخص واحد. ولهذا أحتاج إلى التقاط إطارات متعددة من الكاميرا وحفظها.

#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 أحتاج إلى إعطاء اسم للصورة المراد حفظها. يجب أن تكون الأسماء فريدة وإلا سيتم الكتابة فوق إطارات متعددة على صورة واحدة فقط.كيف يمكنني حل هذه المشكلة؟

هل كانت مفيدة؟

المحلول

يمكنك استخدام متغير عدد صحيح لحساب التسلسل

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

سيتم حفظ صورتك تلقائيًا باستخدام اسم جديد بتسلسل رقمي.

نصائح أخرى

يمكنك ببساطة بنا ffmpeg لذلك. سيتم إيقاف التعليمة البرمجية التالية مقطع فيديو إلى X giveacodicetagpre.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top