c++ - OpenCV videoInput.h Capture speed different than write speed -
i trying write recording software write video stream coming in computer avi file. using opencv , accompanied videoinput.h handle directshow code. in case matters using visual studio 2010 compiler.
the problem having recorded file plays faster previewed file. not alot, enough noticable. example: 10 second preview 7 second file moves little bit fast.
if( bwritevideo ) { writer=cvcreatevideowriter(szfilename,cv_fourcc('d','i','v','x'), fps,cvsize(width, height),iscolor); if( !writer ) return 1; } if( bdisplayvideo ) cvnamedwindow("video", 0); while( key != 'q') { if(vi.isframenew(nsource)) { vi.getpixels(nsource, yourbuffer1, false, true); frame = cvcreateimage(cvsize(width, height), ipl_depth_8u, 3); frame->imagedata = (char*)yourbuffer1; frame->imagedataorigin = frame->imagedata; if( !frame ) break; // display image screen if( bdisplayvideo ) cvshowimage( "video" , frame ); if( bwritevideo ) cvwriteframe( writer, frame ); } key = cvwaitkey ( 1000 / fps ); }
let's suppose frames coming in 30 fps. code following:
- if new frame arrived, save , wait 1000/fps ms (that's ~33.3 ms).
- if not... wait.
let's suppose saving frame takes 10ms. 1 loop-run takes either 33.3 ms (no incoming frame) or 44.3 ms (if had save one). aren't going save of them (sometimes we'll have 2 incoming frames in 44.3 ms period, save one).
so output video have less 30 frames per real second. if play 30 fps... faster reality!
so try avoid waiting lot @ end of loop. example, decreasing amount of time cvwaitkey calls take up. (for example, replacing 1000 100.) loop run several times every frame, waiting 3.3 milliseconds @ time, checking new frame (and if there one, saving it). that's 10 (saving) + 3.3 (waiting) ms in worst case, won't miss new frames during time.
Comments
Post a Comment