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

在搭建好开发环境的前提下,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;
}


 

                
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值