【问题标题】:Error CV_8UC1 in function void adaptive threshold OpenCV android函数void自适应阈值OpenCV android中的错误CV_8UC1
【发布时间】:2014-06-30 19:47:51
【问题描述】:

我将 OpenCV 用于我的图像处理 Android OCR 应用程序,我需要使用 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 上没有发现错误,但是当我在模拟器上运行它时,它给出错误“不幸的应用程序已停止工作”,日志 cat 显示:

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 那里得到建议,图像输入必须是单通道灰度图像,所以我编辑了我的代码,如下所示:

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

【问题讨论】:

  • 错误告诉你adaptiveThreshold需要一个单通道灰度图像作为输入,也就是what the OpenCV documentation says
  • 我已经编辑了我的代码,我添加了这个代码:Imgproc.cvtColor(MatrixBitmapBiner, MatrixBitmapBiner, Imgproc.COLOR_RGB2GRAY);..........但它显示错误:“导致by: java.lang.NullPointerException",.....就像我上面的更新问题一样,谢谢
  • 可能 BitmapBiner.copy(Bitmap.Config.ARGB_8888, true) 返回 null - 我猜是因为不支持从灰度转换为 ARGB_888。也许你需要提出一个新的、具体的问题。
  • 我已经删除了BitmapBiner.copy(Bitmap.Config.ARGB_8888, true),然后是ImageOutput.setImageBitmap(BitmapBiner);的结果,....什么都不显示,怎么了???

标签: android eclipse opencv image-processing


【解决方案1】:

另外,创建一个 4 通道 Mat。

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-03
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    • 2013-01-23
    • 1970-01-01
    • 2018-07-06
    相关资源
    最近更新 更多