1 | initial version |
I have a image(YUV_420_888) that captured by ARCore in pixel, and want to covert it to Mat.
I have try the first answer(http://answers.opencv.org/question/61628/android-camera2-yuv-to-rgb-conversion-turns-out-green/?answer=84473#post-id-84473), but it failed.I can't get the rgb image by save the picture using the opencv function: imwrite().
And then, I try the second answer(http://answers.opencv.org/question/61628/android-camera2-yuv-to-rgb-conversion-turns-out-green/?answer=100322#post-id-100322), I failed too. because in the image that saved by imwrite(), red object covert to green.
Finally, because of a little accident, I try the code that YUV to data(byte[]) that write in the first answer, and try the code that data(byte[]) to Mat that writes in the second answer, and I successd.
---------- Java
Image.Plane Y = image.getPlanes()[0];
Image.Plane U = image.getPlanes()[1];
Image.Plane V = image.getPlanes()[2];
int Yb = Y.getBuffer().remaining();
int Ub = U.getBuffer().remaining();
int Vb = V.getBuffer().remaining();
byte[] data = new byte[Yb + Ub + Vb];
Y.getBuffer().get(data, 0, Yb);
U.getBuffer().get(data, Yb, Ub);
V.getBuffer().get(data, Yb+ Ub, Vb);
return data;
--------- OpenCV JNI ,c++
extern "C"
JNIEXPORT jintArray JNICALL
Java_com_google_ar_core_examples_java_helloar_HelloArActivity_getKeyPoint2
(JNIEnv *env, jobject jobj, jbyteArray _pixels1, jbyteArray _pixels2, jint width, jint height)
{
jbyte *pixels1 = env->GetByteArrayElements(_pixels1 , NULL);
jbyte *pixels2 = env->GetByteArrayElements(_pixels2 , NULL);
if(pixels1==NULL){
int size = -1;
jintArray result = env->NewIntArray(size);
return result;
}
Mat lyuvMat(height + (height / 2), width, CV_8UC1, (unsigned char*)pixels1);
imwrite("/storage/emulated/0/camoutput/xly/lyuv.jpg", lyuvMat);
Mat lrgbMat(height, width, CV_8UC3);
cvtColor(lyuvMat, lrgbMat, COLOR_YUV2RGB_NV21, 3);
imwrite("/storage/emulated/0/camoutput/xly/lrgb.jpg", lrgbMat);
.......
}