在我的方法中,我使用 OpenCV Mat 和脚本
https://gist.github.com/camdenfullmer/dfd83dfb0973663a7974
首先,使用上面链接中的代码将 YUV_420_888 图像转换为 Mat。
*mImage 是我在 ImageReader.OnImageAvailableListener 中获得的 Image 对象
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;
}
我们有 1 个通道的 YUV Mat。为 BGR(还不是 RGB)图像定义新的 Mat:
Mat bgrMat = new Mat(mImage.getHeight(), mImage.getWidth(),CvType.CV_8UC4);
我刚开始学习 OpenCV,所以这不一定是 4 通道 Mat,而是可以是 3 通道,但它适用于我。
现在我使用转换颜色方法将我的 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);
}
}
});