How to use c++ opencv to convert from RGBA to NNV12


I would like to use cvtColor to convert a cv::Mat from RGBA to NV12. However, according to the doc it is only possible from YUV to NV12. I don’t know what color space conversion code to use.
The format I need as an output is YUV420 (NV12).

Thank you in advance.

Apparently you may need a couple of steps for the conversion: RGBA to BGR, BGR to YUV, and finally YUV to NV12…

I tried doing that as so :

 opencvMatrix = cv::Mat(*(params.height), *(params.width), CV_8UC4, (char*)(pointer), *(params.pitch));
 cv::cvtColor(opencvMatrix, opencvMatrix, cv::COLOR_RGBA2RGB);
 cv::cvtColor(opencvMatrix, opencvMatrix, cv::COLOR_RGB2YUV);
 cv::cvtColor(opencvMatrix, opencvMatrix, cv::COLOR_YUV2RGB_NV12);

However I get the following error :

OpenCV Error: Assertion failed (sz.width % 2 == 0 && sz.height % 3 == 0 && depth == CV_8U) in cvtColor, file /usr/src/debug/opencv/3.2.0-r0/git/modules/imgproc/src/color.cpp, line 9865 terminate called after throwing an instance of 'cv::Exception' what(): /usr/src/debug/opencv/3.2.0-r0/git/modules/imgproc/src/color.cpp:9865: error: (-215) sz.width % 2 == 0 && sz.height % 3 == 0 && depth == CV_8U in function cvtColor

If I simply use the first conversion it works but I need the output to be in NV12 format.

for which of the three calls do you get that?

why do you use in-place operation? that is not possible for the last operation. give it a separate result matrix.

why do you seem to dereference params.height? it’s a number, not a pointer, right?

As far as I can tell that is from YUV(NV12) to RGB. It doesn’t look like there is a function to convert to NV12, what do you need NV12 for?