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));
    }
}
相关推荐
jndingxin13 小时前
OpenCV CUDA模块设备层-----反向二值化阈值处理函数thresh_binary_inv_func()
人工智能·opencv·计算机视觉
jndingxin14 小时前
OpenCV CUDA模块设备层-----在 GPU 上执行类似于 std::copy 的操作函数warpCopy()
人工智能·opencv·计算机视觉
晓131314 小时前
OpenCV篇——项目(二)OCR文档扫描
人工智能·python·opencv·pycharm·ocr
jndingxin14 小时前
OpenCV CUDA模块设备层-----在GPU 上高效地执行两个 uint 类型值的最大值比较函数vmax2()
人工智能·opencv·计算机视觉
Mr_Xuhhh18 小时前
网络基础(1)
c语言·开发语言·网络·c++·qt·算法
luofeiju20 小时前
RGB下的色彩变换:用线性代数解构色彩世界
图像处理·人工智能·opencv·线性代数
feiyangqingyun21 小时前
Qt音视频开发技巧/推流带旋转角度/rtsprtmp推流/保存文件到MP4/拉流解析旋转角度
qt·音视频·qt旋转角度推流
前端开发与ui设计的老司机1 天前
数字孪生技术为UI前端注入灵魂:实现产品全生命周期的可视化管理
前端·ui·命令模式
清醒的兰1 天前
Qt 基于TCP套接字编程
网络·qt·tcp
mahuifa10 天前
PySide环境配置及工具使用
python·qt·环境配置·开发经验·pyside