change grabcut background to white [closed]

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

Opencv_lover gravatar image

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

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));, source, 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);
return bitmap;
edit retag flag offensive reopen merge delete

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


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 -0600 )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 -0600 )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

wuling gravatar imagewuling ( 2014-04-29 02:52:47 -0600 )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 -0600 )edit

sorry,my code is also c++:)

wuling gravatar imagewuling ( 2014-04-29 04:23:19 -0600 )edit

@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 -0600 )edit