Mask giving assertion error in Grabcut Algo
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:
Here is my mask :
Please need urgent help.Thanks
ok, images ae visible. but the error got lost on the way. ?
ops my bad. Now updated :)
hehe, what about the code now ?
lol..Sorry buddy,,I m new to this forum so thats why .Updated again have a look and help :( totally struck there
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]
btw, the flag in imread should be
ImgProc.CV_LOAD_IMAGE_GRAYSCALE
(by chance, you picked the same numeric value)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?
you mean - invert) the mask ?
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!
need urgent help dude :(