VS+QT+OpenCV+C++ マルチスレッド マルチカメラ ビデオ監視取得フォーム

注目のプログラム例

VS+QT+OpenCV マルチスレッド マルチカメラ ビデオ監視取得フォーム

動作環境のインストールやリモート デバッグが必要な場合は、記事の下部にある個人用QQ名刺を参照してください。専門および技術担当者がリモートでサポートします。

序文

このブログは、<<VS+QT+OpenCV マルチスレッド マルチカメラ ビデオ監視取得フォーム>> のコードを作成しています。コードはきちんとしていて、規則的で、読みやすいです。学習およびアプリケーションの推奨の最初の選択肢。

機能:マルチカメラビデオの非同期再生


記事ディレクトリ

1. 必要なツールソフトウェア

2. ステップを使用する

        1.ライブラリをインポートする

        2. コードの実装

        3. 走行結果

3. オンラインサポート

1. 必要なツールソフトウェア

1.VS、Qt

2.OpenCV

2. ステップを使用する

1.ライブラリをインポートする

#include "MainWindow.h"

#include<iostream>
#include <QThread>
#include <thread>
#include <chrono>
#include <QObject>
#include <QThread>
#include <QDebug>
#include <QTimer>
#include <QMutex>
#include <QWaitCondition>
#include<opencv2/opencv.hpp>
#include <QDebug>

2. コードの実装

コードは以下のように表示されます。

//.h文件
#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_MainWindow.h"

#include <mutex> 

class MainWindow : public QMainWindow
{
	Q_OBJECT

public:
	MainWindow(QWidget *parent = Q_NULLPTR);

private:
	Ui::MainWindowClass ui;
	
private slots:
	//void modelRun();
	void modelStop();
	void modelRun();
	void videoStart();
	void videoStop();
	void displayFrames();


};

//.cpp文件

MainWindow::MainWindow(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);

	QObject::connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(modelRun()));
    QObject::connect(ui.pushButton_2, SIGNAL(clicked()), this, SLOT(modelStop()));
    QObject::connect(ui.pushButton_3, SIGNAL(clicked()), this, SLOT(videoStart()));
    QObject::connect(ui.pushButton_4, SIGNAL(clicked()), this, SLOT(videoStop()));

}

ThreadOne threadObj;
std::thread thread1;
void MainWindow::modelRun()
{
    std::cout << "modelRun" << std::endl;
    QString buttonText = ui.pushButton->text();
    //std::string buttonText = buttonText2.toLocal8Bit().constData();
    //qDebug() << buttonText;

    if (buttonText == QStringLiteral("模型开始1"))
    {
        std::cout << "模型开始1" << std::endl;
        //ThreadOne threadObj;
        //std::thread thread1(&ThreadOne::sendRun, &threadObj);
        // 如果之前已经启动过线程,则先停止线程,并等待线程执行完毕
        //if (thread1.joinable()) {
        //    thread1.join();
        //}
        thread1 = std::thread(&ThreadOne::sendRun, &threadObj);
        // 不用等待线程执行完成,让它在后台运行
        thread1.detach();

        ui.pushButton->setText(QStringLiteral("暂停模型1"));
    }

    if (buttonText == QStringLiteral("暂停模型1"))
    {
        threadObj.pause();
        ui.pushButton->setText(QStringLiteral("恢复模型1"));
    }

    if (buttonText == QStringLiteral("恢复模型1"))
    {
        threadObj.resume();
        ui.pushButton->setText(QStringLiteral("暂停模型1"));
    }
}

void MainWindow::modelStop()
{
    // 停止远端发送帧线程的逻辑
    threadObj.stop();

    // 停止远端发送帧线程的逻辑
    //if (thread1.joinable()) {
    //    // 发送停止信号给线程
    //    threadObj.stop();
    //    // 等待线程执行完毕
    //    thread1.join();
    //}
}

QTimer* timer = new QTimer(nullptr);
void MainWindow::videoStart()
{
    std::cout << "modelRun" << std::endl;
    QString buttonText = ui.pushButton_3->text();
    //std::string buttonText = buttonText2.toLocal8Bit().constData();
    //qDebug() << buttonText;

    if (buttonText == QStringLiteral("开始1"))
    {
        std::cout << "开始1" << std::endl;
        // 创建Qtimer对象,并设置定时器间隔为1ms
        //QTimer* timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()), this, SLOT(displayFrames()));
        timer->start(100);
        ui.pushButton_3->setText(QStringLiteral("暂停1"));
    }

    if (buttonText == QStringLiteral("暂停1"))
    {
        timer->stop();
        ui.pushButton_3->setText(QStringLiteral("恢复1"));
    }

    if (buttonText == QStringLiteral("恢复1"))
    {
        timer->start(100);
        ui.pushButton_3->setText(QStringLiteral("暂停1"));
    }
}

void MainWindow::videoStop()
{
    // 停止本地接收帧线程的逻辑
    timer->stop();
}


void MainWindow::displayFrames()
{
    std::cout << "test2" << std::endl;

    // 接收帧并显示的逻辑
    if (!frame.empty())
    {
        //std::cout << "frame: " << frame << std::endl;
        //ui.label->clear(); //这个代码必须的,如没有会卡住
        //cvtColor(frame, frame, COLOR_RGB2BGR);
        //QImage img = QImage((const unsigned char*)(frame.data), frame.cols, frame.rows, frame.cols * frame.channels(), QImage::Format_RGB888);
        //ui.label->setPixmap(QPixmap::fromImage(img));
        //ui.label->update();

        cv::cvtColor(frame, frame, cv::COLOR_BGR2RGB); // 将图像转换为RGB格式
        int bytesPerLine = bytesPerComponent *width;
        QImage q_image(frame.data, width, height, bytesPerLine, QImage::Format_RGB888);
        q_image = q_image.scaled(ui.label->width() * 0.8, ui.label->height() * 0.6);
        ui.label->setPixmap(QPixmap::fromImage(q_image));
        update();
    }
}

3. 走行結果

3. オンラインサポート:

動作環境のインストールやリモート デバッグが必要な場合は、 記事の下部にある個人用 QQ名刺を参照してください。専門および技術担当者がリモートでサポートします。 1) リモートインストールおよび操作環境、コードデバッグ2) Qt、C++、Python エントリーガイド3) インターフェース美化4) ソフトウェア制作



現在の記事リンク: Python+Qt デスクトップと Web ページのヒューマン カスタマー サービス コミュニケーション ツール_alicema1111 のブログ - CSDN ブログ

ブロガーおすすめ記事: Python 顔認識統計 QT フォーム - CSDN ブログ

ブロガーおすすめ記事: Python Yolov5 炎煙認識ソースコード共有 - CSDN ブログ

                         Python OpenCV は歩行者用入口に出入りする人の数を認識します - Python は人の数を認識します - CSDN ブログ

個人ブログ ホームページ: alicema1111 の blog_CSDN ブログ - Python、C++、Web ページ分野のブロガー

すべてのブロガーの記事はここをクリックしてください: alicema1111 の blog_CSDN ブログ - Python、C++、Web ページ分野のブロガー

おすすめ

転載: blog.csdn.net/alicema1111/article/details/131661330