change grabcut background to white [closed]

asked Apr 28 '14

Opencv_lover gravatar image

updated Apr 28 '14

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;
Preview: (hide)

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-09-26 13:39:55.110030

Comments

for example

Mat a; a.create(rows,cols,CV_8UC1) a.setTo(255)--->all white background then use img.copyTo(foreground, mask);//提取完成

wuling gravatar imagewuling (Apr 29 '14)edit

I am new to the image process area ,can you give me the right code to me ,I would be very appearcaite it

Opencv_lover gravatar imageOpencv_lover (Apr 29 '14)edit

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

wuling gravatar imagewuling (Apr 29 '14)edit

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~

Opencv_lover gravatar imageOpencv_lover (Apr 29 '14)edit

sorry,my code is also c++:)

wuling gravatar imagewuling (Apr 29 '14)edit
2

@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 ...

StevenPuttemans gravatar imageStevenPuttemans (Apr 29 '14)edit