Ask Your Question
0

How to count number of coins in android opencv ?

asked 2014-07-02 04:29:56 -0600

Nehalkumar gravatar image

updated 2014-07-02 08:15:40 -0600

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.

image description image description

edit retag flag offensive close merge delete

Comments

Please Upload your sample Images.

Balaji R gravatar imageBalaji R ( 2014-07-02 08:07:13 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
0

answered 2014-07-03 01:06:24 -0600

updated 2014-07-03 01:09:45 -0600

Hi @Nehalkumar! Refer the following link & Cpp Example to count the number of Coins in the Image. If you are finding any difficulty feel free to ask questions.

edit flag offensive delete link more

Comments

hello balaji thank you for answer but my problem is i am not get 100% result like(i count 10 coins and some time i get 10 and some time 9,8,11,..,7 so on ). in android is possible i get 100% correct result like i count 10 coin and i get 10

Nehalkumar gravatar imageNehalkumar ( 2014-07-04 08:23:57 -0600 )edit

Yes of course!.Have you tried to apply this Water Shed Algorithm? Share the results screen so that we can help.

Balaji R gravatar imageBalaji R ( 2014-07-04 10:37:38 -0600 )edit

hello balaji i am uploading all images which i am getting during counting coin procedure. please check and give feedback.i am not get 100% result.Give another solution . thanking you

Nehalkumar gravatar imageNehalkumar ( 2014-07-05 04:47:10 -0600 )edit
0

answered 2014-07-05 04:43:45 -0600

Nehalkumar gravatar image

updated 2014-07-05 04:49:25 -0600

These all are screen shot of counting coin procedure 1)input image description 2)gray image description 3)smooth image description 4)threshold image description 5)canny image description 6)smooth image description 7)output image description

i am not get 100 % result every time ? some time get 100% and some time dummy data get

edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-07-02 04:29:56 -0600

Seen: 3,898 times

Last updated: Jul 05 '14