将 YUV420p QVideoFrame 转换为灰度 OpenCV 垫

Transform a YUV420p QVideoFrame into grayscale OpenCV mat

本文关键字:灰度 OpenCV 转换 YUV420p QVideoFrame      更新时间:2023-10-16

我创建了一个扩展QAbstractVideoFilterQVideoFilterRunnable,我已经覆盖了

QVideoFrame run(QVideoFrame* input, const QVideoSurfaceFormat &surfaceFormat, RunFlags flags)`

方法

问题是QVideoFrame格式Format_YUV420P并且没有句柄。我需要将其转换为CV_8UC1才能使用OpenCV算法。

实现此目的的最佳方法是什么?

首先,您需要创建一个cv::Mat,其中包含一个使用数据指针初始化的 API:

cv::Mat img = cv::Mat(rows, cols, CV_8UC3, input.data/*Change this to point the first element of array containing the YUV color info*/)

现在,由于img是使用 YUV 颜色数据初始化的,您可以使用各种cvtColor模式将 YUV 垫转换为其他格式,要将其转换为灰度,您可以尝试:

cv::Mat gray;
cv::cvtColor(img, gray, cv::COLOR_YUV2GRAY_I420);