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

301 篇文章 0 订阅
271 篇文章 2 订阅

作者:jdh99

转自:http://blog.csdn.net/jdh99/article/details/6671737


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

  1. <span style="font-family:'Arial Black';">#ifndef WIDGET_H  
  2. #define WIDGET_H  
  3.   
  4. #include <QWidget>  
  5. #include "iostream"  
  6. #include "stdio.h"  
  7.   
  8. #include "highgui.h"  
  9. #include "cv.h"  
  10. #include <QTimer>  
  11. #include <QImage>  
  12. #include <QPainter>  
  13.   
  14. using namespace std;  
  15.   
  16. #define TIME_OUT    30                  //视频播放间隔时间  
  17. #define FPS         30                  //播放帧率  
  18.   
  19. namespace Ui {  
  20.     class Widget;  
  21. }  
  22.   
  23. class Widget : public QWidget  
  24. {  
  25.     Q_OBJECT  
  26.   
  27. public:  
  28.     explicit Widget(QWidget *parent = 0);  
  29.     ~Widget();  
  30.   
  31. private:  
  32.     Ui::Widget *ui;  
  33.   
  34.     CvCapture *capture;             //视频数据结构  
  35.     IplImage *frame;  
  36.     QTimer *timer;  
  37.     QImage *img;  
  38.   
  39. private slots:  
  40.     void slot_timer();  
  41.   
  42. protected:  
  43.     void paintEvent (QPaintEvent *);  
  44. };  
  45.   
  46. #endif // WIDGET_H  
  47. </span>  

widget.c

  1. <span style="font-family:'Arial Black';">#include "widget.h"  
  2. #include "ui_widget.h"  
  3.   
  4. Widget::Widget(QWidget *parent) :  
  5.     QWidget(parent),  
  6.     ui(new Ui::Widget)  
  7. {  
  8.     ui->setupUi(this);  
  9.   
  10.     timer = new QTimer(this);  
  11.     connect(timer,SIGNAL(timeout()),this,SLOT(slot_timer()));  
  12.     timer->start(FPS);  
  13.   
  14.     capture = cvCreateCameraCapture(0);  
  15.     //cvNamedWindow("jdh",CV_WINDOW_AUTOSIZE);  
  16. }  
  17.   
  18. void Widget::slot_timer()  
  19. {  
  20.     frame = cvQueryFrame(capture);  
  21.     if (!frame)  
  22.     {  
  23.         return;  
  24.     }  
  25.   
  26.     //img->load("test.jpg");  
  27.     cvCvtColor(frame,frame,CV_BGR2RGB);  
  28.     img = new QImage((unsigned char*)frame->imageData,frame->width,frame->height,frame->widthStep,QImage::Format_RGB888);  
  29.     //img = new QImage((unsigned char*)frame->imageData,frame->width,frame->height,QImage::Format_RGB888);  
  30.     update();  
  31.     //cvShowImage("jdh",frame);  
  32. }  
  33.   
  34. void Widget::paintEvent(QPaintEvent * event)  
  35. {  
  36.     //painter->drawImage(0,0,mm);  
  37.     QPainter *pp = new QPainter(this);  
  38.     pp->drawImage(0,0,*img);  
  39. }  
  40.   
  41. Widget::~Widget()  
  42. {  
  43.     delete ui;  
  44.   
  45.     cvReleaseImage(&frame);  
  46.     //cvDestroyWindow("jdh");  
  47. }  
  48. </span>  

效果图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值