Qt + vs2019 配置OpenCV环境并打开rtsp视频流 多线程处理

一.环境搭建

1.opencv3.4.1下载

opencv3.4.1下载: https://opencv.org/releases/page/5/

下载不了可以使用我上传的:opencv3.4.1.zip

2.环境变量配置:

右键“此电脑”——属性——高级系统设置——环境变量。

在系统变量中找到“Path”,编辑。

新建两个环境变量,找到opencv3.4.r的bin目录

例如我的:

D:\opencv3.4.1\build\x64\vc14\bin

3.项目属性配置

在VS2019中新建项目,右键项目名—配置属性—VC++目录,在包含目录中加入opencv3.4.1的include目录.

例如我的:

D:\opencv3.4.1\build\include\opencv2
D:\opencv3.4.1\build\include\opencv
D:\opencv3.4.1\build\include

在库目录中加入opencv3.4.1的lib目录.

例如我的:

D:\opencv3.4.1\build\x64\vc14\lib

链接器——输入,在附加依赖项中加入opencv3.4.1的*.lib文件名:

opencv_world341d.lib

二、头文件代码

#pragma once
#pragma execution_character_set("utf-8")
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

class readVideoThread : public QThread
{
    Q_OBJECT
public:
    readVideoThread(QObject* parent = Q_NULLPTR);
    ~readVideoThread();
    void stop();

    //设置网络摄像头地址
    void setVideoAddress(const QString& str);

signals:
    void sendQImage(const QImage&);
    void sigVideoOpen(bool);

protected:
    virtual void run();

private:
    bool stopped;
    cv::VideoCapture m_VideoCapture;
    //Mat数据
    cv::Mat m_MatRead, m_MatRGB;
    //网络摄像头地址
    QString m_sVideoAddress;
};

三、源文件代码

#include "readVideoThread.h"

readVideoThread::readVideoThread(QObject* parent) : QThread(parent)
{
    stopped = false;
    m_sVideoAddress = "";
}

readVideoThread::~readVideoThread()
{
    stopped = true;
    while(!this->isFinished())
    {      
        this->stop();
        //等待1500毫秒,线程退出
        for (int i = 0; i < 300; ++i)
        {
            if (stopped == false)
                break;
            msleep(5);
        }
    }
}

void readVideoThread::stop()
{
    stopped = true;
}

void readVideoThread::setVideoAddress(const QString& str)
{
    m_sVideoAddress = str;
}

void readVideoThread::run()
{
    stopped = false;
    while (!stopped)
    {
        //打开摄像头
        if (!m_VideoCapture.open(m_sVideoAddress.toStdString()))
        {
            emit sigVideoOpen(false);
            qDebug() << "摄像头打开失败";
            break;
        }
        emit sigVideoOpen(true);
        while (!stopped)
        {
            //读取摄像头视频
            if (m_VideoCapture.read(m_MatRead))
            {                    
                cv::cvtColor(m_MatRead, m_MatRGB, CV_BGR2RGB);//OpenCV使用BGR,QImage使用RGB,通道转换
                QImage image = QImage(m_MatRGB.data, m_MatRGB.cols, m_MatRGB.rows, QImage::Format_RGB888);//cv::Mat转QImage
                if (!stopped)
                    emit sendQImage(image);
            }
            else {
                break;
            }
        }
    }
    stopped = false;
}

四、使用

void mainwindow::mainwindow(QWidget *parent)
    : QMainWindow(parent)
{
    readVideoThread *videoThread = new readVideoThread(this);
    QString rtspUrl = "rtsp://admin:[email protected]:554/cam/realmonitor?channel=1&subtype=0";
//大华摄像头的rtsp,可以用其他的rtsp测试
    videoThread->setVideoAddress(rtspUrl);
    connect(videoThread, SIGNAL(sendQImage(const QImage)), this, SLOT(slotRecviveQimage(const QImage)));
    connect(videoThread, &readVideoThread::sigVideoOpen, this, [=](bool open) {
	    if (!open){
	    	ui.label_video->clear();
		    ui.label_video->setText("视频连接断开,请检查网线是否连接正常");
	    }
	    });
    videoThread->start();
}

void mainwindow::slotRecviveQimage(const QImage& image)
{
	ui.label_video->setPixmap(QPixmap::fromImage(image));
}

五、运行

在生成的.exe文件路径下加入opencv_world341.dll

 

 

你可能感兴趣的:(opencv开发记录,QT开发经验记录,qt,opencv)