在Android上的OpenCV中在Bitmap和Mat之间进行转换的正确方法?

Ant*_*ton 5 java android opencv

我目前正在尝试将一些遗留代码从iPhone迁移到Android.此代码使用OpenCV库进行一些图像处理.我无法理解如何在Mat和Android Bitmap类之间进行转换.

此代码显示了一个非常简化的示例,它将位图加载到Mat中,然后将其转换回Bitmap.生成的图像看起来很奇怪 - 它充满了蓝色和白色像素.原来是普通的PNG图像......

  Mat img = Utils.loadResource(context, resId);
  Bitmap tmp = Bitmap.createBitmap(img.rows(), img.cols(),  
  Bitmap.Config.ARGB_8888);               
  Utils.matToBitmap(img, tmp);
Run Code Online (Sandbox Code Playgroud)

gre*_*ven 13

目前matToBitmap有点儿麻烦,我读过他们打算在将来的版本中修复它.

我可以向它展示我是如何为彩色图像工作的:

mMat = Utils.loadResource(this, resId, Highgui.CV_LOAD_IMAGE_COLOR);
Imgproc.cvtColor(mMat, result, Imgproc.COLOR_RGB2BGRA);
bmp = Bitmap.createBitmap(result.cols(), result.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(result, bmp);
mImageView.setImageBitmap(bmp);
Run Code Online (Sandbox Code Playgroud)

基本上我首先执行那个空间颜色转换,或者像你说的那样,结果将是随机颜色的奇怪混合.

如果它有效,请回答,我认为确实如此.