how to remove black background from thresholding output in Opencv Android? [closed]

asked 2019-11-18 14:19:35 -0500

Fandi gravatar image

updated 2019-11-18 14:48:09 -0500

supra56 gravatar image

Hi, I am using OpenCV android library thresholding method for image segmentation, but the problem is that the output bitmap contains black background which I do not want please note that original image does not have any black background it is actually white. I am attaching the code for your reference, I am new to opencv and don't have much understanding about it also so kindly help me out. here code:

private void Segmentation() {

       Mat srcMat = new Mat();
        gray = new Mat();

        Utils.bitmapToMat(imageBmp, srcMat);
        Imgproc.cvtColor(srcMat, gray, Imgproc.COLOR_RGBA2GRAY);
        grayBmp = Bitmap.createBitmap(imageBmp.getWidth(), imageBmp.getHeight(), Bitmap.Config.RGB_565);
        Utils.matToBitmap(gray, grayBmp);


        Mat threshold = new Mat();
        Imgproc.threshold(gray, threshold, 0, 255, Imgproc.THRESH_BINARY_INV + Imgproc.THRESH_OTSU);
        thresBmp = Bitmap.createBitmap(imageBmp.getWidth(), imageBmp.getHeight(), Bitmap.Config.RGB_565);
        Utils.matToBitmap(threshold, thresBmp);

        Mat closing = new Mat();
        Mat kernel = Mat.ones(5, 5, CvType.CV_8U);
        Imgproc.morphologyEx(threshold, closing, Imgproc.MORPH_CLOSE, kernel, new Point(-1, -1), 3);
        closingBmp = Bitmap.createBitmap(imageBmp.getWidth(), imageBmp.getHeight(), Bitmap.Config.RGB_565);
        Utils.matToBitmap(closing, closingBmp);

        result = new Mat();
        Core.subtract(closing, gray, result);
        Core.subtract(closing, result, result);

        resultBmp = Bitmap.createBitmap(imageBmp.getWidth(), imageBmp.getHeight(), Bitmap.Config.RGB_565);
        Utils.matToBitmap(result, resultBmp);


image description

edit retag flag offensive reopen merge delete

Closed for the following reason duplicate question by berak
close date 2019-11-19 01:11:48.484413



duplicate post?

LBerger gravatar imageLBerger ( 2019-11-18 15:07:10 -0500 )edit

@Fandi, in the future, please rather update your previous question, than posting it all over again, thank you.

berak gravatar imageberak ( 2019-11-19 01:13:43 -0500 )edit