qt5.14.2 opencv调用摄像头显示在label

ui界面添加一个Qlabel名字是默认的label

还有一个button名字是pushButton

mainwindow.h

cpp 复制代码
#ifndef MAINWINDOW_H
#define MAINWINDOW_H


#include <QMainWindow>
#include <opencv2/opencv.hpp>  // 添加OpenCV头文件
#include <QTimer>              // 添加定时器头文件


QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE


class MainWindow : public QMainWindow
{
    Q_OBJECT


public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();


private slots:
    void on_pushButton_clicked();
    void updateFrame();  // 新增的帧更新槽函数


private:
    Ui::MainWindow *ui;
    cv::VideoCapture cap;  // OpenCV视频捕获对象
    QTimer *timer;         // 定时器对象
};
#endif // MAINWINDOW_H

mainwindow.cpp

cpp 复制代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <opencv2/opencv.hpp>
#include <QTimer>
#include <QImage>
#include <QPixmap>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);


    // 初始化定时器
    timer = new QTimer(this);


    // 连接信号和槽
    connect(timer, &QTimer::timeout, this, &MainWindow::updateFrame);


    // 设置Label的缩放策略
    ui->label->setScaledContents(true);
}


MainWindow::~MainWindow()
{
    // 释放资源
    if(cap.isOpened()) {
        cap.release();
    }
    if(timer->isActive()) {
        timer->stop();
    }
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    if (!timer->isActive()) {
        // 尝试打开摄像头
        cap.open(0); // 0表示默认摄像头


        if (!cap.isOpened()) {
            ui->label->setText("无法打开摄像头");
            return;
        }


        // 设置摄像头分辨率(可选)
        cap.set(cv::CAP_PROP_FRAME_WIDTH, 640);
        cap.set(cv::CAP_PROP_FRAME_HEIGHT, 480);


        timer->start(30); // 每30毫秒更新一帧
        ui->pushButton->setText("停止摄像头");
    } else {
        // 停止摄像头
        timer->stop();
        cap.release();
        ui->pushButton->setText("启动摄像头");
        ui->label->clear();
    }
}


void MainWindow::updateFrame()
{
    cv::Mat frame;
    cap >> frame; // 从摄像头获取一帧


    if (!frame.empty()) {
        // 将OpenCV的BGR格式转换为RGB
        cv::cvtColor(frame, frame, cv::COLOR_BGR2RGB);


        // 将cv::Mat转换为QImage
        QImage img(frame.data,
                  frame.cols,
                  frame.rows,
                  frame.step,
                  QImage::Format_RGB888);


        // 将QImage转换为QPixmap并显示在Label上
        ui->label->setPixmap(QPixmap::fromImage(img));
    }
}
相关推荐
R-G-B4 小时前
OpenCV Python——报错AttributeError: module ‘cv2‘ has no attribute ‘bgsegm‘,解决办法
人工智能·python·opencv·opencv python·attributeerror·module ‘cv2‘·no attribute
似乎很简单8 小时前
【opencv-Python学习笔记(5):几何变换】
笔记·opencv·学习
sqmeeting10 小时前
QT6 如何在Linux Wayland 桌面系统抓屏和分享屏幕
linux·qt
荼蘼12 小时前
OpenCv(二)——边界填充、阈值处理
人工智能·opencv·计算机视觉
姓刘的哦13 小时前
Win10上Qt使用Libcurl库
开发语言·qt
蜀中廖化20 小时前
机器学习:基于OpenCV和Python的智能图像处理 实战
python·opencv·机器学习
R-G-B1 天前
【P27 4-8】OpenCV Python——Mat类、深拷贝(clone、copyTo、copy)、浅拷贝,原理讲解与示例代码
人工智能·python·opencv·浅拷贝·深拷贝·opencv python·mat类
hellokandy1 天前
QT QVersionNumber 比较版本号大小
qt·版本号·qversionnumber
星期天要睡觉1 天前
计算机视觉(opencv)实战三——图像运算、cv2.add()、cv2.addWeighted()
人工智能·opencv·计算机视觉
常乐か1 天前
VS2022+QT5.15.2+OCCT7.9.1的开发环境搭建流程
开发语言·qt·opencascade