elvis.dukaj elvis.dukaj -4 years ago 421
C++ Question

Transform a YUV420p QVideoFrame into grayscale OpenCV mat

I've created a filter extending

QAbstractVideoFilter
and
QVideoFilterRunnable
and I've overrided the

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


method

The problem is that
QVideoFrame
format is
Format_YUV420P
and has no handle. I need to convert it into a
CV_8UC1
in order to use
OpenCV
algorithms.

Which is the best way to accomplish this?

Answer Source

First you need to create a cv::Mat which has an API for initializing using data pointer as:

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*/)

Now since the img is initialized with YUV color data, you may use various cvtColor modes to convert the YUV mat to other formats, for converting it to gray-scale you may try:

cv::Mat gray;
cv::cvtColor(img, gray, cv::COLOR_YUV2GRAY_I420);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download