change grabcut background to white [closed]
Here is my code to use grabcut on the android phone,But when finish the grabcut ,the background is black ,how to let it white,Thanks a lot!
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);
// Core.compare(mask,cv::GC_PR_FGD,mask,Core.CMP_EQ);
Mat foreground = new Mat(img.size(), CvType.CV_8UC3, new Scalar(255, 255, 255));
img.copyTo(foreground, mask);//提取完成
//convert to Bitmap
Log.d(TAG, "Convert to Bitmap");
Utils.matToBitmap(foreground, bitmap);
img.release();
imgC3.release();
mask.release();
fgdModel.release();
bgdModel.release();
return bitmap;
for example
Mat a; a.create(rows,cols,CV_8UC1) a.setTo(255)--->all white background then use img.copyTo(foreground, mask);//提取完成
I am new to the image process area ,can you give me the right code to me ,I would be very appearcaite it
You can use findcountours to bulid a mark,this mark foreground is white and background is black. then use r=subtract(255-mark),finally use add the grabcut result and r http://www.cnblogs.com/dawnWind/archive/2013/03/21/OpenCV_00.html
It is very kind of you to help me ,But ,the code is C\C++,it can not run on the android phone ,can you give me the android code to solve this problem,Thank you~
sorry,my code is also c++:)
@Opencv_lover I think you should read the guidelines of this forum. We do not provide ready made solutions. You have to try and tackle your problem yourself first and report back with problems on the attempt ...