我正在尝试使用以下代码将MAt转换为位图:
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Mat tmp = new Mat (width,height,CvType.CV_8UC1,new Scalar(4));
try {
//Imgproc.cvtColor(seedsImage, tmp, Imgproc.COLOR_RGB2BGRA);
Imgproc.cvtColor(seedsImage, tmp, Imgproc.COLOR_GRAY2RGBA, 4);
Utils.matToBitmap(tmp, bmp);}
catch (CvException e){Log.d("Exception",e.getMessage());}
Run Code Online (Sandbox Code Playgroud)
我的seedsImage是一个Mat对象.而Exception和got是10-09 22:15:09.418: D/Exception(2461): ..\..\modules\java\generator\src\cpp\utils.cpp:105: error: (-215) src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols in function void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, _jclass*, jlong, _jobject*, jboolean)
我试图搜索但没有解决方案适合我.任何人都可以帮忙吗?
我想简单地将位图从Android转换为OpenCV的Mat对象.Stack Overflow上经常讨论此主题.例如:
将Mat转换为Android的Bitmap Opencv ;
使用android相机捕获图像后将Bitmap转换为Mat ;
templateMatching 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;
Run Code Online (Sandbox Code Playgroud)
(注意:图像是提供给这行代码的位图)
错误:
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 …Run Code Online (Sandbox Code Playgroud) 我正在开发一个应用程序来检测病变区域,为此我使用抓取来检测 ROI 并从图像中删除背景。但是,在某些图像中,它运行不佳。他最终没有很好地确定感兴趣区域的边界。分水岭可以更好地识别此类工作的边缘,但是我在从抓地到分水岭的过渡过程中遇到了困难。在处理抓取之前,用户使用 touchevent 在感兴趣的图像(伤口区域)周围标记一个矩形,以方便算法的工作。如下图。

但是,使用其他伤口图像,分割效果不佳,显示出 ROI 检测的缺陷。
在应用程序中使用抓取的图像
在桌面中使用分水岭的图像

这是代码:
private fun extractForegroundFromBackground(coordinates: Coordinates, currentPhotoPath: String): String {
// TODO: Provide complex object that has both path and extension
val width = bitmap?.getWidth()!!
val height = bitmap?.getHeight()!!
val rgba = Mat()
val gray_mat = Mat()
val threeChannel = Mat()
Utils.bitmapToMat(bitmap, gray_mat)
cvtColor(gray_mat, rgba, COLOR_RGBA2RGB)
cvtColor(rgba, threeChannel, COLOR_RGB2GRAY)
threshold(threeChannel, threeChannel, 100.0, 255.0, THRESH_OTSU)
val rect = Rect(coordinates.first, coordinates.second)
val fg = Mat(rect.size(), CvType.CV_8U)
erode(threeChannel, fg, Mat(), Point(-1.0, -1.0), 10)
val …Run Code Online (Sandbox Code Playgroud) 注意:我帖子中的所有信息仅适用于 Samsung Galaxy S7 设备。我不知道模拟器和其他设备的行为如何。
在 onImageAvailable 中,我将每个图像连续转换为 NV21 字节数组,并将其转发到需要原始 NV21 格式的 API。
这是我初始化图像读取器并接收图像的方式:
private void openCamera() {
...
mImageReader = ImageReader.newInstance(WIDTH, HEIGHT,
ImageFormat.YUV_420_888, 1); // only 1 for best performance
mImageReader.setOnImageAvailableListener(
mOnImageAvailableListener, mBackgroundHandler);
...
}
private final ImageReader.OnImageAvailableListener mOnImageAvailableListener
= new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
Image image = reader.acquireLatestImage();
if (image != null) {
byte[] data = convertYUV420ToNV21_ALL_PLANES(image); // this image is turned 90 deg using front cam in portrait mode
byte[] data_rotated = rotateNV21_working(data, …Run Code Online (Sandbox Code Playgroud)