OpenCV图像处理--opencv摄像头播放视频

OpenCV图像处理--opencv摄像头播放视频,第1张

OpenCV图像处理--opencv摄像头播放视频

步骤一:
Qt+OpenCV环境配置
步骤二:

创建视频播放线程

核心代码如下
视频播放线程.h文件

#include
#include

using namespace cv;
class PlayerVideoThread : public QThread
{
    Q_OBJECT
public:
    PlayerVideoThread(char *videoPath);
    void run();
private:
    VideoCapture cap;
    Mat  frame;
signals:
    void frameVideo(Mat frame);
};

视频播放线程.cpp源文件

#include "playervideothread.h"
#include
PlayerVideoThread::PlayerVideoThread(char *videoPath)
{
    if(cap.open(videoPath))
    {
           qDebug()<<"open video";
    }
}

void PlayerVideoThread::run()
{
    while(cap.read(frame))
    {
        emit frameVideo(frame);
    }
}

视频显示窗口.h文件

class VideoWidget : public QWidget
{
    Q_OBJECT
public:
    explicit VideoWidget(QWidget *parent = 0);

    QLabel *label;

     VideoCapture cap;
     Mat frame;
     PlayerVideoThread *plaverVideoThread;
     void paintEvent(QPaintEvent *e);
signals:

public slots:
      void  updateImage(Mat img);
};

视频显示窗口.cpp文件

VideoWidget::VideoWidget(QWidget *parent) : QWidget(parent)
{

    this->resize(400,400);
    label = new QLabel(this);
    label->resize(400,400);

    this->plaverVideoThread = new PlayerVideoThread("carMove.mp4");
    connect(this->plaverVideoThread,SIGNAL(frameVideo(Mat)),this,SLOT(updateImage(Mat)),Qt::BlockingQueuedConnection);
    this->plaverVideoThread->start();

}

void VideoWidget::paintEvent(QPaintEvent *e)
{
    QImage q_image = QImage(frame.data,frame.cols,frame.rows,QImage::Format_RGB888);
    label->setPixmap(QPixmap::fromImage(q_image));
    label->setScaledContents(true);
}

void VideoWidget::updateImage(Mat img)
{
    this->frame = img.clone();
    this->update();
}

执行效果

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5702574.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存