Remove background from canny image in android opencv [closed]

asked 2018-12-12 02:02:53 -0600

Akhil Patel gravatar image

updated 2020-12-09 07:53:24 -0600


i want remove background from captured image. As of now i am capture usa driving license image and convert it into canny image. Here bellow is my output image so i want to remove rest of all background Except for square of the license image. i was try grab cut image for this but it's not working properly. because sometime it's transparent card background also. i need transparent only rest of other background canny edges not a card.

It will be great, if you suggest me the best way.

here is my code which i used from this reference code:

    public Bitmap removeBackground(Bitmap bitmap)
         //GrabCut part
         Mat img = new Mat();
         Utils.bitmapToMat(bitmap, img);

         int r = img.rows();
         int c = img.cols();
         Point p1 = new Point(c / 100, r / 100);
         Point p2 = new Point(c - c / 100, r - r / 100);
         Rect rect = new Rect(p1, p2);

         Mat mask = new Mat();
         Mat fgdModel = new Mat();
         Mat bgdModel = new Mat();

         Mat imgC3 = new Mat();
         Imgproc.cvtColor(img, imgC3, Imgproc.COLOR_RGBA2RGB);

         Imgproc.grabCut(imgC3, mask, rect, bgdModel, fgdModel, 4 ,Imgproc.GC_INIT_WITH_RECT);

         Mat source = new Mat(1, 1, CvType.CV_8U, new Scalar(3.0));, source/* GC_PR_FGD */, mask, Core.CMP_EQ);

         //This is important. You must use Scalar(255,255, 255,255), not Scalar(255,255,255)
         Mat foreground = new Mat(img.size(), CvType.CV_8UC3, new Scalar(255, 255, 255, 255));
         img.copyTo(foreground, mask);

         //  convert matrix to output bitmap
         bitmap = Bitmap.createBitmap((int) foreground.size().width, (int) foreground.size().height, 
          Utils.matToBitmap(foreground, bitmap);
          return bitmap;

image description

edit retag flag offensive reopen merge delete

Closed for the following reason spam or advertising by mshabunin
close date 2019-04-04 07:03:40.105604


LBerger gravatar imageLBerger ( 2018-12-12 03:54:55 -0600 )edit

i vote banning him if he will continue keeping silence about this subject. ( i think i warned him before kindly )

sturkmen gravatar imagesturkmen ( 2018-12-12 05:53:32 -0600 )edit

@Akhil Patel, so ppl here are annoyed, that you seem to fake upvotes. do you have any comments on this ? can you just stop it, maybe ?

berak gravatar imageberak ( 2018-12-12 07:22:16 -0600 )edit

no comments @berak

Akhil Patel gravatar imageAkhil Patel ( 2018-12-12 07:24:51 -0600 )edit

@berak please have you any solution or suggestion for my above problem?

Akhil Patel gravatar imageAkhil Patel ( 2018-12-12 22:51:57 -0600 )edit

@berak@LBerger Please suggest me any idea or any suggestion to solve problem?

Akhil Patel gravatar imageAkhil Patel ( 2018-12-17 04:30:32 -0600 )edit

@berak My account is suspend i don't know what's the problem. please help me.

Akhil Patel gravatar imageAkhil Patel ( 2019-04-02 05:33:37 -0600 )edit

@berak my account is suddenly suspended. i don't know what's the problem.

please help me

Akhil Patel gravatar imageAkhil Patel ( 2019-04-02 05:37:25 -0600 )edit

@Akhil Patel you can help yourself by giving some explanation here

sturkmen gravatar imagesturkmen ( 2019-04-02 05:39:18 -0600 )edit