[英]Convert OpenCV mat to Android Bitmap
我正在尝试将OpenCV垫转换为android位图,但这给我的图像带蓝色调(黄色变成蓝色)! 即使我没有对图像进行任何处理! 我不知道为什么会这样。 以下是一些相关代码:
File file = new File(imgDecodableString);
image = Imgcodecs.imread(file.getAbsolutePath(),Imgcodecs.CV_LOAD_IMAGE_COLOR);
resultBitmap = Bitmap.createBitmap(image.cols(), image.rows(),Bitmap.Config.ARGB_8888);;
Utils.matToBitmap(image, resultBitmap);
Bitmap mResult = resultBitmap;
ImageView imgView = (ImageView) findViewById(R.id.imgView);
imgView.setImageBitmap(mResult);
//imgView.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString));
我是android应用程序开发的新手,所以我可能缺少一些简单的东西。 感谢帮助!
编辑:
我正在上传图像以供参考。
疑似问题在于RGB
颜色约定,Android遵循RGB
颜色约定,但OpenCV遵循BGR
颜色约定,您可以使用Imgproc.cvtColor(mat, Imgproc.COLOR_BGR2RGBA)
对其进行纠正,然后再将其显示在ImageView
。
根据给出的建议,我制作了一个将Mat转换为Bitmap的函数。 此功能运行完美。
private static Bitmap convertMatToBitMap(Mat input){
Bitmap bmp = null;
Mat rgb = new Mat();
Imgproc.cvtColor(input, rgb, Imgproc.COLOR_BGR2RGB);
try {
bmp = Bitmap.createBitmap(rgb.cols(), rgb.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(rgb, bmp);
}
catch (CvException e){
Log.d("Exception",e.getMessage());
}
return bmp;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.