Hello ,Everyone ,here is My code use OpenCv method GrabCut,but there is a problem abuse me,The input Image is a color Image,but through Grabcut ,the result Image is gray level image.The code as follows: Long startTime = System.currentTimeMillis();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.p4);
Log.d(TAG, "bitmap: " + bitmap.getWidth() + "x" + bitmap.getHeight());
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
Log.d(TAG, "bitmap 8888: " + bitmap.getWidth() + "x" + bitmap.getHeight());
//GrabCut part
Mat img = new Mat();
Utils.bitmapToMat(bitmap, img);
Log.d(TAG, "img: " + img);
int r = img.rows();
int c = img.cols();
Point p1 = new Point(c/10, r/10);
Point p2 = new Point(c-c/10, r-r/10);
Rect rect = new Rect(p1,p2);
//Rect rect = new Rect(50,30, 100,200);
Log.d(TAG, "rect: " + rect);
Mat mask = new Mat();
Mat fgdModel = new Mat();
Mat bgdModel = new Mat();
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, rect, bgdModel, fgdModel, 2, Imgproc.GC_INIT_WITH_RECT);
Core.convertScaleAbs(mask, mask, 100, 0);
Imgproc.cvtColor(mask, mask, Imgproc.COLOR_GRAY2RGBA,4);
Log.d(TAG, "maskC4: " + mask);
//convert to Bitmap
Log.d(TAG, "Convert to Bitmap");
Utils.matToBitmap(mask, bitmap);
iv.setImageBitmap(bitmap);
Long endTime = System.currentTimeMillis();
Long time=endTime-startTime;
System.out.println("Time Is:"+time);
//release MAT part
img.release();
imgC3.release();
mask.release();
fgdModel.release();
bgdModel.release();