本文介绍了opencv C ++从Android NV21图像数据缓冲区创建Mat对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了一个Android应用程序,该应用程序启动相机并使用JNI接口将所有预览缓冲区发送到本机组件.由于预览数据为NV21图像格式,因此需要从中创建一个cv :: Mat实例.我进行了搜索,找到了以下解决方案:

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?

推荐答案

您需要将YUV图像转换为RGBA图像.

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);

通常,YUV图像是带有1.5*height的1通道图像(如果是RGB图像或灰度图像).

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

或者您可以创建一个新的Mat并将jint数组传递给本机函数,然后使用该数组设置位图的像素.

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);

在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);

这篇关于opencv C ++从Android NV21图像数据缓冲区创建Mat对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 12:02