Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting frame from video

#include "opencv2/opencv.hpp"
#pragma comment (lib , "opencv_core244d.lib")
#pragma comment (lib ,"opencv_highgui244d.lib")
#pragma comment(lib , "opencv_imgproc244d.lib")
int main(int argc, char* argv[])
{
    CvCapture* capture = cvCaptureFromFile("try.avi");

    IplImage* frame = NULL;
    do
    {
        frame = skipNFrames(capture, 1);
        cvNamedWindow("frame", CV_WINDOW_AUTOSIZE);
        cvShowImage("frame", frame);
        cvWaitKey(0);
    } while( frame != NULL );

    cvReleaseCapture(&capture);
    cvDestroyWindow("frame");
    cvReleaseImage(&frame);

    return 0;
}

This is my program to get frames from the video , but when i run this program , it works , it show me the video , but its not saving the frames automatically (without using any button or mouse) , which should save in my directory

like image 726
Rocket Avatar asked Jun 29 '26 05:06

Rocket


1 Answers

To save each frame individually,

  1. #include<stdio.h>

  2. Declare a global variable

    int flag=0;

  3. add following code just below to cvWaitKey(0) :

    char *str=new char[50];
    
    flag++;
    sprintf(str,"%d",flag);
    strcat(str," frame");
    strcat(str,".jpg");
    Mat image=frame;
    imwrite(str,image);
    
like image 85
Saikat Avatar answered Jul 02 '26 01:07

Saikat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!