Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am implementing computer vision project for Android and I am using OpenCV with Android NDK for it. It receives data in YUV NV21 format from camera in byte array from PreviewCallback and calls native part of app.
My problem is, that I don't know, how to create 3-channel Mat with YUV data to do thresholding on it. For thresholding I want to use this code

Mat* threshold = new Mat(height, width, CV_8UC1);    
Scalar low(yuvPixel[0] - 50, yuvPixel[1] - 10, yuvPixel[2] - 10);
Scalar up(yuvPixel[0] + 50, yuvPixel[1] + 10, yuvPixel[2] + 10);
inRange(image, low, up, *threshold);

I only know, how to create 1-channel Mat using

Mat image(height + height/2, width, CV_8UC1, (unsigned char *)frameData);

Is there any simple way to convert this Mat to 3-channel Mat?
Thanks in advance.

to create 3-channel Mat with YUV data.

   Mat myuv(height + height/2, width, CV_8UC1, (unsigned char *)_yuv);

for a yuv 422 data. the things is matching your yuv data format carefully.

"Is there any simple way to convert this Mat to 3-channel Mat?"

did you mean 3-channel RGB data? if so, following codes should work

Mat myuv(height + height/2, width, CV_8UC1, ( unsigned char *)_yuv); Mat mbgra(height, width, CV_8UC4, ( unsigned char *)_bgra); Mat mgray(height, width, CV_8UC1, ( unsigned char *)_yuv); cvtColor(myuv, mbgra, CV_YUV420sp2BGR, 4);

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.