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));
    }
}
相关推荐
把我格子衫拿来2 小时前
VTK 9.5 与 Qt 6 联合开发:从嵌入三维窗口到可维护的桌面架构
c++·python·qt·vtk·三维可视化
晨间晓7 小时前
图片里的细小噪点怎么平滑?用华为云码道开发 OpenCV 图像滤波对比工具
人工智能·opencv·华为云
把我格子衫拿来8 小时前
VTK 与 Qt 的 CMake 工程怎么组织?模块、工具链与二进制兼容排错
c++·qt·vtk·cmake·工程实践
偷吃米饭11 小时前
图片特征怎么描述?用华为云码道开发 OpenCV ORB 特征提取工具
人工智能·opencv·华为云
代钦塔拉11 小时前
【C++/Qt 进阶】设计模式系列(4):原型模式——用“克隆“代替“重新构造“
c++·qt·设计模式
Python图像识别11 小时前
56-【2027毕设】YOLO11X光安检危险物品检测系统 - Python完整源码+PyQt5界面+训练模型+数据集
python·qt·课程设计
晨间晓12 小时前
图片里的关键点怎么找?用华为云码道开发 OpenCV FAST 特征点检测工具
人工智能·opencv·华为云
晨间晓12 小时前
二值图里的噪点怎么去掉?用华为云码道开发 OpenCV 形态学操作工具
人工智能·opencv·华为云
把我格子衫拿来14 小时前
鼠标点到哪里才算选中?Qt 与 VTK 的拾取坐标、DPI 与体素定位
python·qt·vtk·坐标变换·三维交互
晨间晓14 小时前
图片里的角点在哪里?用华为云码道开发 OpenCV Harris 角点检测工具
人工智能·opencv·华为云