Wednesday, July 2, 2008

Two little gems

Hi guys, So been quiet for some time now and I have returned with some goodies for you. First of all I have been munching away at two problems now and have come up some interesting stuff

First of all a video saga. I have been wanting to extract frames out of an avi file and save any frame that I was interested in. For this we need to be able to see the movie entirely and select a a frame to save as a jpg or bmp. I created a simple solution for this I created a window with a trackbar in it and then set the trackbar maximum value to the no of frames encountered. And then the user is able to select the frame he want using the slider. When he selects a frame he has the option of selecting between saving it and exiting the programme. If you press space at anypoint then the image is saved with the frame number as the image name. Or if you press enter the program exits.

I will post the code in here. Please note that there is some borrowed code in here for calculating the no frames in here. I have since forgotten where the code was, but all the credit to the original writer for that.

#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#include
#include

int frameNumber;
IplImage *capFrame;
CvCapture *myCap;
int nFrames;

void CV_CDECL mainTrackCallBack(int pos){

if((pos > 0) && (pos <>
cvSetCaptureProperty(myCap, CV_CAP_PROP_POS_FRAMES, pos);
capFrame = cvQueryFrame(myCap);
cvShowImage("CaptureFrames", capFrame);
frameNumber = pos;
}

}

int _tmain(int argc, _TCHAR* argv[])
{
using namespace std;

char tempSize[4];
std::ifstream videoFile(L"C:\\car.avi", ios::in|ios::binary);
videoFile.seekg( 0x30 , ios::beg );
videoFile.read( tempSize , 4 );
nFrames = (unsigned char ) tempSize[0] + 0x100*(unsigned char ) tempSize[1] + 0x10000*(unsigned char ) tempSize[2] + 0x1000000*(unsigned char ) tempSize[3];
videoFile.close();

myCap = cvCaptureFromAVI("C:\\car.avi");
cvNamedWindow("CaptureFrames");
cvCreateTrackbar("FrameNumber", "CaptureFrames", &frameNumber, nFrames, mainTrackCallBack);
capFrame = cvQueryFrame(myCap);
cvShowImage("CaptureFrames", capFrame);
char fileName[100];
int keyPress;
frameNumber = 0;
while(1){
keyPress = cvWaitKey(500);
if(keyPress == 32){
_itoa_s(frameNumber, fileName, 200, 10);
int tempLen = strlen(fileName);
strcpy( fileName + tempLen, "Image.jpg");
cvSaveImage(fileName, capFrame);
}else if(keyPress == 13){
break;
}
}
return 0;
}

No comments: