Ask Your Question

Using adaptative thresshold on android.

asked 2014-05-15 18:03:42 -0500

updated 2014-05-16 00:45:54 -0500

berak gravatar image

I am trying to use an adaptative thresshold to binarize one image using the open cv library. The code is the following:

public Bitmap filter(Bitmap image){

    Log.d(TAG, " Filtering ... ");

    Mat inputMat = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8U/*.CV_8UC1*/);
    Utils.bitmapToMat(image, inputMat);

    Mat inputMat2 = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8U);     
    Mat outputMat = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8U);

    Log.d(TAG, " Pre Filtering ... ");

    Imgproc.adaptiveThreshold(inputMat, outputMat, 50, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 5, 25);

    // Photo.fastNlMeansDenoisingColored(inputMat, outputMat);
    Log.d(TAG, " Filtering Done... ");

    Bitmap output = Bitmap.createBitmap(image.getWidth(), image.getHeight(), Bitmap.Config.RGB_565);
    Utils.matToBitmap(outputMat, output);

    return output;  

I had tried several types as CV_8UC1 or CV_8SC1, but all always cause the same error at executing the function:

05-16 00:50:33.125: E/AndroidRuntime(1892): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=102, result=-1, data=Intent { dat=content://media/external/images/media/15312 (has extras) }} to activity {com.medicasafe.images/com.medicasafe.images.ImageDecodingActivity}: CvException [org.opencv.core.CvException: cv::Exception: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/imgproc/src/thresh.cpp:796: error: (-215) src.type() == CV_8UC1 in function void cv::adaptiveThreshold(cv::InputArray, cv::OutputArray, double, int, int, int, double)

The method is executed in onActivityResult() after the user select one image from gallery.

edit retag flag offensive close merge delete


convert inputMat to grayscale before applying adaptiveThreshold, it does not work on 3 channel images

berak gravatar imageberak ( 2014-05-16 00:44:08 -0500 )edit

1 answer

Sort by » oldest newest most voted

answered 2014-05-16 12:15:20 -0500

Thank you, issue resolved. The bitmap was actually a gray image but openCv seems that it´s considering all the images as RGB when using the BitmapToMat method, so I needed to se cvColor.

Thank you.

edit flag offensive delete link more

Question Tools


Asked: 2014-05-15 18:03:42 -0500

Seen: 2,248 times

Last updated: May 15 '14