之前在QT creator上成功编译了opencv,由于课题需要,需要采集摄像头的信息。故搜集了网上的一些资料,依葫芦画瓢的照着做了一下,终于简单的成功采集了信息。

打开QTcreator,新建一个widget工程。

在界面上放两个label 分别用来显示摄像头采集到的数据和照的照片。

在widget.h中的源代码如下:

#ifndef WIDGET_H

#define WIDGET_H
#include <QWidget>
#include <QImage>
#include <QTimer> //设置采集数据的间隔时间
#include <highgui.h>
#include <cv.h>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
private slots:
    void openCamara(); //打开时摄像头
    void readFarme();  //读取当前帧信息
    void closeCamara();//关闭摄像头
    void takingPictures(); //拍照
private:
    Ui::Widget *ui;
   QTimer *timer;
    QImage *imag;
    CvCapture *cam;//视频获取结构,用来作为视频获取函数的一个参数
    IplImage *frame;//申请IplImage类型指针,就是申请内存空间来存放每一帧图像
};

#endif // WIDGET_H

在widget.cpp中的源文件如下:

#include "widget.h"

#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    cam=NULL;
    timer=new QTimer(this);
    imag=new QImage();
    connect(timer,SIGNAL(timeout()),this,SLOT(readFarme()));
    connect(ui->open,SIGNAL(clicked()),this,SLOT(openCamara()));
    connect(ui->pic,SIGNAL(clicked()),this,SLOT(takingPictures()));
    connect(ui->close,SIGNAL(clicked()),this,SLOT(closeCamara()));
}
Widget::~Widget()
{
    delete ui;
}
void Widget::openCamara()
{
    cam=cvCreateCameraCapture(0);
    timer->start(33);
}
void Widget::readFarme()
{
    frame=cvQueryFrame(cam);
  //  QImage imag((const uchar*)frame->imageData,frame->width,frame->height,QImage::Format_RGB888);
    QImage imag=QImage((const uchar*)frame->imageData,frame->width,frame->height,QImage::Format_RGB888).rgbSwapped();
    ui->label->setPixmap(QPixmap::fromImage(imag));
}
void Widget::closeCamara()
{
    timer->stop();
    cvReleaseCapture(&cam);//释放内存;
}

void Widget::takingPictures()
{
   frame = cvQueryFrame(cam);
   QImage imag((const uchar*)frame->imageData, frame->width, frame->height, QImage::Format_RGB888);
   ui->label_2->setPixmap(QPixmap::fromImage(imag));
}

好了,全部代码都OK了。

(大致思路是:点击打开摄像头按钮,则开启摄像头,并设置一个定时器,每隔一段时间捕获摄像头摄取到的帧,将其转换成QImage,最最后在QLabel控件上显示)

QT creator中使用opencv采集摄像头信息的更多相关文章

  1. Qt creator中配置opencv win7 64bit

    配置方法的原文来自https://www.librehat.com/qt-5-2-vs2012-opencv-2-4-7-dev-environment-setup-tutorial/. 补充,在张静 ...

  2. QT creator中使用opencv

    最近要用到opencv做图像方面的东西,网上很多是用VS加opencv,但自己对VS不怎么喜欢,想用QT Creator.在网上搜索了很多资料,终于花了一天的时间,在QT Creator上能使用ope ...

  3. 在Qt Creator中添加OpenCV库

    在项目的pro文件中添加如下代码:INCLUDEPATH += D:/opencv/build/include win32:CONFIG(debug, debug|release): {LIBS += ...

  4. Qt Creator中,include路径包含过程(或如何找到对应的头文件)

    Qt Creator中,include路径包含过程(或如何找到对应的头文件) 利用Qt Creator开发程序时,需要包含利用#include来添加头文件. 大家都知道,#include <&g ...

  5. Qt Creator中的3D绘图及动画教程(参照NeHe)

    Qt Creator中的3D绘图及动画教程(参照NeHe) http://blog.csdn.net/cly116/article/details/47184729 刚刚学习了Qt Creator,发 ...

  6. Windows XP 下如何使用Qt Creator中的Git版本控制功能

     原文地址:http://www.qtcn.org/bbs/simple/?t16960.html Qt Creator是针对Qt应用开发平台专门设计的IDE开发工具,集成了很多功能,分别有win ...

  7. qt creator中使用qwt插件

    前提:我用mingw编译的qwt. 将qwt插件集成到qt designer非常easy.仅仅要把qwt编译的qwt_designer_plugin.dll复制到C:\Qt\Qt5.3.1\5.3\m ...

  8. Qt在VS2013或Qt Creator 中的控制台输出方式设置

    首先值得注意的是:在写程序的时候,项目保存路径不要涉及到中文,否则容易出错! 一.Qt在VS2013中的控制台输出方式: 注意:这里是而不是Qt Application. 然后直接点击finish即可 ...

  9. qt creator中编辑Makefile的设置

    在qt creator中编辑Makefile时的Tab键总是不能识别,需要这样设置

随机推荐

  1. Host myCloudData.net on your own server (支持自建服务器)

    http://www.myclouddata.net/#/home Host myCloudData.net on your own serverUse the myCloudData.net SDK ...

  2. 虚拟机LUN扩大后,重新分区

    [root@ywcrmdb ~]# fdisk -l Disk /dev/sda: 751.6 GB, 751619276800 bytes 255 heads, 63 sectors/track, ...

  3. 32G sd卡格式化成fat

    fat文件系统格式只支持4G及以下的内存空间的存储设备,对于4G及以上的存储设备可以使用fat32或ntfs文件系统,建议使用ntfs文件系统,这样可以最有效地利用存储设备的存储空间,而且可以减少出现 ...

  4. 常用JS模板

    var _win, _doc, _stt, _do = document.domain, _arr = _do.split("."); function _st() { try { ...

  5. SEO_Alexa排名

    1.Alexa排名是评价某一网站访问量的一个指标,对SEO优化没有任何作用,这个数据是可以做假的,考虑Alexa排名纯粹是在浪费时间. 2.Alexa排名是针对已经在浏览器中安装了Alexa Tool ...

  6. Problem with generating association inside dbml file for LINQ to SQL

    Question: I have created a dbml file in my project, and then dragged two tables from a database into ...

  7. [Javascript] JSON.parse API

    JSON (JavaScript Object Notation) is a standard method to serialize JavaScript objects and is common ...

  8. Android实现获取本机中所有图片

    本示例演示如何在Android中使用加载器(Loader)来实现获取本机中的所有图片,并进行查看图片的效果. 在这个示例中,我使用android-support-v4.jar中的加载器(Loader) ...

  9. RHEL6.3进入单用户模式并重置root密码

    单用户模式类似于windows下的安全模式,允许root账号不输入密码直接启动并登录系统进行系统维护. 单用户模式只允许root账号登录,不允许其它用户使用ssh协议进行远程连接. 重启系统时按Ent ...

  10. ubuntu server 14.04.4 无线网卡没有启用,找不到wlan0端口

    Ubuntu Server默认的情况下是不会启用无线网卡的,想想实际服务器上怎么可能有无线网卡呢,呵呵.所以我们需要手动来启用无线网卡,难点就在这里了. 使用ifconfig命令,发现没有wlan口, ...