Counting coin open cv android

asked 2018-05-21 03:09:29 -0500

akbar gravatar image

updated 2018-05-21 03:14:48 -0500

berak gravatar image

hi, i'm new on opencv especially for android, i'm try to counting object (coins) in an image. I try to find some examples and much of them in C++, and unfortunatly I'm noob on C++ as well as android

I see some examples, and I try to follow like this

but with different image

this my image

image description

here's my code

//convert bmp to Mat
Bitmap myBitmap = BitmapFactory.decodeFile(path);
Bitmap bmpImg = myBitmap.copy(Bitmap.Config.ARGB_8888, false);
 Mat srcMat = new Mat ( bmpImg.getHeight(), bmpImg.getWidth(), CvType.CV_8UC3);
Utils.bitmapToMat(bmpImg, srcMat);

//convert img to gray
Mat gray = new Mat(mat.size(), CvType.CV_8UC3);
Imgproc.cvtColor(srcMat, gray, Imgproc.COLOR_RGB2GRAY, 4);

Imgproc.threshold(grayImage, grayImage, 100, 255, Imgproc.THRESH_BINARY);

Rect rect = null;
Point flood = new Point(10, 10);
 Scalar lowerDiff = new Scalar(10, 10, 10);
Scalar upperDiff = new Scalar(10, 10, 10);
Mat floodfilled = Mat.zeros(grayImage.rows() + 2, grayImage.cols() + 2, CvType.CV_8U);
 Imgproc.floodFill(img, floodfilled, new Point(0, 0), new Scalar(255), new Rect(), new Scalar(0), new Scalar(0), 4 + (255 << 8) + Imgproc.FLOODFILL_MASK_ONLY);
Core.subtract(floodfilled, Scalar.all(0), floodfilled);

 Rect roi = new Rect(1, 1, img.cols() - 2, img.rows() - 2);
Mat temp = new Mat();

and the resul like this image description

next step I try to find countour but the result like this,

image description

and I want after countour something like this, like the sample above

image description

Any help is much appreciated.

Thanks & Cheers!

edit retag flag offensive close merge delete


hmm, could you add the code for finding the contours ? it's also unclear, where the black image is coming from.

berak gravatar imageberak ( 2018-05-21 03:18:43 -0500 )edit