您的位置:首页 > 编程语言 > Qt开发

Qt 5.3 下OpenCV 2.4.11 开发(2)摄像头采集

2015-07-29 10:42 483 查看
在搭建好开发环境的前提下,Qt下新建一个控制台应用程序,main.cpp代码如下
#include <QCoreApplication>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace cv;

int main()
{
bool stop = false;
Mat frame;
VideoCapture cap(0);

if(!cap.isOpened())
{
return -1;
}

while(!stop)
{
cap>>frame;
namedWindow("video",0);
imshow("video",frame);
if(waitKey(30) >=0)
stop = true;
}
return 0;
}
函数说明:
1、VideocCapture 用来采集摄像头或者视频文件中的图像信息,cap(0)构造函数初始化为第0个摄像头设备,假如有两个摄像头设备同时接入,则参数0和1都可以使用;isOpened()方法用于检测摄像头设备是否可以成功采集图像信息。
2、对于采集到的图像信息,我们以帧为单位显示在预览窗口,即将cap对象的瞬时帧采集输出到 Mat frame中,Mat类主要用来保存图片信息。
3、采集频率用延时函数 waitKey(x) 来控制,参数 x 为延时的时间,单位 ms,如果在此期间有按键按下,则立即结束并返回按下案件的ASCII码,否则返回 -1,如果 x=0 ,就无限等待下去,直到有按键按下。
若电脑有两个视频设备,同时采集,代码如下:
#include <QCoreApplication>#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <opencv2/imgproc/imgproc.hpp>using namespace cv;int main(){bool stop = false;Mat frame0;Mat frame1;VideoCapture cap0(0);VideoCapture cap1(1);if(!cap0.isOpened()&&!cap1.isOpened()){return -1;}while(!stop){cap0>>frame0;cap1>>frame1;namedWindow("video0",0);imshow("video0",frame0);namedWindow("video1",0);imshow("video1",frame1);if(waitKey(30) >=0)stop = true;}return 0;}
[/code]

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: