位图与垫之间的OpenCV的Andr​​oid上正确的方式来转换?位图、正确、方式、Andr

2023-09-06 13:22:51 作者:尛侽亽旳傲气

目前,我正在试图一点点遗留code,从iPhone移植到Android。这code使用了OpenCV库做一些图像处理。 而且我不明白怎么做垫和Android位图类之间的转换。

这code显示了加载一个位图到垫非常简单的例子,然后将其转换回位图。由此产生的图像看起来奇怪 - 它充满了蓝色和白色像素。而原本​​是一个正常的PNG图像...

 垫IMG = Utils.loadResource(背景下,渣油);
  位图TMP = Bitmap.createBitmap(img.rows(),img.cols(),
  Bitmap.Config.ARGB_8888);
  Utils.matToBitmap(IMG,TMP);
 

解决方案

目前matToBitmap是有点马车,我读过他们打算修复它在未来的版本。

我可以告诉它怎么工作围绕它的彩色图像:

  mMat = Utils.loadResource(这一点,渣油,Highgui.CV_LOAD_IMAGE_COLOR);
Imgproc.cvtColor(mMat,结果,Imgproc.COLOR_RGB2BGRA);
BMP = Bitmap.createBitmap(result.cols(),result.rows(),Bitmap.Config.ARGB_8888);
Utils.matToBitmap(结果,BMP);
mImageView.setImageBitmap(BMP);
 

基本上我第一次执行太空色彩转换或者像你说的,结果将是混合的随机颜色怪怪的。

答,如果它的工作原理,我认为它。

I'm currently trying to migrate a bit of legacy code from iPhone to Android. This code uses the OpenCV library to do some image processing. And I cannot understand how to do the conversion between Mat and Android Bitmap classes.

This code shows a very simplified example which loads a bitmap into a Mat, then converts it back to Bitmap. The resulting image looks weird - it's filled with blue and white pixels. And the original is a normal PNG image...

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

解决方案

Currently matToBitmap is a bit buggy, I've read they intend to fix it in a future release.

I can show it how I worked around it for a color image:

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);

Basically i perform that space color conversion first or like you said the result will be a weird blend of random colors.

Answer if it works, I think it does.