Created
October 25, 2013 18:44
-
-
Save lxc-xx/7159752 to your computer and use it in GitHub Desktop.
VideoCapture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <opencv2/core/core.hpp> | |
#include <opencv2/highgui/highgui.hpp> | |
#include <opencv2/imgproc/imgproc.hpp> | |
#include <iostream> | |
using namespace std; | |
using namespace cv; | |
int main() | |
{ | |
//Open video file | |
VideoCapture capture("D:/videos/PetsD2TeC2.avi"); | |
e | |
if(!capture.isOpened()) | |
cout<<"fail to open!"<<endl; | |
//get the number of total frames | |
long totalFrameNumber = capture.get(CV_CAP_PROP_FRAME_COUNT); | |
cout<<"整个视频共"<<totalFrameNumber<<"帧"<<endl; | |
//设置开始帧() | |
long frameToStart = 300; | |
capture.set( CV_CAP_PROP_POS_FRAMES,frameToStart); | |
cout<<"从第"<<frameToStart<<"帧开始读"<<endl; | |
//设置结束帧 | |
int frameToStop = 400; | |
if(frameToStop < frameToStart) | |
{ | |
cout<<"结束帧小于开始帧,程序错误,即将退出!"<<endl; | |
return -1; | |
} | |
else | |
{ | |
cout<<"结束帧为:第"<<frameToStop<<"帧"<<endl; | |
} | |
//获取帧率 | |
double rate = capture.get(CV_CAP_PROP_FPS); | |
cout<<"帧率为:"<<rate<<endl; | |
//定义一个用来控制读取视频循环结束的变量 | |
bool stop = false; | |
//承载每一帧的图像 | |
Mat frame; | |
//显示每一帧的窗口 | |
namedWindow("Extracted frame"); | |
//两帧间的间隔时间: | |
//int delay = 1000/rate; | |
int delay = 1000/rate; | |
//利用while循环读取帧 | |
//currentFrame是在循环体中控制读取到指定的帧后循环结束的变量 | |
long currentFrame = frameToStart; | |
//滤波器的核 | |
int kernel_size = 3; | |
Mat kernel = Mat::ones(kernel_size,kernel_size,CV_32F)/(float)(kernel_size*kernel_size); | |
while(!stop) | |
{ | |
//读取下一帧 | |
if(!capture.read(frame)) | |
{ | |
cout<<"读取视频失败"<<endl; | |
return -1; | |
} | |
//这里加滤波程序 | |
imshow("Extracted frame",frame); | |
filter2D(frame,frame,-1,kernel); | |
imshow("after filter",frame); | |
cout<<"正在读取第"<<currentFrame<<"帧"<<endl; | |
//waitKey(int delay=0)当delay ≤ 0时会永远等待;当delay>0时会等待delay毫秒 | |
//当时间结束前没有按键按下时,返回值为-1;否则返回按键 | |
int c = waitKey(delay); | |
//按下ESC或者到达指定的结束帧后退出读取视频 | |
if((char) c == 27 || currentFrame > frameToStop) | |
{ | |
stop = true; | |
} | |
//按下按键后会停留在当前帧,等待下一次按键 | |
if( c >= 0) | |
{ | |
waitKey(0); | |
} | |
currentFrame++; | |
} | |
//关闭视频文件 | |
capture.release(); | |
waitKey(0); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment