Featured program examples
VS+QT+OpenCV multi-threaded multi-camera video surveillance acquisition form
If you need to install the operating environment or remote debugging, see the personal QQ business card at the bottom of the article, and professional and technical personnel will assist remotely!
foreword
This blog writes code for <<VS+QT+OpenCV multi-threaded multi-camera video surveillance acquisition form>>, the code is neat, regular and easy to read. The first choice for learning and application recommendation.
Function: Multi-camera video asynchronous playback .
Article directory
1. Required tool software
2. Use steps
1. Import library
2. Code implementation
3. Running results
3. Online assistance
1. Required tool software
1. VS, Qt
2. OpenCV
2. Use steps
1. Import library
#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. Code implementation
code show as below:
//.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. Running results
3. Online assistance:
If you need to install the operating environment or remote debugging, see the personal QQ business card at the bottom of the article, and professional and technical personnel will assist remotely!
1) Remote installation and operation environment, code debugging
2) Qt, C++, Python entry guide
3) Interface beautification
4) Software production
Current article link: Python+Qt desktop and webpage human customer service communication tool_alicema1111's blog-CSDN blog
Blogger recommended article: python face recognition statistics qt form - CSDN Blog
Blogger recommended article: Python Yolov5 flame smoke recognition source code sharing - CSDN blog
Personal blog homepage: alicema1111's blog_CSDN blog-Python, C++, bloggers in the field of web pages
Click here for all the blogger's articles : alicema1111's blog_CSDN blog-Python, C++, bloggers in the field of web pages