簡體   English   中英

從格式為 YUV 的圖像中獲取中心像素

[英]Get center pixel from image with format YUV

我將圖像從 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;

我期待圖像中心的顏色。 但我有不同地方的顏色

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM