Android YUV_420_888 to GrayU8

641 views
Skip to first unread message

kha...@gmail.com

unread,
Mar 10, 2017, 8:54:29 PM3/10/17
to BoofCV
Unlike the previous android Camera API, which preferred the NV21 image format, the new Camera2 API seems to want to use YUV_420_888. I would like to convert a YUV_420_888 preview image to a GrayU8 instance as efficiently as possible. My understanding is that the Y color plane already represents the grayscale data. I currently have the following code to obtain the first plane's bytes:

            Image image = reader.acquireLatestImage();
            ByteBuffer buffer = image.getPlanes()[0].getBuffer();
            byte[] bytes = new byte[buffer.remaining()];
            buffer.get(bytes);

Hoping there is BoofCV method that accepts these bytes and returns a GrayU8. Any help is greatly appreciated.

- Ken





kha...@gmail.com

unread,
Mar 10, 2017, 9:24:10 PM3/10/17
to BoofCV
Maybe as simple as this?

            Image image = reader.acquireLatestImage();
            Image.Plane yPlane = image.getPlanes()[0];
            ByteBuffer buffer = yPlane.getBuffer();
            byte[] bytes = new byte[buffer.remaining()];
            buffer.get(bytes);
            
            GrayU8 ret = new GrayU8();
            ret.width = image.getWidth();
            ret.height = image.getHeight();
            ret.startIndex = 0;
            ret.stride = yPlane.getRowStride();
            ret.data = bytes;

Or is this wishful thinking?

kha...@gmail.com

unread,
Mar 11, 2017, 10:20:08 AM3/11/17
to BoofCV
So it turns out that my phone (Galaxy S5) uses YV12, not YUV_420_888. [http://stackoverflow.com/questions/34717969/imagereader-format-overridden-in-some-devices] But grabbing the first plane's data and passing directly into the GrayU8 mostly works. The image is rotated 90 degrees counter-clockwise, appears stretched horizontally and is a horizontal mirror image.

Peter A

unread,
Mar 15, 2017, 2:54:24 PM3/15/17
to boofcv
thanks for bringing this up.  Not totally shocked that you are seeing a weird distortion.  The rotation is device dependent but you can figure out how to rotate it by looking at the camera's info.  The latest version of the library handles that automatically with the old camera API.  

The stretching makes me think they might only do a subsampling in the planar gray format.  Some image formats do that.  I would need to read up on the specs.  I'm a bit busy at the moment to fix this myself but can help if you want to do the leg work in reading up on the format.

- Peter

--
You received this message because you are subscribed to the Google Groups "BoofCV" group.
To unsubscribe from this group and stop receiving emails from it, send an email to boofcv+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
"Now, now my good man, this is no time for making enemies."    — Voltaire (1694-1778), on his deathbed in response to a priest asking that he renounce Satan.
Reply all
Reply to author
Forward
0 new messages