OpenCV 播放视频文件
OpenCV播放视频文件
使用cv::VideoCapture类来读取视频文件,并用cv::imshow逐帧播放视频。
版本一:使用c api
int playVideoTest(const std::string & videoPath )
{
CvCapture* capture;
capture = cvCreateFileCapture(videoPath.c_str());
if (!capture)
return -100;
double rate= (double)cvGetCaptureProperty( capture , CV_CAP_PROP_FPS);
bool stop(false);
IplImage* frame = NULL;
cvNamedWindow("Extracted Frame");
int delay= 1000/rate;
while (!stop) {
frame = cvQueryFrame(capture);
if (!frame)
break;
cvShowImage("Extracted Frame",frame);
if (cvWaitKey(delay)>=0)
stop= true;
}
cvDestroyWindow("Extracted Frame");
cvReleaseCapture(&capture);
return 1;
}
注:不知道如何释放frame空间,使用cvReleaseImage()和cvReleaseImageHeader()均会出错。
版本二:使用c++ api
int playVideo( const std::string & videoPath )
{
cv::VideoCapture capture(videoPath)
if (!capture.isOpened())
return -1;
double rate= capture.get(CV_CAP_PROP_FPS);
bool stop(false);
cv::Mat frame;
cv::namedWindow("Extracted Frame");
int delay= static_cast<int>(1000/rate);
while (!stop) {
if (!capture.read(frame))
break;
cv::imshow("Extracted Frame",frame);
if (cv::waitKey(delay)>=0)
stop= true;
}
cv::destroyWindow("Extracted Frame");
capture.release();
return 1;
}