Получить центральный пиксель из изображения в формате YUV - PullRequest
0 голосов
/ 04 апреля 2019

Я сделал преобразование изображения из YUV (NV21 Android) в RGB

И я попытался получить цвет пикселя на х, у кординаты Например, я пытался получить пиксель из центра. Но я получаю цвет из неправильного места.

Собрать изображение 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();

Что у меня есть в текущий момент.

// 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;

Я ожидаю, что цвет от центра изображения. Но у меня есть цвет из другого места

1 Ответ

0 голосов
/ 05 апреля 2019

https://github.com/lirugo/screenDetector

Я нашел решение, если кто-то хочет больше, см. Мой код по ссылке, чтобы получить пиксель с координатами x, y, код ниже

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);
...