我一直在尝试开发一个用C ++编写并使用OpenCV编写的程序,该程序对某些图像中显示的硬币的整体价值进行计数。 我应该注意,我是opencv平台的新手。
据我所知,为了实现这一目标,必须使用霍夫变换来检测硬币的比率。我在OpenCV网站上找到了此代码示例,但是我无法为硬币设置值。
这是我到目前为止所做的。
#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 );
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,radius,255),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,Size(9,9),2,2 );
int cannyThreshold = cannyThresholdInitialValue;
int accumulatorThreshold = accumulatorThresholdInitialValue;
namedwindow( windowName,WINDOW_AUTOSIZE );
createTrackbar(cannyThresholdTrackbarName,windowName,&cannyThreshold,maxCannyThreshold);
createTrackbar(accumulatorThresholdTrackbarName,&accumulatorThreshold,maxaccumulatorThreshold);
char key = 0;
while(key != 'q' && key != 'Q')
{
cannyThreshold = std::max(cannyThreshold,1);
accumulatorThreshold = std::max(accumulatorThreshold,1);
HoughDetection(src_gray,src,accumulatorThreshold);
key = (char)waitKey(10);
}
return 0;
}