android - 函数无效自适应阈值OpenCV android中的错误CV_8UC1

标签 android eclipse opencv image-processing

我在我的图像处理 Android OCR 应用程序中使用 OpenCV,我需要通过 OpenCV 使用自适应阈值函数获取二值图像,我的代码如下所示:

  String SourcePicture = cursor.getString(URIPicture);

  Bitmap ImagePicker = BitmapFactory.decodeFile(SourcePicture);
  Bitmap InputImagePicker = ImagePicker.copy(Bitmap.Config.ARGB_8888, true);

  Mat MatrixBitmapSmooth = Utils.bitmapToMat(InputImagePicker);                   
  Mat MatrixBitmapBiner = new Mat();
  Imgproc.adaptiveThreshold(MatrixBitmapSmooth, MatrixBitmapBiner, 255,    
         Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 15, 4);

  Bitmap BitmapBiner = null;
  Utils.matToBitmap(MatrixBitmapBiner, BitmapBiner);
  ImageInput.setImageBitmap(ImagePicker);

看起来不错,在 Eclipse 上没有发现错误,但是当我在模拟器上运行它时,它给出错误“不幸的应用程序已停止工作”,日志猫显示:

05-12 01:09:55.425: E/AndroidRuntime(1164): Caused by: CvException [org.opencv.core.CvException: /home/andreyk/OpenCV2/trunk/opencv_2.3.1.b2/modules/imgproc/src/thresh.cpp:555: error: (-215) src.type() == CV_8UC1 in function void cv::adaptiveThreshold(const cv::_InputArray&, const cv::_OutputArray&, double, int, int, int, double)

我的代码有什么问题???如果我的代码有问题???你能帮帮我吗???

编辑

我从 Mr.Roger Rowland 得到了建议该图像输入必须是单 channel 灰度图像,所以我编辑了我的代码,如下所示:

String SourcePicture = cursor.getString(URIPicture);
Bitmap InputImagePicker = BitmapFactory.decodeFile(SourcePicture);

Mat MatrixBitmapSmooth = new 
        Mat(InputImagePicker.getWidth(),InputImagePicker.getHeight(), CvType.CV_8UC1);
MatrixBitmapSmooth = Utils.bitmapToMat(InputImagePicker);                 

Mat MatrixBitmapBiner = new                
        Mat(InputImagePicker.getWidth(),InputImagePicker.getHeight(), CvType.CV_8UC1);
MatrixBitmapBiner = Utils.bitmapToMat(InputImagePicker);

Imgproc.cvtColor(MatrixBitmapBiner, MatrixBitmapBiner, Imgproc.COLOR_RGB2GRAY);
Imgproc.cvtColor(MatrixBitmapSmooth, MatrixBitmapSmooth, Imgproc.COLOR_BGR2GRAY); 
Imgproc.adaptiveThreshold(MatrixBitmapSmooth, MatrixBitmapBiner, 255,       
                        Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 15, 8); 

Bitmap BitmapBiner = null;
Utils.matToBitmap(MatrixBitmapBiner, BitmapBiner);
BitmapBiner = BitmapBiner.copy(Bitmap.Config.ARGB_8888, true);
ImageOutput.setImageBitmap(BitmapBiner);

消失前的错误logcat:

05-12 01:09:55.425: E/AndroidRuntime(1164): Caused by: CvException [org.opencv.core.CvException: /home/andreyk/OpenCV2/trunk/opencv_2.3.1.b2/modules/imgproc/src/thresh.cpp:555: error: (-215) src.type() == CV_8UC1 in function void cv::adaptiveThreshold(const cv::_InputArray&, const cv::_OutputArray&, double, int, int, int, double)

但随后显示错误:

05-14 00:18:40.252: E/AndroidRuntime(1371): Caused by: java.lang.NullPointerException

最佳答案

此外,创建一个 4 channel 垫。

Mat MatrixBitmapSmooth = new 
    Mat(InputImagePicker.getWidth(),InputImagePicker.getHeight(), CvType.CV_8UC4);

您需要将 RGBA 转换为灰色。使用 COLOR_RGBA2GRAY

执行 adaptiveThreshold 后,如果要将其转换回位图,您可能希望将其转换回 RGBA,因此请使用 COLOR_GRAY2RGBA

关于android - 函数无效自适应阈值OpenCV android中的错误CV_8UC1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23626130/

相关文章:

java - 无法在 Apple MacPro 上启动新的 Android 应用程序

opencv - OpenCV的拼接模块是否可以拼接平行运动相机拍摄的图像?

安卓生命体征。 "Session"和 "crash free users"

android - java.lang.ClassCastException : android. app.Application 无法转换为 greendroid.app.GDApplication

eclipse - 在 eclipse mars 中安装新插件时,配置文件 ID _SELF_ 未注册错误

java - Eclipse 不适用于 java 8 和 win 8

python线程: only one Python thread is running

android - Activity 在图像处理期间自动关闭(android)

android - 使用 Realm + Okhttp 时无法获取崩溃的确切原因

android - Firebase 日期格式 - 桥接 IoS 和 Android 的最佳实践