C/C++ OpenCV读取视频与调用摄像头

原文:http://blog.csdn.net/qq78442761/article/details/54173104

 

OpenCV通过VideoCapture类,来对视频进行读取,调用摄像头

读取视频:

 

1.先实例化再初始化

VideoCapture capture;

Capture.open("1.avi");

2.实例化的同时进行初始化

VideoCapture capture("1.avi");

 

播放视频:

 

 

视频读如到VideoCapture类对象之后,用一个循环将每一帧显示出来

while(1)

{

Mat frame;

capture>>frame;

imshow("读取视频",frame);

waitkey(30);

}

 

调用摄像头

 

 

将代码VideoCapture capture("1.avi")中的1.avi换成0就可以了

 

 

 

 

 

下面来看一段代码:

 

[cpp] view plain copy
 
  1. #include <opencv2\opencv.hpp>  
  2. using namespace cv;  
  3. using namespace std;  
  4.   
  5. int main()  
  6. {  
  7.     //读取视频或摄像头  
  8.     VideoCapture capture("1.avi");  
  9.   
  10.     while (true)  
  11.     {  
  12.         Mat frame;  
  13.         capture >> frame;  
  14.         imshow("读取视频", frame);  
  15.         waitKey(30);    //延时30  
  16.     }  
  17.     return 0;  
这是读取文件然后进行播放:

 

下面是运行结果:


下面看看工程目录的图

下面是打开摄像头的代码:

 

[cpp] view plain copy
 
  1. #include <opencv2\opencv.hpp>  
  2. using namespace cv;  
  3. using namespace std;  
  4.   
  5. int main()  
  6. {  
  7.     //读取视频或摄像头  
  8.     VideoCapture capture(0);  
  9.   
  10.     while (true)  
  11.     {  
  12.         Mat frame;  
  13.         capture >> frame;  
  14.         imshow("读取视频", frame);  
  15.         waitKey(30);    //延时30  
  16.     }  
  17.     return 0;  
  18. }  
运行结果:

 

 

 
 
posted @ 2017-07-20 09:48  lizhigang  阅读(4618)  评论(2编辑  收藏  举报