opencv C++ create Mat object from android NV21 image data buffer

匿名 (未验证) 提交于 2019-12-03 02:05:01

问题:

I have implemented an android application that starts the camera and send all the preview buffer down to native components using JNI interface. Since the preview data is in NV21 image format, I need to create a cv::Mat instance from it. I searched for it and found the below solution:

cv::Mat _yuv(height, width, CV_8UC1, (uchar *) imagebuffer);  where imagebuffer is jbyte* 

However, don't get the expected image in the output image. It's all filled with some random lines etc. Does anyone know how exactly I can do it?

回答1:

You need to convert YUV image to RGBA image.

cv::Mat _yuv(height+height/2, width, CV_8UC1, (uchar *)imagebuffer); cv::cvtColor(_yuv, _yuv, CV_YUV2RGBA_NV21); 

Usually, YUV images are 1 channel images with 1.5*height (if it were an RGB or grayscale image).

Or you could create a new Mat and pass jint array to native function and use that array to set pixels of bitmap.

jint *_out = env->GetIntArrayElements(out, 0);       cv::Mat _yuv(height + height/2, width, CV_8UC1, (uchar*)imagebuffer); cv::Mat _rgba(height, width, CV_8UC4, (uchar *)_out);  cv::cvtColor(_yuv, _rgba, CV_YUV2RGBA_NV21);  env->ReleaseIntArrayElements(out, _out, 0); 

In Java,

bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); pixels = new int[width * height];  native_function(height, width, bytedata, pixels);  bitmap.setPixels(pixels, 0, width, 0, 0, width, height); 


回答2:

1st answer may not works as correct color image. this code is my answer.

    cv::Mat yuv(height+height/2, width, CV_8UC1,(uchar *)nv21ImageBuffer);     cv::Mat converted(height, width, CV_8UC3);     cv::cvtColor(yuv, converted, CV_YUV2BGR_NV21);     cv::imwrite("anywhere/colorImage.jpg",converted); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!