Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Counting coin open cv android

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 https://stackoverflow.com/questions/24204779/counting-objects-better-way-to-filling-holes

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

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

//foolfilled 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(); floodfilled.submat(roi).copyTo(temp);

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!

click to hide/show revision 2
None

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

berak gravatar image

Counting coin open cv android

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 https://stackoverflow.com/questions/24204779/counting-objects-better-way-to-filling-holes

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

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

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

Imgproc.THRESH_BINARY); //foolfilled 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);

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

floodfilled.submat(roi).copyTo(temp);

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!