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

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

还有一个button名字是pushButton

mainwindow.h

#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

#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));
    }
}

你可以使用QtOpenCV调用摄像头。以下是一个简单的示例代码,演示如何使用QtOpenCV进行摄像头捕捉: ```cpp #include <QApplication> #include <QMainWindow> #include <QVBoxLayout> #include <QLabel> #include <QTimer> #include <opencv2/opencv.hpp> using namespace cv; class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr) : QMainWindow(parent) { // 创建布局和标签 QVBoxLayout *layout = new QVBoxLayout(); QLabel *label = new QLabel(this); layout->addWidget(label); // 设置布局为主窗口的中心部分 QWidget *centralWidget = new QWidget(this); centralWidget->setLayout(layout); setCentralWidget(centralWidget); // 创建定时器,用于定时更新摄像头QTimer *timer = new QTimer(this); connect(timer, &QTimer::timeout, this, &MainWindow::updateFrame); timer->start(30); // 设置时间间隔 // 打开摄像头 capture.open(0); if (!capture.isOpened()) { label->setText("无法打开摄像头"); return; } } ~MainWindow() { // 关闭摄像头 capture.release(); } private slots: void updateFrame() { Mat frame; capture >> frame; // 从摄像头获取一帧图像 // 将图像转换为Qt显示的格式 QImage image(frame.data, frame.cols, frame.rows, frame.step, QImage::Format_BGR888); // 调整图像大小以适应标签的大小 image = image.scaled(label->size(), Qt::KeepAspectRatio); // 在标签上显示图像 label->setPixmap(QPixmap::fromImage(image)); } private: VideoCapture capture; QLabel *label; }; int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); } ``` 这个示例代码创建了一个基于Qt的主窗口,该窗口使用OpenCV捕捉并显示摄像头的帧。注意,需要在项目中添加OpenCVQt的头文件和库。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

养牛大人

感谢您的鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值