我想简单地将位图从 Android 转换为用于 OpenCV 的 Mat 对象。这个话题经常在 Stack Overflow 上得到解决。例如:
将 Mat 转换为 Android 的 Bitmap Opencv;
使用android相机捕获图像后将Bitmap转换为Mat;
模板匹配 mattoBitmap opencv for android
还有更多的东西可以找到。我按照这个答案中的说明进行操作,但我仍然无法以正确的方式完成。
最小代码:
//First convert Bitmap to Mat
Mat ImageMat = new Mat ( image.getHeight(), image.getWidth(), CvType.CV_8U, new Scalar(4));
Bitmap myBitmap32 = image.copy(Bitmap.Config.ARGB_8888, true);
Utils.bitmapToMat(myBitmap32, ImageMat);
//Do smth.
Imgproc.cvtColor(ImageMat, ImageMat, Imgproc.COLOR_RGB2GRAY,4);
//Then convert the processed Mat to Bitmap
Bitmap resultBitmap = Bitmap.createBitmap(ImageMat.cols(), ImageMat.rows(),Bitmap.Config.ARGB_8888);;
Utils.matToBitmap(ImageMat, resultBitmap);
//Set member to the Result Bitmap. This member is displayed in an ImageView
mResult = resultBitmap;
(注意:图像是提供给这行代码的位图)
错误:
08-07 15:13:59.188:E/AndroidRuntime(2115):致命异常:主要
08-07 15:13:59.188: E/AndroidRuntime(2115): java.lang.NoClassDefFoundError: org.opencv.core.Mat
但我的进口是:
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
//OpenCV
import org.opencv.android.Utils;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;
非常感谢任何形式的帮助。谢谢丹斯