ubuntu下基于qt+opencv控制摄像头

本文介绍如何使用Qt和OpenCV在Ubuntu环境下实现摄像头图像的捕捉与显示。通过设置定时器每30毫秒从摄像头获取一帧图像,并将OpenCV的IplImage格式转换为Qt的QImage格式以便于显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文博客链接: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

#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"); }

效果图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值