将 YUV 图像转换为灰度图像 - 与 RGB 转换为灰度的结果相同吗?
Convert YUV Image into greyscale Image - Same Result as RGB to Grayscale?
我想对 YUV_420_888 图像进行一些图像处理,并且需要它的灰度版本。当我读到 YUV 图像时,它应该足以提取图像的 Y 平面。在 Android 中,我将尝试使用此工作流程将 Y 平面转换为字节数组。
Image.Plane Y = img.getPlanes()[0];
ByteBuffer byteBuffer = Y.getBuffer();
byte[] data = new byte[byteBuffer.remaining()];
byteBuffer.get(data);
因此,当我想将我现在得到的这张图像与另一张灰度图像(或至少是图像处理的结果)进行比较时,我有一个问题,我得到的提取 Y 平面的灰度图像是否几乎相同作为变成灰度的 RGB?还是我必须为此做一些额外的处理步骤?
是的,你从 Y 平面得到的 data
应该和你通过 RGB 图像得到的一样。
不,我使用的是红外传感器,我在其中得到一张已经是灰度的 YUV_420_888 图像。但是为了以字节为单位转换它,我使用了以下函数,这给了我错误。根据你的回答,我只拍了 Y 平面,结果它给了我绿屏。
ByteBuffer[] buffer = new ByteBuffer[1];
Image image = reader.acquireNextImage();
buffer[0] = image.getPlanes()[0].getBuffer().duplicate();
//buffer[1] = image.getPlanes()[1].getBuffer().duplicate();
int buffer0_size = buffer[0].remaining();
//int buffer1_size = buffer[1].remaining();
buffer[0].clear();
//buffer[1].clear();
byte[] buffer0_byte = new byte[buffer0_size];
//byte[] buffer1_byte = new byte[buffer1_size];
buffer[0].get(buffer0_byte, 0, buffer0_size);
//buffer[1].get(buffer1_byte, 0, buffer1_size);
byte[] byte2 = buffer0_byte;
//byte2=buffer0_byte;
//byte2[1]=buffer1_byte;
image.close();
mArrayImageBuffer.add(byte2);
将字节出队并转到函数后:
public static byte[] convertYUV420ToNV12(byte[] byteBuffers){
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
outputStream.write( byteBuffers);
//outputStream.write( byteBuffers[1] );
} catch (IOException e) {
e.printStackTrace();
}
// outputStream.write( buffer2_byte );
byte[] rez = outputStream.toByteArray();
return rez;
}
我想对 YUV_420_888 图像进行一些图像处理,并且需要它的灰度版本。当我读到 YUV 图像时,它应该足以提取图像的 Y 平面。在 Android 中,我将尝试使用此工作流程将 Y 平面转换为字节数组。
Image.Plane Y = img.getPlanes()[0];
ByteBuffer byteBuffer = Y.getBuffer();
byte[] data = new byte[byteBuffer.remaining()];
byteBuffer.get(data);
因此,当我想将我现在得到的这张图像与另一张灰度图像(或至少是图像处理的结果)进行比较时,我有一个问题,我得到的提取 Y 平面的灰度图像是否几乎相同作为变成灰度的 RGB?还是我必须为此做一些额外的处理步骤?
是的,你从 Y 平面得到的 data
应该和你通过 RGB 图像得到的一样。
不,我使用的是红外传感器,我在其中得到一张已经是灰度的 YUV_420_888 图像。但是为了以字节为单位转换它,我使用了以下函数,这给了我错误。根据你的回答,我只拍了 Y 平面,结果它给了我绿屏。
ByteBuffer[] buffer = new ByteBuffer[1];
Image image = reader.acquireNextImage();
buffer[0] = image.getPlanes()[0].getBuffer().duplicate();
//buffer[1] = image.getPlanes()[1].getBuffer().duplicate();
int buffer0_size = buffer[0].remaining();
//int buffer1_size = buffer[1].remaining();
buffer[0].clear();
//buffer[1].clear();
byte[] buffer0_byte = new byte[buffer0_size];
//byte[] buffer1_byte = new byte[buffer1_size];
buffer[0].get(buffer0_byte, 0, buffer0_size);
//buffer[1].get(buffer1_byte, 0, buffer1_size);
byte[] byte2 = buffer0_byte;
//byte2=buffer0_byte;
//byte2[1]=buffer1_byte;
image.close();
mArrayImageBuffer.add(byte2);
将字节出队并转到函数后:
public static byte[] convertYUV420ToNV12(byte[] byteBuffers){
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
outputStream.write( byteBuffers);
//outputStream.write( byteBuffers[1] );
} catch (IOException e) {
e.printStackTrace();
}
// outputStream.write( buffer2_byte );
byte[] rez = outputStream.toByteArray();
return rez;
}