Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Map cv::Mat to QAbstractPlanarVideoBuffer subclass
Forum Updated to NodeBB v4.3 + New Features

Map cv::Mat to QAbstractPlanarVideoBuffer subclass

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 278 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Konrad Matusiak
    wrote on last edited by
    #1

    Im trying to map a cv::Mat to QAbstractPlanarVideoBuffer and then to QVideoFrame.
    A conversion from cv::Mat to QImage and then to QVideoFrame is to slow for my use case.
    The following code produces mostly green frames. I have no experience with video encoding so I don't know what the problem could be. Any help would be appreciated. Thanks in advance.

    CVBuffer.h:

    #ifndef CVBUFFER_H
    #define CVBUFFER_H
    
    #include <QAbstractPlanarVideoBuffer>
    #include "opencv2/core.hpp"
    #include "opencv2/imgproc.hpp"
    #include "opencv2/calib3d.hpp"
    
    class CVBuffer : public QAbstractPlanarVideoBuffer
    {
    
    public:
        CVBuffer(cv::Mat *frame);
        ~CVBuffer();
    
        int map(MapMode mode, int *numBytes, int bytesPerLine[4], uchar *data[4]);
    
        MapMode mapMode() const;
    
        void unmap();
    private:
        cv::Mat frame;
    
    };
    #endif // CVBUFFER_H
    

    CVBuffer.cpp

    #include "cvbuffer.h"
    
    CVFBuffer::FBuffer(cv::Mat* frame)
         : QAbstractPlanarVideoBuffer(NoHandle)
         , frame(frame->clone())
     {
     }
    
     CVBuffer::~CVBuffer() {
     }
    
     int FBuffer::map(MapMode mode, int *numBytes, int bytesPerLine[4], uchar *data[4]) {
         if (mode == ReadOnly) {
              *numBytes = frame.total() * frame.elemSize();
             bytesPerLine[0] = frame.step;
             data[0] = frame.data;
             return 3;
         }
         return 0;
     }
    
     FBuffer::MapMode FBuffer::mapMode() const { return ReadOnly; }
    
     void FBuffer::unmap() { }
    
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Why to you return 3? I only see that you have one plane.
      Also you should free/unref your frame in the dtor

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • B Offline
        B Offline
        Bonnie
        wrote on last edited by
        #3

        I have no experience in this but it may help to look at how Qt use gstreamer's video frame to construct a planar video buffer:
        https://code.woboq.org/qt5/qtmultimedia/src/gsttools/qgstvideobuffer.cpp.html

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved