Rocket
Rocket

Reputation: 553

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

Upvotes: 3

Views: 1692

Answers (4)

Saikat
Saikat

Reputation: 1219

To see each frame of the video individually use cvWaitKey(0). It shows current frame of the video and wait for a key press infinitely. So to see the next frame press a key.

Upvotes: 4

Saikat
Saikat

Reputation: 1219

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

Upvotes: 3

user1850156
user1850156

Reputation:

#include"stdafx.h"
#include<cv.h>
#include<highgui.h>
#include<cxcore.h>


int main(int argc, char* argv[]) {

int c=1;
IplImage* img=0;
char buffer[1000];
CvCapture* cv_cap = cvCaptureFromFile("try.avi");
cvNamedWindow("Video",CV_WINDOW_AUTOSIZE);
while(1) {

img=cvQueryFrame(cv_cap);
cvShowImage("Video",img);
sprintf(buffer,"D:/image%u.jpg",c);
cvSaveImage(buffer,img);
c++;
  if (cvWaitKey(100)== 27) break;
}

cvDestroyWindow("Video"); 
return 0;
}

Try this , this will work

Upvotes: 2

Adi Shavit
Adi Shavit

Reputation: 17295

You need to use cvSaveImage() to explicitly save each frame.
This should be done in your loop, wherever you want to save the frame.
Obviously, if you want to save each frame with a different name you have to generate different names for each call. @baban shows one way to do it.

Upvotes: 0

Related Questions