码迷,mamicode.com
首页 > 其他好文 > 详细

qt上用opencv显示摄像头视频

时间:2016-02-29 21:38:14      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:

参考:http://blog.csdn.net/augusdi/article/details/8865541

代码如下:

注意,要在ui界面上放置一个“Vertical Layout”控件,调整到合适大小


#include <QtWidgets/QMainWindow> #include <QPaintEvent> #include <QTimer> #include <QPainter> #include <QPixmap> #include <QLabel> #include <QImage> #include <opencv.hpp> #include "ui_mainwindow.h" using namespace cv; class mainwindow : public QMainWindow { Q_OBJECT public: mainwindow(QWidget *parent = 0); ~mainwindow(); public slots: void updateImage(); private: QTimer theTimer; Mat srcImage; VideoCapture videoCap; QLabel *imageLabel; Ui::mainwindowClass ui; protected: void paintEvent(QPaintEvent *e); };

  

mainwindow::mainwindow(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);
	connect(&theTimer, &QTimer::timeout, this, &mainwindow::updateImage);
	//从摄像头捕获视频
	if(videoCap.open(0))
	{
		srcImage = Mat::zeros(videoCap.get(CV_CAP_PROP_FRAME_HEIGHT), videoCap.get(CV_CAP_PROP_FRAME_WIDTH), CV_8UC3);
		theTimer.start(33);
	}
	//设置显示视频用的Label
	imageLabel = new QLabel(this);
	ui.verticalLayout->addWidget(imageLabel);
}

mainwindow::~mainwindow()
{
	
}

void mainwindow::paintEvent(QPaintEvent *e)
{
	//显示方法一
	QPainter painter(this);
	QImage image1 = QImage((uchar*)(srcImage.data), srcImage.cols, srcImage.rows, QImage::Format_RGB888);
	painter.drawImage(QPoint(20,20), image1);
	//显示方法二
	QImage image2 = QImage((uchar*)(srcImage.data), srcImage.cols, srcImage.rows, QImage::Format_RGB888);
	imageLabel->setPixmap(QPixmap::fromImage(image2));
	imageLabel->resize(image2.size());
	imageLabel->show();
}

void mainwindow::updateImage()
{
	videoCap>>srcImage;
	if(srcImage.data)
	{
		cvtColor(srcImage, srcImage, CV_BGR2RGB);//Qt中支持的是RGB图像, OpenCV中支持的是BGR
		this->update();	//发送刷新消息
	}
}

  演示结果:

技术分享

qt上用opencv显示摄像头视频

标签:

原文地址:http://www.cnblogs.com/LaplaceAkuir/p/5228805.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!