TRUMP
TRUMP

Reputation: 113

Counting coins using OpenCV

I have been trying to develop a program - written with C++ and using OpenCV -which counts the overall value of coins shown in some image. I should note that I am new to the opencv platform.

In order to achieve this goal, as far as I understand - there has to be a use of the Hough transform to detect the ratio of the coins. I found this code example on the OpenCV site, but I can't put a value for the coins.

Here is what I have done so far.

#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>

using namespace std;
using namespace cv;
namespace
{

    const std::string windowName = "Coins detection";
    const std::string cannyThresholdTrackbarName = "Canny threshold";
    const std::string accumulatorThresholdTrackbarName = "Accumulator Threshold";


    const int cannyThresholdInitialValue = 41;
    const int accumulatorThresholdInitialValue = 87;
    const int maxAccumulatorThreshold = 200;
    const int maxCannyThreshold = 255;

    void HoughDetection(const Mat& src_gray, const Mat& src_display, int cannyThreshold, int accumulatorThreshold)
    {

        std::vector<Vec3f> circles;

        HoughCircles( src_gray, circles, HOUGH_GRADIENT, 1, src_gray.rows/8, cannyThreshold, accumulatorThreshold, 0, 0 );


        Mat display = src_display.clone();
        for( size_t i = 0; i < circles.size(); i++)
        {
            Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
            int radius = cvRound(circles[i][2]);



            circle( display, center, 3, Scalar(0,255,0), -1, 8, 0 );

            circle( display, center, radius, Scalar(0,0,255), 3, 8, 0 );

        }


        imshow( windowName, display);
    }
}


int main(int argc, char** argv)
{
    Mat src, src_gray;


    String imageName("c:\\moedas.jpg");
    if (argc > 1)
    {
       imageName = argv[1];
    }
    src = imread( imageName, IMREAD_COLOR );

    if( src.empty() )
    {
        std::cerr<<"Invalid input image\n";

        return -1;
    }


    cvtColor( src, src_gray, COLOR_BGR2GRAY );


    GaussianBlur( src_gray, src_gray, Size(9, 9), 2, 2 );


    int cannyThreshold = cannyThresholdInitialValue;
    int accumulatorThreshold = accumulatorThresholdInitialValue;


    namedWindow( windowName, WINDOW_AUTOSIZE );
    createTrackbar(cannyThresholdTrackbarName, windowName, &cannyThreshold,maxCannyThreshold);
    createTrackbar(accumulatorThresholdTrackbarName, windowName, &accumulatorThreshold, maxAccumulatorThreshold);


    char key = 0;
    while(key != 'q' && key != 'Q')
    {

        cannyThreshold = std::max(cannyThreshold, 1);
        accumulatorThreshold = std::max(accumulatorThreshold, 1);


        HoughDetection(src_gray, src, cannyThreshold, accumulatorThreshold);


        key = (char)waitKey(10);
    }

    return 0;
}

Upvotes: 2

Views: 2790

Answers (1)

Javier Silva Ort&#237;z
Javier Silva Ort&#237;z

Reputation: 2982

The code you have so far only segments circle shapes in an input image. This is just the first step to count the coins. There are many ways to perform that task, ranging from simple contour counting techniques to complicated deep learning, and the explanation of such techniques is too broad and large in scope to put efficiently and concisely in an SO answer. However, here are some coin detectors/counter implementations/tutorials that you can check:

  • Implementation 1 in Python. This is the best of the list, although the code file is larger than yours, not too hard to port to C++. It has the best detection/counting performance but deals with Neural Networks, specifically, a Multilayer Perceptron.

  • Implementation 2 in Python. This is a very small code file, nearly as large as yours and has no idiomatic Python code, porting it to C++ is a no brainer, you should start here. This implementation uses mere contour counting with the aid of the Canny edge detector.

  • Tutorial 1 in C++. A simple tutorial in C++, but only serves introductory purposes, the implementations listed above are the real thing.

Upvotes: 2

Related Questions