Ask Your Question

Dexter's profile - activity

2015-03-19 02:24:14 -0600 received badge  Famous Question (source)
2014-10-21 23:19:25 -0600 received badge  Student (source)
2014-09-21 12:12:44 -0600 received badge  Notable Question (source)
2014-06-15 09:40:33 -0600 received badge  Popular Question (source)
2014-04-15 13:29:07 -0600 asked a question Problem implementing grabcut algo using mask in OpenCV Android 2.4.5

Hi all, As we all know grabcut() has two versions one which uses a rect for extraction and other is the one which use mask for image extraction, I have been trying to use a mask in grabcut algorithm but hav't been succesful in getting desired output.I am using OpenCV for android V 2.4.5 Below are the steps of what I am doing in my code.

1) I first extract the image using grabcut() the mask obtained from this execution is used in next execution of grabcut method with mask. 2) I convert the mask from Mat to Bitmap the bitmap so obtained has only black and white color areas. 3) I modify the bitmap by adding or removing black or white regions using paint brush. 4) generate the Mat from this modified bitmap so that I can use it in grabcut with mask method. 5) The matrix so generated has only 2 values in it ie 0 (Black) and 255 (White). 6) If I directly use this mask matrix in grabcut() it throws an error saying that the mask can only have these values

Imgproc.GC_BGD ie 0
Imgproc.GC_FGD ie 1
Imgproc.GC_PR_BGD ie 2
Imgproc.GC_PR_FGD ie 3

7) So I use Imgproc.threshold(maskMat0, maskMat0, 0, Imgproc.GC_PR_FGD, Imgproc.THRESH_BINARY); so now maskMat0 will have 0 or 3 as values only. 8) Now when I use this maskMat0 in grabcut() with mask method it gives me some wierd output vis regions which I marked as foreground were not considered as foregraound at all,the output ie the maskMat0 stays as it was before execution. If I use Imgproc.GC_FGD in step 7 Imgproc.threshold() method whole image is considered as background and the output mask is completely black.Even if I reuse the mask generated from grabcut() with rect without any modification except step 7 with Imgproc.GC_FGD I get a blank image ie the output mask is completely black.My sole aim is to control the output of grabcut() - the white markers shoud be considered as foreground.

Kindly help me as soon as possible

Code for your reference

    Bitmap avyu  = displayView.getTargetBitmap(); // avyu is the traget bitmap
    cropRect = displayView.getCroppingRect(); // cropRect was used for previous iteration of grabCut() ie Imgproc.GC_INIT_WITH_RECT 

    //Mat mask = new Mat();
    Mat maskMat0 = new Mat(mask.rows(), mask.cols(), mask.type()); // mask was the output matrix from previous iteration of grabCut() with Imgproc.GC_INIT_WITH_RECT 
    Mat maskMat24 = new Mat(); // Temporary Mat
    Utils.bitmapToMat(maskBit, maskMat24); // mask bitmap is converted to Mat


    //Grabcut part begins
    Mat img = new Mat(); // img mat used as the source image  
    Utils.bitmapToMat(avyu, img); // bitmap to mat conversion


    Mat fgdModel = new Mat();
    fgdModel.setTo(new Scalar(100, 100, 100));
    Mat bgdModel = new Mat();
    bgdModel.setTo(new Scalar(0, 0, 0));

    Mat imgC3 = new Mat();

    Imgproc.cvtColor(img, imgC3, Imgproc.COLOR_RGBA2RGB);
    Imgproc.cvtColor(maskMat24, maskMat0, Imgproc.COLOR_RGBA2GRAY); // Changeing the color space from RGBA to RGB otherwise grabcut() will throw error maskMat0 is used in ...
(more)
2013-11-30 09:36:54 -0600 commented answer How to remove black background from grabcut output image in OpenCV android ?

Thanks a lot Haris, I am really grateful to you.

2013-11-24 12:09:27 -0600 asked a question How to remove black background from grabcut output image in OpenCV android ?

image description

Hi, I am using OpenCV android library grabcut() method to extract an image from background, 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 and I am able to successfully extract the fish image from that but the output contains this kind of black background. I am attaching the code for your reference, I am new to opencv and don't have much understanding about it and grabcut algorithm also so kindly help me out.

public class Grabcut extends Activity {
    ImageView iv;
    Bitmap bitmap;
    Canvas canvas;
    Scalar color = new Scalar(255, 0, 0, 255);
    Point tl, br;
    int counter;
    Bitmap bitmapResult, bitmapBackground;
    Mat dst = new Mat();
    final String pathToImage  = Environment.getExternalStorageDirectory()+"/gcut.png";
    public static final String TAG = "Grabcut demo";
    static {
          if (!OpenCVLoader.initDebug()) {
            // Handle initialization error
          }
        }
    @Override
    public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
        setContentView(R.layout.grabcut_main);
        iv = (ImageView) this.findViewById(R.id.imageView);


        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.grabcut);
        Log.d(TAG, "bitmap: " + bitmap.getWidth() + "x" + bitmap.getHeight());


        bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
        Log.d(TAG, "bitmap 8888: " + bitmap.getWidth() + "x" + bitmap.getHeight());


        //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(c/5, r/5);
        Point p2 = new Point(c-c/5, r-r/8);

        Rect rect = new Rect(p1,p2);
        //Rect rect = new Rect(50,30, 100,200);
        Log.d(TAG, "rect: " + rect);

        Mat mask = new Mat();
        debugger(""+mask.type());
        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, rect, 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));
        img.copyTo(foreground, mask);
        Core.rectangle(img, p1, p2, color);

        Mat background = new Mat();
        try {
            background = Utils.loadResource(getApplicationContext(),
                    R.drawable.wall2 );
        } catch (IOException e) {

            e.printStackTrace();
        }
        Mat tmp = new Mat();
        Imgproc.resize(background, tmp, img.size());

        background = tmp;

        Mat tempMask = new Mat(foreground.size(), CvType.CV_8UC1, new Scalar(255, 255, 255));
        Imgproc.cvtColor(foreground, tempMask, 6/* COLOR_BGR2GRAY */);
        //Imgproc.threshold(tempMask, tempMask, 254, 255, 1 /* THRESH_BINARY_INV */);

        Mat vals = new Mat(1, 1, CvType.CV_8UC3, new Scalar(0.0));
        dst = new Mat();
        background.setTo(vals, tempMask);
        Imgproc.resize(foreground, tmp, mask.size());
        foreground = tmp;
        Core.add(background, foreground, dst, tempMask);

        //convert to Bitmap
        Log.d(TAG, "Convert to Bitmap");
        Utils.matToBitmap(dst, bitmap);

        iv.setBackgroundResource(R.drawable.wall2);
        iv.setImageBitmap(bitmap);
        //release MAT part
        img.release();
        imgC3.release();
        mask.release();
        fgdModel.release();
        bgdModel.release();

    }

    public void debugger(String s){
        Log.v("","########### "+s);
    }
}

I have ... (more)