How to count number of coins in android opencv ?
Here is my code but i am not able to count the coins..
Bitmap i = getBitmap(imgPath + "mmsqqq.jpg");
//Log.i("after Bitmap i",""+imgPath);
Bitmap bmpImg = i.copy(Bitmap.Config.ARGB_8888, false);
//Log.i("after Bitmap bmpImg",""+imgPath);
Mat srcMat = new Mat ( bmpImg.getHeight(), bmpImg.getWidth(), CvType.CV_8UC3);
Bitmap myBitmap32 = bmpImg.copy(Bitmap.Config.ARGB_8888, true);
Utils.bitmapToMat(bmpImg, srcMat);
//convert to gray scale and save image
Mat gray = new Mat(srcMat.size(), CvType.CV_8UC1);
//Imgproc.cvtColor(srcMat, gray, Imgproc.COLOR_RGB2GRAY,4);
Imgproc.cvtColor(srcMat, gray, Imgproc.COLOR_BGRA2GRAY);
//write bitmap
Boolean grayBool = Highgui.imwrite(imgPath + "gray.jpg", gray);
//Toast.makeText(this, "Gray scale image saved!", Toast.LENGTH_SHORT).show();
//thresholding
Mat threshed = new Mat(bmpImg.getWidth(),bmpImg.getHeight(), CvType.CV_8UC1);
Imgproc.adaptiveThreshold(gray, threshed, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 75, 5);//15, 8 were original tests. Casey was 75,10
//Imgproc.adaptiveThreshold(threshed, threshed, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY_INV, 75, 5);
Core.bitwise_not(threshed, threshed);
Utils.matToBitmap(threshed, bmpImg);
//write bitmap
Boolean boolThr = Highgui.imwrite(imgPath + "threshed.jpg", threshed);
//Toast.makeText(this, "Thresholded image saved!", Toast.LENGTH_SHORT).show();
//perform Canny Edge Detection and convert to 4 channel
Mat edge = new Mat();
Mat dst = new Mat();
Imgproc.Canny(threshed, edge, 80, 90);
Imgproc.cvtColor(edge, dst, Imgproc.COLOR_GRAY2RGBA,4);
//convert to bitmap
Bitmap resultBitmap = Bitmap.createBitmap(dst.cols(), dst.rows(),Bitmap.Config.ARGB_8888);
Utils.matToBitmap(dst, resultBitmap);
//write bitmap
Boolean bool = Highgui.imwrite(imgPath + "edges.jpg", dst);
//Toast.makeText(this, "Edge information image saved!", Toast.LENGTH_SHORT).show();
//smoothing
Mat smoothed = new Mat(bmpImg.getWidth(),bmpImg.getHeight(), CvType.CV_8UC1);
Imgproc.GaussianBlur(dst, smoothed, new org.opencv.core.Size(3,3), 50);
Utils.matToBitmap(smoothed, bmpImg);
//write bitmap
Boolean boolSmoothed = Highgui.imwrite(imgPath + "smoothed.jpg", smoothed);
//Toast.makeText(this, "Smoothed image saved!", Toast.LENGTH_SHORT).show();
//morphological operations
//dilation
Mat dilated = new Mat(bmpImg.getWidth(),bmpImg.getHeight(), CvType.CV_8UC1);
Imgproc.dilate(smoothed, dilated, Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new org.opencv.core.Size (16, 16)));
Utils.matToBitmap(dilated, bmpImg);
//write bitmap
Boolean boolDilated = Highgui.imwrite(imgPath + "dilated.jpg", dilated);
//Toast.makeText(this, "Dilated image saved!", Toast.LENGTH_SHORT).show();
//erosion
Mat eroded = new Mat(bmpImg.getWidth(),bmpImg.getHeight(), CvType.CV_8UC1);
Imgproc.erode(dilated, eroded, Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new org.opencv.core.Size(15, 15)));
Utils.matToBitmap(eroded, bmpImg);
//write bitmap
Boolean boolEroded = Highgui.imwrite(imgPath + "eroded.jpg", eroded);
//Toast.makeText(this, "Eroded image saved!", Toast.LENGTH_SHORT).show();
//hough circles
Mat circles = new Mat();
// parameters
int iCannyUpperThreshold = 100;
int iMinRadius = 20;
int iMaxRadius = 100;
int iAccumulator = 100;
Imgproc.HoughCircles(eroded,circles,Imgproc.CV_HOUGH_GRADIENT,1.0, eroded.rows()/8,iCannyUpperThreshold,iAccumulator,iMinRadius,iMaxRadius);
Log.i("cccccccccccccccccccccc","cccc"+circles.cols());
// draw
if (circles.cols() > 0)
{
//Toast.makeText(this, "Coins : " +circles.cols() , Toast.LENGTH_LONG).show();
alertString = "Number of coins detected : " + circles.cols();
displayAlert(alertString);
}
else
{
//Toast.makeText(this, "No coins found", Toast.LENGTH_LONG).show();
alertString = "No objects detected";
displayAlert(alertString);
}
Help me to solve this problem. Thank You.
Please Upload your sample Images.