Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Help!!! Problem about grabCut

536 views
Skip to first unread message

Chenglong Wang

unread,
May 1, 2012, 1:30:10 PM5/1/12
to android...@googlegroups.com
I use grabCut in my app, but the program always had no response when "GrabCut begins" appeared in logcat.
So I am pretty sure the problem it's in the function. 

Is it a format problem or something else? I use Android OpenCV-2.3.1 library.
Here is the codes:

...............
   Bitmap bitmap = BitmapFactory.decodeFile(mCurrentImagePath);
Log.i(TAG, mCurrentImagePath);


//JPEG2RGB888
bitmap = JPEG2RGB888(bitmap);
Log.i(TAG, "JPEG2RGB888");
//GrabCut part
ma_img=Utils.bitmapToMat(bitmap);
Rect rect;
Point p1, p2;
p1 = new Point(0,0);
       p2 = new Point(ma_img.cols(),ma_img.rows());
       rect=new Rect(p1,p2);
       
       mask = new Mat();
       fgdModel = new Mat();
       bgdModel = new Mat();

       ma_img.convertTo(ma_img, CvType.CV_8UC3);
                   Log.i(TAG, "Grabcut begins");
           
Imgproc.grabCut(ma_img, mask, rect, bgdModel, fgdModel, 0, Imgproc.GC_INIT_WITH_RECT);
Imgproc.grabCut(ma_img, mask, rect, bgdModel, fgdModel, 2);
           Log.i(TAG, "Grabcut ends");
//Imgproc.cvtColor(mask, mask, Imgproc.COLOR_GRAY2RGBA, 4);

//convert to Bitmap
Log.i(TAG, "Convert to Bitmap");
bitmap = Bitmap.createBitmap(mask.cols(), mask.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(mask, bitmap);

//release MAT part
ma_img.release();
mask.release();
fgdModel.release();
bgdModel.release();

           mImageView.setImageBitmap(bitmap);

Andrey Pavlenko

unread,
May 2, 2012, 5:48:25 AM5/2/12
to
Here is your code that I modified to do the work OK.
But be aware that I used OpenCV 2.4.0 package, the API changed a bit..
Also be aware that grabCut() call is very slow! It may take several seconds.
    public void doTask1(ImageView iv) {
   
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.fruits);
 
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);
 
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);
       
Log.d(TAG, "Grabcut ends");
 
 
Log.d(TAG, "mask: " + mask);
 
Log.d(TAG, "bgdModel: " + bgdModel);
 
Log.d(TAG, "fgdModel: " + fgdModel);
 
 
Core.convertScaleAbs(mask, mask, 100, 0);
 
Imgproc.cvtColor(mask, mask, Imgproc.COLOR_GRAY2RGBA);
 
Log.d(TAG, "maskC4: " + mask);
 
 
//convert to Bitmap
 
Log.d(TAG, "Convert to Bitmap");
 
//Utils.matToBitmap(imgC3, bitmap);
 
Utils.matToBitmap(mask, bitmap);
    iv
.setImageBitmap(bitmap);


 
//release MAT part
 img
.release();
 imgC3
.release();
 mask
.release();
 fgdModel
.release();
 bgdModel
.release();
   
}

Reply all
Reply to author
Forward
0 new messages