aihot  2017-04-30 23:29:53  OpenCV |   查看评论   

 OpenCV学习笔记(三十五)——用Qt做摄像头读取

再介绍一下我的开发环境Qt4.7.4+OpenCV2.3.1+VS2008,其实很简单,先在自己的QMainWindow子类里面声明如下变量:

  1. public:  
  2.     camCapture(QWidget *parent = 0, Qt::WFlags flags = 0);  
  3.     ~camCapture();  
  4. protected:  
  5.     void paintEvent(QPaintEvent * e);  
  6.   
  7. private:  
  8.     Ui::camCaptureClass ui;  
  9.     cv::Mat frame;  
  10.     cv::VideoCapture capture;  
  11.     QImage *image;  
  12.     QTimer *timer;  
  13.   
  14. private slots:  
  15.     void nextFrame();  


paintEvent函数是重载的,目的是为了更新绘图,在其定义中添加:

  1. void camCapture::paintEvent(QPaintEvent * e)  
  2. {  
  3.     // 更新图像  
  4.     QPainter painter(this);  
  5.     painter.drawImage(QPoint(0, 12), *image);  
  6. }  


camCapture的构造函数里面添加如下初始化:

  1. // 初始化处理,建立QImage和frame的关联,开启定时器  
  2.     capture.open(-1);  
  3.     if (capture.isOpened())  
  4.     {  
  5.         capture >> frame;  
  6.         if (!frame.empty())  
  7.         {  
  8.             cv::cvtColor(frame, frame, CV_BGR2RGB);  
  9.             cv::flip(frame, frame, 1);  
  10.             image = new QImage((const unsigned char*)(frame.data), frame.cols, frame.rows, QImage::Format_RGB888);  
  11.             timer = new QTimer(this);  
  12.             timer->setInterval(30);  
  13.             connect(timer, SIGNAL(timeout()), this, SLOT(nextFrame()));  
 

除特别注明外,本站所有文章均为 赢咖4注册 原创,转载请注明出处来自OpenCV学习笔记(三十五)——用Qt做摄像头读取

留言与评论(共有 0 条评论)
   
验证码:
[lianlun]1[/lianlun]