gpt4 book ai didi

c++ - 多线程opencv视频处理Qt/C++

转载 作者:太空宇宙 更新时间:2023-11-03 22:30:11 30 4
gpt4 key购买 nike

希望你一切顺利!我从昨天开始苦苦思索如何用opencv实现一个多线程的视频处理程序。

我了解线程实际上是如何工作的,如何使用简单的互斥体等等......但是当涉及到实现时,我完全迷失了。

我的目标是使用 Qt 创建一个界面并显示两个标签,一个显示原始视频源,另一个显示处理后的视频,每个标签由一个线程处理。

现在我只是想在线程上显示图像,但我真的很挣扎。

这是我到目前为止所做的:

CaptureThread 继承自 QThread 的类:它应该处理凸轮捕获等的开始......

#ifndef CAPTURETHREAD_H
#define CAPTURETHREAD_H

#include <QThread>

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "opencv2/videoio.hpp"

#include <QTimer>

class CaptureThread : public QThread
{
Q_OBJECT

public:
explicit CaptureThread(QObject *parent);


protected:
void run();

signals:
void frameCaptured(cv::Mat);

public slots:
void captureFrame();

private:
QTimer* tmrTimer;
cv::VideoCapture capWebam;
cv::Mat capturedFrame;

};

#endif // CAPTURETHREAD_H

这是它的实现:

#include "capturethread.h"
#include <QDebug>

CaptureThread::CaptureThread(QObject* parent):QThread(parent)
{
capWebam.open(0);
}

void CaptureThread::run()
{
tmrTimer = new QTimer(this);
QObject::connect(tmrTimer, SIGNAL(timeout()), this, SLOT(captureFrame()));

tmrTimer->start(10);
exec();
}

void CaptureThread::captureFrame()
{
if(capWebam.isOpened()){
capWebam.read(capturedFrame);

emit frameCaptured(capturedFrame);
}

}

MainWindow 用于显示相机提要等...

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "opencv2/videoio.hpp"
#include <capturethread.h>

#include <QTimer>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

public slots:
void pricessFrameAndUpdateGUI(cv::Mat matOriginal);

private slots:
void on_button_clicked();

private:
Ui::MainWindow *ui;
QImage toGrayscale(QImage image);

cv::VideoCapture capWebcam;
cv::Mat matOriginal;
cv::Mat matProcessed;

QImage qimgOriginal;
QImage qimgProcessed;

QTimer* tmrTimer;

CaptureThread* cpThread;
};

#endif // MAINWINDOW_H

及其实现:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCore>
#include <cv.h>
#include <QColor>
#include <opencv/highgui.h>

MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{
ui->setupUi(this);

cpThread = new CaptureThread(this);
QObject::connect(cpThread, SIGNAL(frameCaptured(cv::Mat)),this, SLOT(pricessFrameAndUpdateGUI(cv::Mat)));
cpThread->start();

}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::pricessFrameAndUpdateGUI(cv::Mat matOriginal)
{


cv::Canny(matOriginal, matProcessed, 100, 300);

cv::cvtColor(matOriginal, matOriginal, CV_BGR2RGB);
QImage qimgOriginal((uchar*)matOriginal.data, matOriginal.cols, matOriginal.rows, matOriginal.step, QImage::Format_RGB888);
QImage qimgProcessed((uchar*)matProcessed.data, matProcessed.cols, matProcessed.rows, matProcessed.step,QImage::Format_Indexed8);

ui->original->setPixmap(QPixmap::fromImage(qimgOriginal));
ui->modified->setPixmap(QPixmap::fromImage(qimgProcessed));

}

编译和执行程序后我得到这个错误:

Starting /media/wassim/BLAZER/Workspace/CPP/build-firstCV-Desktop_Qt_5_3_GCC_64bit-Debug/firstCV...

VIDEOIO ERROR: V4L/V4L2: VIDIOC_S_CROP

QObject: Cannot create children for a parent that is in a different thread.

