强制Qt相机视频格式



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

我正在开发一个自定义视频过滤器:

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

我开始在Windows上部署应用程序,我有:

  • 帧可在QAbstractVideoBuffer::ReadWrite中映射
  • 帧像素格式PixelFormat::Format_BGR32

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

  • 框架仅QAbstractVideoBuffer::ReadOnly
  • 帧像素格式PixelFormat::Format_YUYV

现在我真的不知道如何将这个框架转换为 OpenCVMat.

有没有办法选择相机的像素格式?

就在史蒂文,

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);

我在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);

最新更新