Ask Your Question

Revision history [back]

how to count coins with open cv

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;

}

click to hide/show revision 2
None

how to count coins with open cv

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>

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

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

}