OpenCV在Qt中显示视频的两种方法


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

  1. #include <QtWidgets/QMainWindow>  
  2. #include <QPaintEvent>  
  3. #include <QTimer>  
  4. #include <QPainter>  
  5. #include <QPixmap>  
  6. #include <QLabel>  
  7. #include <QImage>  
  8. #include <opencv.hpp>  
  9. #include "ui_mainwindow.h"  
  10. using namespace cv;  
  11. class mainwindow : public QMainWindow  
  12. {  
  13.     Q_OBJECT  
  14.   
  15. public:  
  16.     mainwindow(QWidget *parent = 0);  
  17.     ~mainwindow();  
  18. public slots:  
  19.     void updateImage();  
  20. private:  
  21.   
  22.     QTimer theTimer;  
  23.     Mat srcImage;  
  24.     VideoCapture videoCap;  
  25.     QLabel *imageLabel;  
  26.   
  27.     Ui::mainwindowClass ui;  
  28.   
  29. protected:  
  30.     void paintEvent(QPaintEvent *e);  
  31. };  
#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);
};
  1. mainwindow::mainwindow(QWidget *parent)  
  2.     : QMainWindow(parent)  
  3. {  
  4.     ui.setupUi(this);  
  5.     connect(&theTimer, &QTimer::timeout, this, &mainwindow::updateImage);  
  6.     //从摄像头捕获视频  
  7.     if(videoCap.open(0))  
  8.     {  
  9.         srcImage = Mat::zeros(videoCap.get(CV_CAP_PROP_FRAME_HEIGHT), videoCap.get(CV_CAP_PROP_FRAME_WIDTH), CV_8UC3);  
  10.         theTimer.start(33);  
  11.     }  
  12.     //设置显示视频用的Label  
  13.     imageLabel = new QLabel(this);  
  14.     ui.verticalLayout->addWidget(imageLabel);  
  15. }  
  16.   
  17. mainwindow::~mainwindow()  
  18. {  
  19.       
  20. }  
  21.   
  22. void mainwindow::paintEvent(QPaintEvent *e)  
  23. {  
  24.     //显示方法一  
  25.     QPainter painter(this);  
  26.     QImage image1 = QImage((uchar*)(srcImage.data), srcImage.cols, srcImage.rows, QImage::Format_RGB888);  
  27.     painter.drawImage(QPoint(20,20), image1);  
  28.     //显示方法二  
  29.     QImage image2 = QImage((uchar*)(srcImage.data), srcImage.cols, srcImage.rows, QImage::Format_RGB888);  
  30.     imageLabel->setPixmap(QPixmap::fromImage(image2));  
  31.     imageLabel->resize(image2.size());  
  32.     imageLabel->show();  
  33. }  
  34.   
  35. void mainwindow::updateImage()  
  36. {  
  37.     videoCap>>srcImage;  
  38.     if(srcImage.data)  
  39.     {  
  40.         cvtColor(srcImage, srcImage, CV_BGR2RGB);//Qt中支持的是RGB图像, OpenCV中支持的是BGR  
  41.         this->update();  //发送刷新消息  
  42.     }  
  43. }  
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();	//发送刷新消息
	}
}

演示结果

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM