change grabcut background to white

asked 2014-04-28 06:55:07 -0500

Opencv_lover gravatar image

updated 2014-04-28 07:02:55 -0500

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;
edit retag flag offensive close merge delete

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 ( 2014-04-28 19:41:41 -0500 )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 ( 2014-04-29 00:45:14 -0500 )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 ( 2014-04-29 02:52:47 -0500 )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 ( 2014-04-29 04:15:10 -0500 )edit

sorry,my code is also c++:)

wuling gravatar imagewuling ( 2014-04-29 04:23:19 -0500 )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 ( 2014-04-29 04:38:16 -0500 )edit