错误CV_8UC1函数void自适应阈值OpenCV android

时间:2014-05-13 08:23:55

标签: android eclipse opencv image-processing

我正在使用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上没有发现错误,但是当我在模拟器上运行时,它会给出错误“UNFORTUNATELY APPS已经停止工作”,日志猫显示:

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

1 个答案:

答案 0 :(得分:0)

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

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

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

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

相关问题