0

I want to convert a QVideoFrame in an OpenCV Mat. The frame is mapped in read-only mode.

I wrote the following code:

cv::Mat openCVBGRMatFrom(const QVideoFrame& frame)
{
    auto width = frame.width();
    auto height = frame.height();
    auto* bits = const_cast<uchar*>(frame.bits());

    cv::Mat result(height, width, CV_8UC3, cv::Scalar{0});
    cv::Mat tmp{height + height/2, width, CV_8UC1, bits};

    switch (frame.pixelFormat()) {
    case QVideoFrame::Format_YUYV:
        cv::cvtColor(tmp, result, cv::COLOR_YUV2BGR_YUYV);
        break;

    default:
        throw std::runtime_error{"Unknown YUV pixel format"};
    }

    return result;
}

But when I run it, I've got the following runtime error message:

terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(4.0.0-pre) ../modules/imgproc/src/color.hpp:253: error: (-215:Assertion failed) VScn::contains(scn) && VDcn::contains(dcn) && VDepth::contains(depth) in function 'CvtHelper'

I think this post is not a duplicate because... What's wrong with my code?

artless noise
  • 21,212
  • 6
  • 68
  • 105
Elvis Dukaj
  • 7,142
  • 12
  • 43
  • 85

0 Answers0