从VlcMedia转换为Mat,反之亦然

时间:2015-09-22 13:13:05

标签: c++ qt opencv image-processing vlc-qt

我试图连接" vlc-qt "到" opencv " 。我正在使用 VlcInstance VlcMedia 阅读我的ip camera。

据我所知,opencv可以工作,需要将视频放在Mat格式中:

videoCapture cap;
Mat frame ;
cap >> frame ; 

但我有 VlcMedia 格式。有没有办法将其转换为Mat格式(或作为视频捕捉的来源),然后将其转换回VlcMedia,以便能够在VlcMediaPlayer中显示它。 我需要它快速(实际上是实时快速的:D)。

这是我的代码(我在Visual Studio中使用Qt):

我的头文件:

#ifndef QT_VLC_OPENCV1_H
#define QT_VLC_OPENCV1_H

#include <QtWidgets/QMainWindow>
#include "ui_qt_vlc_opencv1.h"

#include <VLCQtCore\Instance.h>
#include <VLCQtCore\Media.h>
#include <VLCQtCore\MediaPlayer.h>

class qt_vlc_opencv1 : public QMainWindow
{
    Q_OBJECT

public:
    qt_vlc_opencv1(QWidget *parent = 0);
    ~qt_vlc_opencv1();
private slots:
    void on_btnOpenFile_clicked();

private:
    Ui::qt_vlc_opencv1Class ui;

    VlcInstance *_instance;
    VlcMedia *_media;
    VlcMediaPlayer *_player;
};

#endif // QT_VLC_OPENCV1_H

和我的cpp文件:

#include "qt_vlc_opencv1.h"

#include <VLCQtCore\Common.h>
#include <qinputdialog.h>

#include <opencv2/highgui/highgui.hpp>
#include <cv.h>
using namespace cv;

qt_vlc_opencv1::qt_vlc_opencv1(QWidget *parent)
: QMainWindow(parent), _media(0)
{
    ui.setupUi(this);

    _instance = new VlcInstance(VlcCommon::args(), this);
    _player = new VlcMediaPlayer(_instance);
    _player->setVideoWidget(ui.video);

    ui.video->setMediaPlayer(_player);
}

qt_vlc_opencv1::~qt_vlc_opencv1()
{
    delete _player;
    delete _media;
    delete _instance;
}

void qt_vlc_opencv1::on_btnOpenFile_clicked()
{
    QString file = QInputDialog::getText(this,tr("Open File"),tr("Enter a file path or a Url"));
    if (file.isEmpty())
        return;

    _media = new VlcMedia(file, true, _instance);
    _player->open(_media);
}

1 个答案:

答案 0 :(得分:0)

在Qt-vlc库中有一个VlcVideoStream类。 VlcVideoStream设置适当的回调以从libVLC获取YUV帧。 此类应为子类,并实现frameUpdated以指定如何处理框架。 在frameUpdator下面的代码中是VlcVideoStream的子类,我已经实现了它的frameUpdated函数。 例如,您可以通过以下方式实现frameUpdated:

  void frameUpdator::frameUpdated()
 {
int rows, cols, matType;

// convert to shared pointer to const frame to avoid crash
std::shared_ptr<const VlcYUVVideoFrame> frame = std::dynamic_pointer_cast<const VlcYUVVideoFrame>(renderFrame());

if (!frame) {
    return; // LCOV_EXCL_LINE
}

 //     rows = frame->height + frame->height / 2;
 //     cols = frame->width;
 //     matType = CV_8UC1;
 // 
 //     cv::Mat myuv(rows, cols, matType, (void*)frame->frameBuffer.data());
 //     cv::Mat mrgb(frame->height, frame->width, CV_8UC3);
 //     cv::cvtColor(myuv, mrgb, CV_YUV2RGB_I420);

  int width = frame->width;
  int height = frame->height;

   cv::Mat result = cv::Mat(height, width, CV_8UC1,(void*)frame->frameBuffer.data());
cv::imshow("result", result);
cv::waitKey(2);

   }