0

I'm receiving YUV420 image data in byte[] on every onDrawFrame(). I need to find out the colors present in the image from the byte array given. How can I extract U and V value for each pixel and use them to determine if the specific color is present in that image (something like HSV color range).

I've separated UV array from original image data like this:

byte[] data = image.getData();
int inputYLength = image.getWidth() * image.getHeight();
int inputUVLength = image.getWidth() * image.getHeight() / 2;
ByteBuffer uvBuffer = ByteBuffer.allocateDirect(inputUVLength);

uvBuffer.put(image.getData(), inputYLength, inputUVLength);
uvBuffer.position(0);

uvBuffer holds byte values for U and V components. How to use this for color detection?

sneharc
  • 493
  • 3
  • 21
  • The size of the image is around 700x500 and the length of `uvBuffer` is above 100000. So sampling this much data on every `onDrawFrame()` is not feasible at all also the formulas mentioned require some other calculations too. I'm trying my head around `YuvImage` and `OpenCV` – sneharc Dec 18 '18 at 06:29

1 Answers1

0

The U and V components of your image are down-sampled relative to the Y component. So the first step is either:

  • to upsample the U and V to match the Y, or
  • down-sample the Y to match the U and V.

Which one you do depends on what resolution you need for the final phase of determining the Hue.

The second step is to convert the YUV from the first step into RGB.

The third step is to convert the RGB from the second step into HSL (Hue, Saturation and Lightness).

The final step is to select the H (Hue) corresponding to the colours you want.

This answer links to the formulae you need.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432