【问题标题】:Force Qt Camera video format强制 Qt Camera 视频格式
【发布时间】:2018-10-09 15:05:42
【问题描述】:

我正在尝试使用 QML 中的 Qt Camera。

我正在开发一个自定义 VideoFilter:

QVideoFrame MyFilterRunnable::run(QVideoFrame* input, const QVideoSurfaceFormat&, RunFlags)

我开始在 Windows 上部署该应用程序,并且得到了:

  • 框架可映射到QAbstractVideoBuffer::ReadWrite
  • 帧像素格式为PixelFormat::Format_BGR32

不幸的是,当我迁移到 Linux 时,一切都变了,我没有更换相机:

  • 框架只有QAbstractVideoBuffer::ReadOnly
  • 帧像素格式为PixelFormat::Format_YUYV

现在我真的不知道如何将此帧转换为 OpenCV Mat

有什么方法可以选择相机的像素格式吗?

【问题讨论】:

    标签: c++ qt opencv camera qml


    【解决方案1】:

    史蒂文,

        if (input->pixelFormat() == QVideoFrame::Format_YUYV)
        {
            auto input_w = input->width ();
            auto input_h = input->height();
    
            auto cam_data = input->bits();
    
            cv::Mat yuyv(input_h, input_w,CV_8UC2, cam_data);
            cv::Mat rgb (input_h, input_w,CV_8UC3);
    
            cvtColor(yuyv, rgb, CV_YUV2BGR_YUYV);
    
            m_mat = rgb;
        }
        else
        if (input->pixelFormat() == QVideoFrame::Format_YUV420P || input->pixelFormat() == QVideoFrame::Format_NV12) {
            m_yuv = true;
            m_mat = yuvFrameToMat8(*input);
        } else {
            m_yuv = false;
            QImage wrapper = imageWrapper(*input);
            if (wrapper.isNull()) {
                if (input->handleType() == QAbstractVideoBuffer::NoHandle)
                    input->unmap();
                return *input;
            }
            m_mat = imageToMat8(wrapper);
        }
        ensureC3(&m_mat);
    

    【讨论】:

      【解决方案2】:

      我在我的 Linux 机器和 Raspberry 之间面临同样的问题:我使用相同的相机,并且 QVideoFrame 提供的像素格式不同。它可能与v4l2有关

      关于从 YUYV 到 OpenCV Mat 的转换,这段代码对我有用:

      QVideoFrame *input ;
      ...
      auto input_w = input->width ();
      auto input_h = input->height();
      
      auto cam_data = input->bits();
      
      cv::Mat yuyv(input_h, input_w,CV_8UC2, cam_data);
      cv::Mat rgb (input_h, input_w,CV_8UC3);
      
      cvtColor(yuyv, rgb, CV_YUV2BGR_YUYV);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-25
        • 2011-08-16
        • 2011-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多