从格式为YUV的图像中获取中心像素

问题描述 投票:-1回答:1

我将YUV(NV21 Android)的图像转换为RGB

我尝试通过x,y坐标获得像素的颜色。例如,我尝试从中心获取像素。但我从错误的地方得到颜色。

收集图像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;

我期待从图像中心的颜色。但我有来自不同地方的颜色

java image yuv
1个回答
0
投票

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);
© www.soinside.com 2019 - 2024. All rights reserved.