我正在编写一款应用程序,它使用相机捕获的数据流并将其转换为RGB格式以进行处理。
在旧的相机实现中,该应用程序可以正常运行,因为使用了NV21 YUV格式。但是在新的YUV格式YUV_420_888下,出现了问题。在使用新的 Camera2 API 时,由于该API发送的是YUV_420_888格式而不是NV21(YUV_420_SP)格式,所以图像无法正确转换为RGB。
请问有谁能告诉我如何将YUV_420_888转换为RGB呢?
谢谢。
我正在编写一款应用程序,它使用相机捕获的数据流并将其转换为RGB格式以进行处理。
在旧的相机实现中,该应用程序可以正常运行,因为使用了NV21 YUV格式。但是在新的YUV格式YUV_420_888下,出现了问题。在使用新的 Camera2 API 时,由于该API发送的是YUV_420_888格式而不是NV21(YUV_420_SP)格式,所以图像无法正确转换为RGB。
请问有谁能告诉我如何将YUV_420_888转换为RGB呢?
谢谢。
在Java中将Camera2 YUV_420_888转换为RGB Mat(使用opencv)
@Override
public void onImageAvailable(ImageReader reader){
Image image = null;
try {
image = reader.acquireLatestImage();
if (image != null) {
byte[] nv21;
ByteBuffer yBuffer = mImage.getPlanes()[0].getBuffer();
ByteBuffer uBuffer = mImage.getPlanes()[1].getBuffer();
ByteBuffer vBuffer = mImage.getPlanes()[2].getBuffer();
int ySize = yBuffer.remaining();
int uSize = uBuffer.remaining();
int vSize = vBuffer.remaining();
nv21 = new byte[ySize + uSize + vSize];
//U and V are swapped
yBuffer.get(nv21, 0, ySize);
vBuffer.get(nv21, ySize, vSize);
uBuffer.get(nv21, ySize + vSize, uSize);
Mat mRGB = getYUV2Mat(nv21);
}
} catch (Exception e) {
Log.w(TAG, e.getMessage());
}finally{
image.close();// don't forget to close
}
}
public Mat getYUV2Mat(byte[] data) {
Mat mYuv = new Mat(image.getHeight() + image.getHeight() / 2, image.getWidth(), CV_8UC1);
mYuv.put(0, 0, data);
Mat mRGB = new Mat();
cvtColor(mYuv, mRGB, Imgproc.COLOR_YUV2RGB_NV21, 3);
return mRGB;
}
Mat mYuvMat = imageToMat(mImage);
public static Mat imageToMat(Image image) {
ByteBuffer buffer;
int rowStride;
int pixelStride;
int width = image.getWidth();
int height = image.getHeight();
int offset = 0;
Image.Plane[] planes = image.getPlanes();
byte[] data = new byte[image.getWidth() * image.getHeight() * ImageFormat.getBitsPerPixel(ImageFormat.YUV_420_888) / 8];
byte[] rowData = new byte[planes[0].getRowStride()];
for (int i = 0; i < planes.length; i++) {
buffer = planes[i].getBuffer();
rowStride = planes[i].getRowStride();
pixelStride = planes[i].getPixelStride();
int w = (i == 0) ? width : width / 2;
int h = (i == 0) ? height : height / 2;
for (int row = 0; row < h; row++) {
int bytesPerPixel = ImageFormat.getBitsPerPixel(ImageFormat.YUV_420_888) / 8;
if (pixelStride == bytesPerPixel) {
int length = w * bytesPerPixel;
buffer.get(data, offset, length);
if (h - row != 1) {
buffer.position(buffer.position() + rowStride - length);
}
offset += length;
} else {
if (h - row == 1) {
buffer.get(rowData, 0, width - pixelStride + 1);
} else {
buffer.get(rowData, 0, rowStride);
}
for (int col = 0; col < w; col++) {
data[offset++] = rowData[col * pixelStride];
}
}
}
}
Mat mat = new Mat(height + height / 2, width, CvType.CV_8UC1);
mat.put(0, 0, data);
return mat;
}
Mat bgrMat = new Mat(mImage.getHeight(), mImage.getWidth(),CvType.CV_8UC4);
我刚开始学习OpenCV,所以这可能不需要是4通道的Mat,而可以是3通道的,但对我来说它能够工作。现在我使用convert color方法将我的yuv Mat转换为bgr Mat。
Imgproc.cvtColor(mYuvMat, bgrMat, Imgproc.COLOR_YUV2BGR_I420);
现在我们可以进行所有与图像处理相关的操作,如查找轮廓、颜色、圆等。为了将图像打印回屏幕上,我们需要将其转换为位图:
Mat rgbaMatOut = new Mat();
Imgproc.cvtColor(bgrMat, rgbaMatOut, Imgproc.COLOR_BGR2RGBA, 0);
final Bitmap bitmap = Bitmap.createBitmap(bgrMat.cols(), bgrMat.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(rgbaMatOut, bitmap);
我将所有的图像处理放在了一个单独的线程中,因此要设置我的ImageView,我需要在UI线程上执行此操作。
runOnUiThread(new Runnable() {
@Override
public void run() {
if(bitmap != null) {
mImageView.setImageBitmap(bitmap);
}
}
});
Shyam Kumar的答案对我的手机不适用,但Daniel Więcek的答案是正确的。我进行了调试,发现planes[i].getRowStride()为1216,planes[i].getPixelStride()为2。而图像的宽度和高度都是1200。
由于我的声望只有3,所以我无法评论,但可以发布答案。
你尝试过使用这个脚本吗?这是yydcdut在this问题中发布的答案。
https://github.com/pinguo-yuyidong/Camera2/blob/master/camera2/src/main/rs/yuv2rgb.rs
我遇到了完全相同的问题,我的代码从OnPreviewFrame()获取旧的YUV_420_SP格式的byte[]数据并将其转换为RGB。
关键在于byte[]中的“旧”数据是YYYYYY ... CrCbCrCbCrCb,而来自Camera2 API的“新”数据分为3个平面:0=Y,1=Cb,2=Cr。从这里,您可以获得每个字节数组。因此,您需要做的就是重新排序新数据作为与“旧”格式匹配的单个数组,您可以将其传递给现有的toRGB()函数:
Image.Plane[] planes = image.getPlanes(); // in YUV220_888 format
int acc = 0, i;
ByteBuffer[] buff = new ByteBuffer[planes.length];
for (i = 0; i < planes.length; i++) {
buff[i] = planes[i].getBuffer();
acc += buff[i].capacity();
}
byte[] data = new byte[acc],
tmpCb = new byte[buff[1].capacity()] , tmpCr = new byte[buff[2].capacity()];
buff[0].get(data, 0, buff[0].capacity()); // Y
acc = buff[0].capacity();
buff[2].get(tmpCr, 0, buff[2].capacity()); // Cr
buff[1].get(tmpCb, 0, buff[1].capacity()); // Cb
for (i=0; i<tmpCb.length; i++) {
data[acc] = tmpCr[i];
data[acc + 1] = tmpCb[i];
acc++;
}
现在,data[] 的格式与旧的 YUV_420_SP 格式相同。
(希望这能帮助一些人,尽管已经过去了几年..)
Image image = reader.acquireLatestImage();
...
Mat yuv = new Mat(image.getHeight() + image.getHeight() / 2, image.getWidth(), CvType.CV_8UC1);
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
final byte[] data = new byte[buffer.limit()];
buffer.get(data);
yuv.put(0, 0, data);
...
image.close();
image.getPlanes(0)
)实际上是完整的NV21直接缓冲区。但这种情况很少见。 - Alex Cohn