Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to change Grabcut black backgroung to other color

Hello,every one,I use grabcut as opencv step by step ,but the result image is not the right image as I need,here is the code,It is should be output an white background bitmap ,but it is still black.

//GrabCut part Mat img = new Mat(); Utils.bitmapToMat(bitmap, img); Log.d(TAG, "img: " + img);

     int r = img.rows();
     int c = img.cols();

     Point p1 = new Point(rect.left,rect.top );
     Point p2 = new Point(rect.right, rect.bottom);
     org.opencv.core.Rect rect1=new  org.opencv.core.Rect(p1,p2) ;
     //Rect rect = new Rect(50,30, 100,200);
     Log.d(TAG, "rect: " + rect);
     Mat mask = new Mat();
     mask.setTo(new Scalar(125));
     Mat fgdModel = new Mat();
     fgdModel.setTo(new Scalar(255, 255, 255));
     Mat bgdModel = new Mat();
     bgdModel.setTo(new Scalar(255, 255, 255));
     Mat imgC3 = new Mat();  
     Imgproc.cvtColor(img, imgC3, Imgproc.COLOR_RGBA2RGB);
     Log.d(TAG, "imgC3: " + imgC3);
     Log.d(TAG, "Grabcut begins");
     Imgproc.grabCut(imgC3, mask, rect1, bgdModel, fgdModel,5, Imgproc.GC_INIT_WITH_RECT);
     Mat source = new Mat(1, 1, CvType.CV_8U, new Scalar(3.0));
     Core.compare(mask, source, mask, Core.CMP_EQ);
     Mat foreground = new Mat(img.size(), CvType.CV_8UC3, new Scalar(255, 255, 255));//background should be white
      img.copyTo(foreground,mask);//finish
    //  foreground.setTo(new Scalar(255, 255, 255));
        Utils.matToBitmap(foreground, bitmap);
click to hide/show revision 2
No.2 Revision

How to change Grabcut black backgroung to other color

Hello,every one,I use grabcut as opencv step by step ,but the result image is not the right image as I need,here is the code,It is should be output an white background bitmap ,but it is still black.

 //GrabCut part
Mat img = new Mat();
Utils.bitmapToMat(bitmap, img);
Log.d(TAG, "img: " + img);

img);
 int r = img.rows();
 int c = img.cols();
 Point p1 = new Point(rect.left,rect.top );
 Point p2 = new Point(rect.right, rect.bottom);
 org.opencv.core.Rect rect1=new org.opencv.core.Rect(p1,p2) ;
 //Rect rect = new Rect(50,30, 100,200);
  Log.d(TAG, "rect: " + rect);
 Mat mask = new Mat();
  mask.setTo(new Scalar(125));
 Mat fgdModel = new Mat();
  fgdModel.setTo(new Scalar(255, 255, 255));
 Mat bgdModel = new Mat();
  bgdModel.setTo(new Scalar(255, 255, 255));
 Mat imgC3 = new Mat();
  Imgproc.cvtColor(img, imgC3, Imgproc.COLOR_RGBA2RGB);
  Log.d(TAG, "imgC3: " + imgC3);
 Log.d(TAG, "Grabcut begins");
  Imgproc.grabCut(imgC3, mask, rect1, bgdModel, fgdModel,5, Imgproc.GC_INIT_WITH_RECT);
 Mat source = new Mat(1, 1, CvType.CV_8U, new Scalar(3.0));
  Core.compare(mask, source, mask, Core.CMP_EQ);
  Mat foreground = new Mat(img.size(), CvType.CV_8UC3, new Scalar(255, 255, 255));//background should be white
 img.copyTo(foreground,mask);//finish
  // foreground.setTo(new Scalar(255, 255, 255));
 Utils.matToBitmap(foreground, bitmap);

How to change Grabcut black backgroung to other color

Hello,every one,I use grabcut as opencv step by step ,but the result image is not the right image as I need,here is the code,It is should be output an white background bitmap ,but it is still black.

 //GrabCut part
         Mat img = new Mat();
         Utils.bitmapToMat(bitmap, img);
         Log.d(TAG, "img: " + img);

         int r = img.rows();
         int c = img.cols();

         Point p1 = new Point(rect.left,rect.top );
         Point p2 = new Point(rect.right, rect.bottom);
         org.opencv.core.Rect rect1=new  org.opencv.core.Rect(p1,p2) ;
         //Rect rect = new Rect(50,30, 100,200);
         Log.d(TAG, "rect: " + rect);
         Mat mask = new Mat();
         mask.setTo(new Scalar(125));
         Mat fgdModel = new Mat();
         fgdModel.setTo(new Scalar(255, 255, 255));
         Mat bgdModel = new Mat();
         bgdModel.setTo(new Scalar(255, 255, 255));
         Mat imgC3 = new Mat();  
         Imgproc.cvtColor(img, imgC3, Imgproc.COLOR_RGBA2RGB);
         Log.d(TAG, "imgC3: " + imgC3);
         Log.d(TAG, "Grabcut begins");
         Imgproc.grabCut(imgC3, mask, rect1, bgdModel, fgdModel,5, Imgproc.GC_INIT_WITH_RECT);
         Mat source = new Mat(1, 1, CvType.CV_8U, new Scalar(3.0));
         Core.compare(mask, source, mask, Core.CMP_EQ);
         Mat foreground = new Mat(img.size(), CvType.CV_8UC3, new Scalar(255, 255, 255));//background should be white
          img.copyTo(foreground,mask);//finish
        //  foreground.setTo(new Scalar(255, 255, 255));
            Utils.matToBitmap(foreground, bitmap);