How to remove the background image

asked 2019-10-23 07:57:02 -0500

Fandi gravatar image

updated 2019-10-23 09:30:56 -0500

berak gravatar image

I'm working on the Opencv project on Android. When I segmented images, I didn't know how to remove the background. Here is the source code for image segmentation. `private void Threshold() {

    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(3, 3, CvType.CV_8U);
    Imgproc.morphologyEx(threshold, closing, Imgproc.MORPH_CLOSE, kernel, new Point(-1, -1), 2);
    closingBmp = Bitmap.createBitmap(imageBmp.getWidth(),imageBmp.getHeight(),Bitmap.Config.RGB_565);
    Utils.matToBitmap(closing, closingBmp);

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


image description

edit retag flag offensive close merge delete


Could you be more detailed on your question? You wish to keep the segmentated part of your image in a blank background, or is your objective to put the background transparent?

dbots94 gravatar imagedbots94 ( 2019-10-24 11:40:44 -0500 )edit

background transparent

Fandi gravatar imageFandi ( 2019-10-25 01:35:59 -0500 )edit

duplicate post?

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

use grabcut to remove background image. it will work. read the grabcut documentation you will get the solution

Imgproc.grabCut(img, firstMask, rect, bgModel, fgModel, 1, 0 /* GC_INIT_WITH_RECT */);

Akhil Patel gravatar imageAkhil Patel ( 2019-11-25 08:14:23 -0500 )edit