0

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?

artless noise
  • 21,212
  • 6
  • 68
  • 105
Elvis Dukaj
  • 7,142
  • 12
  • 43
  • 85
  • Check [this](https://stackoverflow.com/questions/5026965/how-to-convert-an-opencv-cvmat-to-qimage) for a possible solution. – rbaleksandar Jun 13 '17 at 09:55

1 Answers1

1

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);
ZdaR
  • 22,343
  • 7
  • 66
  • 87
  • Y'420P is ordered the first half of data is the lumia (look at this pic https://en.wikipedia.org/wiki/File:Yuv420.svg in https://en.wikipedia.org/wiki/YUV) so a simple solution is to copy only the first half of data `cv::Mat mat(height, width, CV_8UC1, data);` – Elvis Dukaj Jun 13 '17 at 10:16
  • Yeah it would serve the purpose if you want to convert the img to gray-scale only, but what if someone wants to convert to RGB, I tried to put up a generic answer, However your approach is more optimised :) – ZdaR Jun 13 '17 at 10:18