I’m trying to make a small program that will show a video from a file and save frames as individual images when you hold a key.
It seems to work fine except that the video freezes up while saving frames, which makes it really difficult to figure out when to stop saving frames. I’m wondering if anyone has an idea of how to avoid this problem.
This is the program as is:
#include "stdafx.h"
#include <cv.h>
#include <highgui.h>
#include <iostream>
int main( int argc, char** argv[] ) {
int cnt = 1;
char c;
cvNamedWindow( "Video", CV_WINDOW_AUTOSIZE );
CvCapture* capture = cvCreateFileCapture( "D:\\AVI Video\\SGVideo.mp4" );
IplImage* frame;
while(1) {
char num[40]="D:\\Bilder\\SGVSamples\\";
char str[10];
char pmp[10]=".jpg";
_itoa(cnt, str, 10);
strcat(num,str);
strcat(num,pmp);
frame = cvQueryFrame( capture );
if( !frame ) {
cout << "End of video!\n";
break;
}
cvShowImage( "Video", frame );
c = cvWaitKey(34);
// 'Space' -> Save frame :: 'Esc' -> Exit
if( c == 32 ) {
try {
cvSaveImage( num, frame );
cout << "Saved image: " << num <<" - Frame count: " << cnt << "\n";
} catch(...) {
cout << "Failed to save image!\n";
}
} else if( c == 27 ) {
cout << "Shutting down...";
break;
}
cnt = cnt+1;
}
cvReleaseCapture( &capture );
cvDestroyWindow( "Video" );
}
I’m fairly new to OpenCV and cpp in general, so any tips would be much appreciated!
You should run another thread to save images.
That block is making the freeze. If you had another thread which is only processing this block (when needed), video wouldn’t have been freezing.