how to count coins with open cv

asked 2018-01-06 17:07:36 -0600

rubenp gravatar image

updated 2018-01-07 11:29:29 -0600

Eduardo gravatar image

hello at the moment i used the hough trasform to detect the ratio of the coins, i used the code exemplo of the open cv site, but now i just can't put a value for the coins, i´m new on this... my work is about read the value of the coins on an image with six coins and do the some of all the coins in the image. can someone help me? my code at the time:

#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;
}
edit retag flag offensive close merge delete

Comments

Without a valid image to test, there is no telling what would be the best solution for your case. So can you please provide some samples?

StevenPuttemans gravatar imageStevenPuttemans ( 2018-01-08 03:39:40 -0600 )edit