OpenCV+多线程视频采集(二)

一、在完成OpenCV+多线程视频采集(一)后,我们可以改用moveToThread函数来实现多线程,不需要从OThread继承它,创建一个新类VidoProcessor;

1、vidoprocessor.h
#ifndef VIDOPROCESSOR_H
#define VIDOPROCESSOR_H

#include <QObject>
#include <opencv2/opencv.hpp>
#include <QPixmap>
#include <QImage>
using namespace std;
using namespace cv;

class VidoProcessor : public QObject
{
    Q_OBJECT
public:
    explicit VidoProcessor(QObject *parent = nullptr);

signals:
    void inDisplay1(QPixmap pixmap);
    void outDisplay1(QPixmap pixmap);

public slots:
    void startVideo();
    void stopVideo();
private:
    bool stopped; 
};

#endif // VIDOPROCESSOR_H      

2、vidoprocessor.cpp

#include "vidoprocessor.h"
#include <QDebug>
VidoProcessor::VidoProcessor(QObject *parent) : QObject(parent)
{
  stopped=false; 
void VidoProcessor::startVideo()
{

    VideoCapture camera(0);
    Mat inFrame, outFrame;
    while (camera.isOpened() && !stopped) {
        camera >> inFrame;
        if(inFrame.empty())
            continue;  
        bitwise_not(inFrame,outFrame);
        emit inDisplay1(
                    QPixmap::fromImage(
                        QImage(inFrame.data,
                               inFrame.cols,
                               inFrame.rows,
                               static_cast<int>(inFrame.step),
                               QImage::Format_RGB888).rgbSwapped()));
        emit outDisplay1(
                    QPixmap::fromImage(
                        QImage(outFrame.data,
                               outFrame.cols,
                               outFrame.rows,
                               static_cast<int>(outFrame.step),
                               QImage::Format_RGB888).rgbSwapped()));

    }
}            
void VidoProcessor::stopVideo()
{
    stopped = true;
}

二、现在,可以MainWindow类中使用它,然后将以下内容添加到MainWindow的私有成员部分:

//    VideoProcessorThread processor;
VidoProcessor *processor;

三、在MainWindow构造函数中:

processor = new VidoProcessor();
processor->moveToThread(new QThread(this));
connect(processor->thread(),SIGNAL(started()),processor,SLOT(startVideo()));
connect(processor->thread(),SIGNAL(finished()),processor,SLOT(deleteLater()));
processor->thread()->start();

四、在MainWindow析构函数中:

processor->stopVideo();
processor->thread()->quit();
processor->thread()->wait();
delete ui;
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值