Ask Your Question
0

Mask giving assertion error in Grabcut Algo

asked 2014-08-20 08:22:05 -0600

swag gravatar image

updated 2020-11-07 02:05:33 -0600

I want to implement Grabcut using Mask. But its is giving me the assertion error.

Code:

Mat firstMask = new Mat();

    Mat bgModel = new Mat();
    Mat fgModel = new Mat();
    Mat mask;
    Mat source = new Mat(1, 1, CvType.CV_8U, new Scalar(3.0));
    dst = new Mat();
    Rect rect = new Rect(tl, br);

    firstMask=Highgui.imread("/storage/emulated/0/DCIM/Camera/pokimen.jpg", CvType.CV_8UC1);

    Imgproc.grabCut(img, firstMask, rect, bgModel, fgModel, 3, Imgproc.GC_INIT_WITH_MASK);
    Core.compare(firstMask, source/* GC_PR_FGD */, firstMask, Core.CMP_EQ);
    Log.d("hello", "asdas");
    Mat foreground = new Mat(img.size(), CvType.CV_8UC3, new Scalar(255,255, 255));
    img.copyTo(foreground, firstMask);

     Core.rectangle(img, tl, br, color);

       Mat tmp = new Mat();
       Imgproc.resize(background, tmp, img.size());
       background = tmp;
       mask = new Mat(foreground.size(), CvType.CV_8UC1, new Scalar(255, 255, 255));

    Imgproc.cvtColor(foreground, mask, 6/* COLOR_BGR2GRAY */);
    Imgproc.threshold(mask, mask, 254, 255, 1 /* THRESH_BINARY_INV */);

    Mat vals = new Mat(1, 1, CvType.CV_8UC3, new Scalar(0.0));
    background.copyTo(dst);

    background.setTo(vals, mask);

    Core.add(background, foreground, dst, mask);

    firstMask.release();
    source.release();
    bgModel.release();
    fgModel.release();
    vals.release();

Error:OpenCV Error: Bad argument (mask element value must be equelGC_BGD or GC_FGD or GC_PR_BGD or GC_PR_FGD)

Here is my Image: image description

Here is my mask : image description

Please need urgent help.Thanks

edit retag flag offensive close merge delete

Comments

ok, images ae visible. but the error got lost on the way. ?

berak gravatar imageberak ( 2014-08-20 08:26:47 -0600 )edit

ops my bad. Now updated :)

swag gravatar imageswag ( 2014-08-20 08:45:27 -0600 )edit

hehe, what about the code now ?

berak gravatar imageberak ( 2014-08-20 08:47:32 -0600 )edit

lol..Sorry buddy,,I m new to this forum so thats why .Updated again have a look and help :( totally struck there

swag gravatar imageswag ( 2014-08-20 08:52:20 -0600 )edit

k. nice now.

bear with me, i never used grabcut, but looking at the code that thows this error, it seems, that the values for the mask image have to be one of 0,1,2,3. (yours is probably [0..255])

so, the mask probably needs threshold(src,dst,128,1,0); to restrict it to [0,1]

berak gravatar imageberak ( 2014-08-20 09:20:55 -0600 )edit

btw, the flag in imread should be ImgProc.CV_LOAD_IMAGE_GRAYSCALE (by chance, you picked the same numeric value)

berak gravatar imageberak ( 2014-08-20 09:35:32 -0600 )edit

okay fine i got ..Now problem is that i want to set the white region of mask as FGD and black as BGD ? how to do so?

swag gravatar imageswag ( 2014-08-21 05:14:59 -0600 )edit

you mean - invert) the mask ?

berak gravatar imageberak ( 2014-08-21 05:19:49 -0600 )edit

no no .. i mean that i want to tell grabcut Algo that white area of the mask is foreground and back is background which should be subtract!

swag gravatar imageswag ( 2014-08-21 05:27:09 -0600 )edit

need urgent help dude :(

swag gravatar imageswag ( 2014-08-21 06:05:03 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-07-10 07:12:33 -0600

arqam gravatar image

That means that the mask that you are passing is having some points which is not having one of the four values specified for the mask to be passed in grabcut function.

When you use Imgproc.GC_INIT_WITH_MASK you should have the mask with any of the four values. For case when you use Imgproc.GC_INIT_WITH_RECT you can have an empty mask also.

So for solving your problem make sure that your mask has either 0,1,2 or 3 meaning background, foreground, probable background, probable foreground respectively.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-08-20 08:22:05 -0600

Seen: 2,296 times

Last updated: Jul 10 '17