将YUV420p QVideoFrame转换为灰度OpenCV垫

时间:2017-06-13 09:43:07

标签: c++ qt opencv yuv qvideoframe

我创建了一个扩展QAbstractVideoFilter和的过滤器 QVideoFilterRunnable并且我已经超越了

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

方法

问题是QVideoFrame格式为Format_YUV420P并且无法处理。我需要将其转换为CV_8UC1才能使用OpenCV算法。

实现这一目标的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

首先,您需要创建一个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*/)

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

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