-1

I made image converting from YUV(NV21 Android) to RGB

And i tried get color of pixel by x,y cordinates. For example i tried get pixel from center. But i get color from wrong place.

Collect image YUV

ByteBuffer yBuff = image.getPlanes()[0].getBuffer();
ByteBuffer uBuff = image.getPlanes()[1].getBuffer();
ByteBuffer vBuff = image.getPlanes()[2].getBuffer();
ByteArrayOutputStream  outputStream = new ByteArrayOutputStream();
byte[] yByte = new byte[yBuff.remaining()];
byte[] uByte = new byte[uBuff.remaining()];
byte[] vByte = new byte[vBuff.remaining()];
yBuff.get(yByte);
uBuff.get(uByte);
vBuff.get(vByte);

// Create converting byte[] NV21
try {
    outputStream.write(yByte);
    for (int i=0; i < uByte.length; i++) {
        outputStream.write(vByte[i]);
        outputStream.write(uByte[i]);
    }
} catch (Exception e) {
    e.printStackTrace();
}
byte[] imageBytes = outputStream.toByteArray();

What i have in current moment.

// Coordinates
x = width / 2;
y = height / 2;

// Get YUV coordinates  for x y position                        
int YCord     = y * width + x;
int UCord     = (y >> 1) * (width) + x + total + 1;
int VCord     = (y >> 1) * (width) + x + total;

// Get YUV colors
int Y = (0xFF & imageBytes[YCord]) - 16;
int U = (0xFF & imageBytes[UCord]) - 128;
int V = (0xFF & imageBytes[VCord]) - 128;

I expect color from center of image. But i have color from different place

lirugo
  • 41
  • 4
  • In this line `int UCord = (y >> 1) * (width) + x + total + 1;` you take account of the y resolution being halved by shifting, but you don't do so for `x`. I think you need to use `x>>1` rather than `x`. – Mark Setchell Apr 04 '19 at 11:01

1 Answers1

0

https://github.com/lirugo/screenDetector

i found solution if some one want more, see my code by link about get pixel by x,y coordinates code below

x = 210; // might be 250
y = height - 215; // might be 150

// Get YUV coordinates  for x y position
int YCord     = y*width+x;
int UCord     = ((y/2)*width+(x&~1)+total+1);
int VCord     = ((y/2)*width+(x&~1)+total);
lirugo
  • 41
  • 4