如何在opencv中只保存视频中的两帧

时间:2014-02-25 07:11:16

标签: opencv

实际上我想使用opencv和C ++保存当前帧和前一帧,我在编辑后从互联网上获取此代码,这将把帧保存为当前帧。

int main()
{
 IplImage* currFrame = 0;
 IplImage* prevFrame = 0;
 CvCapture* cap = cvCaptureFromAVI("how.mp4");
 currFrame = cvQueryFrame( cap );
 char s [20];

  prevFrame  = cvCloneImage( currFrame );

  while(currFrame = cvQueryFrame( cap ))
 {

int num = 1;
cvShowImage( "DisplayVideo", currFrame );
sprintf(s,"pics/frame%d.jpg",num);
cvSaveImage(s,currFrame);
cvNamedWindow("image1");
cvShowImage("image1",currFrame);

cvCopy( currFrame , prevFrame);
num = 2;
sprintf(s,"pics/frame%d.jpg",num);
cvSaveImage(s,prevFrame);
cvNamedWindow("image2");
cvShowImage("image2",prevFrame);

char c = cvWaitKey(500); if( c == 27 ) break;
}
 cvReleaseCapture( &cap );
}

1 个答案:

答案 0 :(得分:0)

我得到了答案,这是代码

int main()
{
VideoCapture cap("how.mp4");
Mat curr, prev;
// if(!cap)return -1;
cap>>curr;
curr.copyTo(prev);
while(1)
{
  cap>>curr;
  imwrite("pics2/current.jpg",curr);
  imwrite("pics2/previous.jpg", prev);
  imshow("image1",curr);
  imshow("image2",prev);
  curr.copyTo(prev);
  if(waitKey(500)==27)break; //Esc pressed
}
}

感谢All,感谢您的回答和支持..