Home:ALL Converter>Convert Android camera2 api YUV_420_888 to RGB

Convert Android camera2 api YUV_420_888 to RGB

Ask Time:2015-05-28T23:28:48         Author:Constantin Georgiu

Json Formatter

I am writing an app that takes the camera feed, converts it to rgb, in order to do some processing.

It works fine on the old camera implementation which uses NV21 Yuv format. The issue I am having is with the new Yuv format, YUV_420_888. The image is no longer converted correctly to RGB in the new Camera2 Api which sends YUV_420_888 yuv format instead of NV21 (YUV_420_SP) format.

Can someone please tell me how should I convert YUV_420_888 to RGB?

Thanks

Author:Constantin Georgiu,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30510928/convert-android-camera2-api-yuv-420-888-to-rgb
Shyam Kumar :

Camera2 YUV_420_888 to RGB Mat(opencv) in Java\n\n@Override\n public void onImageAvailable(ImageReader reader){\n Image image = null;\n\n try {\n image = reader.acquireLatestImage();\n if (image != null) {\n\n byte[] nv21;\n ByteBuffer yBuffer = mImage.getPlanes()[0].getBuffer();\n ByteBuffer uBuffer = mImage.getPlanes()[1].getBuffer();\n ByteBuffer vBuffer = mImage.getPlanes()[2].getBuffer();\n\n int ySize = yBuffer.remaining();\n int uSize = uBuffer.remaining();\n int vSize = vBuffer.remaining();\n\n nv21 = new byte[ySize + uSize + vSize];\n\n //U and V are swapped\n yBuffer.get(nv21, 0, ySize);\n vBuffer.get(nv21, ySize, vSize);\n uBuffer.get(nv21, ySize + vSize, uSize);\n\n Mat mRGB = getYUV2Mat(nv21);\n\n\n\n }\n } catch (Exception e) {\n Log.w(TAG, e.getMessage());\n }finally{\n image.close();// don't forget to close\n }\n }\n\n\n\n public Mat getYUV2Mat(byte[] data) {\n Mat mYuv = new Mat(image.getHeight() + image.getHeight() / 2, image.getWidth(), CV_8UC1);\n mYuv.put(0, 0, data);\n Mat mRGB = new Mat();\n cvtColor(mYuv, mRGB, Imgproc.COLOR_YUV2RGB_NV21, 3);\n return mRGB;\n}\n",
2018-09-24T10:07:33
Daniel Więcek :

In my approach I use OpenCV Mat and script from\nhttps://gist.github.com/camdenfullmer/dfd83dfb0973663a7974\n\nFirst of all you convert your YUV_420_888 Image to Mat with the code in the link above. \n\n*mImage is my Image object which i get in ImageReader.OnImageAvailableListener\n\nMat mYuvMat = imageToMat(mImage);\n\npublic static Mat imageToMat(Image image) {\n ByteBuffer buffer;\n int rowStride;\n int pixelStride;\n int width = image.getWidth();\n int height = image.getHeight();\n int offset = 0;\n\n Image.Plane[] planes = image.getPlanes();\n byte[] data = new byte[image.getWidth() * image.getHeight() * ImageFormat.getBitsPerPixel(ImageFormat.YUV_420_888) / 8];\n byte[] rowData = new byte[planes[0].getRowStride()];\n\n for (int i = 0; i < planes.length; i++) {\n buffer = planes[i].getBuffer();\n rowStride = planes[i].getRowStride();\n pixelStride = planes[i].getPixelStride();\n int w = (i == 0) ? width : width / 2;\n int h = (i == 0) ? height : height / 2;\n for (int row = 0; row < h; row++) {\n int bytesPerPixel = ImageFormat.getBitsPerPixel(ImageFormat.YUV_420_888) / 8;\n if (pixelStride == bytesPerPixel) {\n int length = w * bytesPerPixel;\n buffer.get(data, offset, length);\n\n if (h - row != 1) {\n buffer.position(buffer.position() + rowStride - length);\n }\n offset += length;\n } else {\n\n\n if (h - row == 1) {\n buffer.get(rowData, 0, width - pixelStride + 1);\n } else {\n buffer.get(rowData, 0, rowStride);\n }\n\n for (int col = 0; col < w; col++) {\n data[offset++] = rowData[col * pixelStride];\n }\n }\n }\n }\n\n Mat mat = new Mat(height + height / 2, width, CvType.CV_8UC1);\n mat.put(0, 0, data);\n\n return mat;\n}\n\n\nWe have 1 channel YUV Mat. Define new Mat for BGR(not RGB yet) image:\n\nMat bgrMat = new Mat(mImage.getHeight(), mImage.getWidth(),CvType.CV_8UC4);\n\n\nI just started learning OpenCV so propably this doesn't have to be 4-channel Mat and instead could be 3-channel but it works for me.\nNow I use convert color method to change my yuv Mat into bgr Mat.\n\nImgproc.cvtColor(mYuvMat, bgrMat, Imgproc.COLOR_YUV2BGR_I420);\n\n\nNow we can do all the image processing like finding contours, colors, circles, etc. To print image back on screen we need to convert it to bitmap:\n\nMat rgbaMatOut = new Mat();\nImgproc.cvtColor(bgrMat, rgbaMatOut, Imgproc.COLOR_BGR2RGBA, 0);\nfinal Bitmap bitmap = Bitmap.createBitmap(bgrMat.cols(), bgrMat.rows(), Bitmap.Config.ARGB_8888);\nUtils.matToBitmap(rgbaMatOut, bitmap);\n\n\nI have all my image processing in seperate thread so to set my ImageView I need to do this on the UI thread.\n\nrunOnUiThread(new Runnable() {\n @Override\n public void run() {\n if(bitmap != null) {\n mImageView.setImageBitmap(bitmap);\n }\n }\n });\n",
2016-02-05T10:17:33
Ciprian :

Have you tried using this script? It's an answer posted by yydcdut on this question\n\nhttps://github.com/pinguo-yuyidong/Camera2/blob/master/camera2/src/main/rs/yuv2rgb.rs",
2015-05-29T07:44:54
silva :

Use Shyam Kumar's answer is not right for my phone, but Daniel Więcek's is right.I debug it, find planes[i].getRowStride() is 1216, planes[i].getPixelStride() is 2. While image width and height is both 1200.\n\nBecause my reputation is 3, so I cann't comment but post an answer.",
2019-09-01T09:26:08
coyer :

Approximately 10 times faster than the mentioned \"imageToMat\"-Function above is this code:\n\nImage image = reader.acquireLatestImage();\n...\nMat yuv = new Mat(image.getHeight() + image.getHeight() / 2, image.getWidth(), CvType.CV_8UC1);\nByteBuffer buffer = image.getPlanes()[0].getBuffer();\nfinal byte[] data = new byte[buffer.limit()];\nbuffer.get(data);\nyuv.put(0, 0, data);\n...\nimage.close();\n",
2016-04-12T12:44:32
yy