ubuntu下基于qt+opencv控制摄像头
2011-08-09 10:55
453 查看
本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.
环境:ubuntu10.04 + opencv2.2.0 + qt4.7.0
opencv下控制摄像头是容易的,提供的highgui库调用linux本身的v4l2机制就能控制摄像头.在这里我与qt混合编程,在qt中开一个30ms的定时器,不断通过摄像头捕捉图像,这30ms就是帧速。
捕捉的图像在opencv中是IplImage类型,在qt中调用图像一般是QImage类型,所以需要进行一个格式转换,而且捕捉到的图像颜色是BGR,需要转换城RGB。摄像头捕捉的图像显示窗口为QWidget部件。
源代码:
widget.h
widget.c
效果图:
环境:ubuntu10.04 + opencv2.2.0 + qt4.7.0
opencv下控制摄像头是容易的,提供的highgui库调用linux本身的v4l2机制就能控制摄像头.在这里我与qt混合编程,在qt中开一个30ms的定时器,不断通过摄像头捕捉图像,这30ms就是帧速。
捕捉的图像在opencv中是IplImage类型,在qt中调用图像一般是QImage类型,所以需要进行一个格式转换,而且捕捉到的图像颜色是BGR,需要转换城RGB。摄像头捕捉的图像显示窗口为QWidget部件。
源代码:
widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include "iostream" #include "stdio.h" #include "highgui.h" #include "cv.h" #include <QTimer> #include <QImage> #include <QPainter> using namespace std; #define TIME_OUT 30 //视频播放间隔时间 #define FPS 30 //播放帧率 namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); private: Ui::Widget *ui; CvCapture *capture; //视频数据结构 IplImage *frame; QTimer *timer; QImage *img; private slots: void slot_timer(); protected: void paintEvent (QPaintEvent *); }; #endif // WIDGET_H
widget.c
#include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); timer = new QTimer(this); connect(timer,SIGNAL(timeout()),this,SLOT(slot_timer())); timer->start(FPS); capture = cvCreateCameraCapture(0); //cvNamedWindow("jdh",CV_WINDOW_AUTOSIZE); } void Widget::slot_timer() { frame = cvQueryFrame(capture); if (!frame) { return; } //img->load("test.jpg"); cvCvtColor(frame,frame,CV_BGR2RGB); img = new QImage((unsigned char*)frame->imageData,frame->width,frame->height,frame->widthStep,QImage::Format_RGB888); //img = new QImage((unsigned char*)frame->imageData,frame->width,frame->height,QImage::Format_RGB888); update(); //cvShowImage("jdh",frame); } void Widget::paintEvent(QPaintEvent * event) { //painter->drawImage(0,0,mm); QPainter *pp = new QPainter(this); pp->drawImage(0,0,*img); } Widget::~Widget() { delete ui; cvReleaseImage(&frame); //cvDestroyWindow("jdh"); }
效果图:
![](http://hi.csdn.net/attachment/201108/9/0_1312858210LNGO.gif)
相关文章推荐
- ubuntu下基于qt+opencv控制摄像头
- ubuntu下QT+OpenCV控制摄像头通过label 显示
- 虚拟机下打开摄像头(Linux ubuntu 2.6.32-122-rtai+QT+opencv)
- 基于opencv在摄像头ubuntu根据视频获取
- 基于OpenCV 、VS2008 MFC对话框的USB摄像头的控制和视频播放、跟踪(logitech sphere AF网络摄像头)
- 基于QT和opencv的摄像头(本地图片)读取并输出程序
- 基于opencv在摄像头ubuntu根据视频获取
- OpenCV学习笔记(6)基于 VC+OpenCV+DirectShow 的多个摄像头同步工作
- 在Ubuntu下的QT内使用Opencv
- 基于OpenCV打开摄像头的控制台程序
- 【opencv学习之六】opencv+Qt+Qzxing调取本地摄像头识别二维码
- 基于qt和opencv3实现机器学习之:利用svm(支持向量机)分类
- 基于Ubuntu14.04环境下编译qt-everywhere-opensource-src-5.6.1配置qt creater 的arm编译器环境并移植到tiny210v2开发板上
- ubuntu14.04 + Qt5.4 + opencv2.4.10
- Install opencv-2.4.7 in Ubuntu 11.10 with Qt support
- ubuntu下配置qt+opengl+opencv
- 使用OpenCV实现WebCam摄像头保存JPEG图片(改良3版) Ubuntu
- 基于QT和OpenCV的人脸检测识别系统(2)
- 11、新手入手树莓派教程--Qt中使用wiringPi来驱动RGB灯、使用opencv打开摄像头
- ubuntu, windows下安装opencv, 以及在qt,vs2010,dev,codeblock中的使用