【OpenCV+Qt】在Qt界面中显示OpenCV的视频或摄像头图像

您所在的位置:网站首页 opencv打开摄像头原理 【OpenCV+Qt】在Qt界面中显示OpenCV的视频或摄像头图像

【OpenCV+Qt】在Qt界面中显示OpenCV的视频或摄像头图像

2023-10-22 15:25| 来源: 网络整理| 查看: 265

文章目录 思路界面效果代码

思路

1.在传统OpenCV编程中,播放视频和摄像头图像时都需要使用waitKey()来实现延时。在Qt框架下,可以使用QTime类来代替,然后将其中的timeout()函数作为信号。 2.OpenCV的Mat类要转换成Qt的QImage,注意通道BGR(Mat)转换成RGB(QImage)。

界面

在这里插入图片描述 控件: 1.label:用于显示视频图像; 2.comboBox:用于选择视频输入还是摄像头输入; 3.pushButton(2):作为播放和停止按钮。

效果

视频输入: 在这里插入图片描述 摄像头输入: 在这里插入图片描述

代码

main.cpp

#include "videodisplayinqt.h" #include int main(int argc, char *argv[]) { QApplication a(argc, argv); videodisplayinqt w; w.show(); return a.exec(); }

videodisplayinqt.h

#pragma once #include #include "ui_videodisplayinqt.h" #include #include using namespace cv; class videodisplayinqt : public QMainWindow { Q_OBJECT public: videodisplayinqt(QWidget *parent = Q_NULLPTR); private: Ui::videodisplayinqtClass ui; VideoCapture capture; QTimer *timer; Mat frame; bool isCamera = 0; private slots: void on_comboBox_currentIndexChanged(); void importFrame(); void on_displayButton_clicked(); void on_stopButton_clicked(); };

videodisplayinqt.cpp

#include "videodisplayinqt.h" videodisplayinqt::videodisplayinqt(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(importFrame()));//import frame when timeout } void videodisplayinqt::on_comboBox_currentIndexChanged()//whether the camera import or not { if (ui.comboBox->currentIndex()) isCamera = 1; else isCamera = 0; } void videodisplayinqt::importFrame() { capture >> frame; cvtColor(frame, frame, CV_BGR2RGB);//only RGB of Qt QImage srcQImage = QImage((uchar*)(frame.data), frame.cols, frame.rows, QImage::Format_RGB888); ui.label->setPixmap(QPixmap::fromImage(srcQImage)); ui.label->resize(srcQImage.size()); ui.label->show(); } void videodisplayinqt::on_displayButton_clicked() { if (isCamera) { capture.open(0); } else { capture.open("01.mp4"); } timer->start(33);// Start timing, Signal out when timeout } void videodisplayinqt::on_stopButton_clicked() { timer->stop(); capture.release(); }

如有错误,欢迎指正!



【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3