(Parent is CaptureThread(0xc02be0), parent's thread is QThread(0xae82c0), current thread is CaptureThread(0xc02be0)

The program has unexpectedly finished.

/media/wassim/BLAZER/Workspace/CPP/build-firstCV-Desktop_Qt_5_3_GCC_64bit-Debug/firstCV crashed

谢谢大家的帮助!

最佳答案

您看到的错误是由于 QTimer 被传递给位于另一个线程中的父级所引起的。 CaptureThread 存在于 UI 线程中。 QTimer 在另一个线程中创建(它在 run() 方法中)。

最简单的解决方案:将 QTimer 的实例化(和开始调用)移动到 ctor 中:

tmrTimer = new QTimer(this);
QObject::connect(tmrTimer, SIGNAL(timeout()),
this, SLOT(captureFrame()));
tmrTimer->start(40);

这应该有效。但它不会像它应该的那样工作。 timeout() 信号将在 CaptureThread 所在的线程(即 UI 线程)中排队消息。所以一切都将在 UI 线程中完成,而不仅仅是后期处理。最快的解决方案:

CaptureThread::CaptureThread() : QObject()
{
capWebam.open(0);

QThread* t = new QThread();
moveToThread(t);
t->start();

tmrTimer = new QTimer;
QObject::connect(tmrTimer, SIGNAL(timeout()),
this, SLOT(captureFrame()));
tmrTimer->start(40);
}

CaptureThread 被移动到一个新的 QThread(它不是 QThread 的子类)。另外,将后处理移至此线程。这只是概念,然后您必须处理清理、注册元类型等...

编辑:好的,只是一个快速测试代码(在 Mac OS 上测试过),可能会损坏并需要优化,我没有检查内存清理等......(还要注意你如何隐藏 matOriginal 在你的代码,您没有将类成员传递给 QImage,而是传递给本地实例):

主要.cpp

#include <QApplication>
#include <QLabel>
#include <QTimer>
#include <QThread>

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

#include "src.h"

int main(int argc, char* argv[])
{
QApplication a(argc, argv);

cv::VideoCapture vc;
if (!vc.open(0))
return 1;

QTimer t;
QThread th;
CaptureHandler handler(&vc);
handler.moveToThread(&th);
th.start();

MainWidget w1;
w1.resize(100, 100);
w1.show();

MainWidget w2;
w2.resize(100, 100);
w2.show();

QObject::connect(&t, SIGNAL(timeout()),
&handler, SLOT(handleFrame()));
QObject::connect(&handler, SIGNAL(frameReady(QImage)),
&w1, SLOT(onFrame(QImage)));
QObject::connect(&handler, SIGNAL(framePpReady(QImage)),
&w2, SLOT(onFrame(QImage)));
t.start(20);

return a.exec();
}

源文件

#ifndef SRC_H
#define SRC_H

#include <QObject>
#include <QImage>
#include <QLabel>

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>

class CaptureHandler : public QObject
{
Q_OBJECT
public:
CaptureHandler(cv::VideoCapture* vc);
signals:
void frameReady(QImage frame);
void framePpReady(QImage frame);
public slots:
void handleFrame();
private:
cv::VideoCapture* vc;
};

class MainWidget : public QLabel
{
Q_OBJECT
public slots:
void onFrame(QImage frame);
};

#endif // SRC_H

源文件

#include <QObject>
#include <QImage>
#include <QLabel>

#include "src.h"

void cleanup_mat(void* info)
{
delete (cv::Mat*)info;
}

CaptureHandler::CaptureHandler(cv::VideoCapture* vc) : QObject(), vc(vc) {}

void CaptureHandler::handleFrame() {
cv::Mat* original = new cv::Mat;
if (!vc->read(*original))
return;

cv::Mat* processed = new cv::Mat;
cv::Canny(*original, *processed, 100, 300);
cv::cvtColor(*original, *original, CV_BGR2RGB);
QImage qimgOriginal((uchar*)original->data,
original->cols,
original->rows,
original->step,
QImage::Format_RGB888, cleanup_mat, original);
QImage qimgProcessed((uchar*)processed->data,
processed->cols,
processed->rows,
processed->step,
QImage::Format_Indexed8, cleanup_mat, processed);

emit frameReady(qimgOriginal);
emit framePpReady(qimgProcessed);
}

void MainWidget::onFrame(QImage frame) {
setPixmap(QPixmap::fromImage(frame));
}

射击:-)

enter image description here

关于c++ - 多线程opencv视频处理Qt/C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27655588/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com