void HoughCircles(InputArray image,OutputArray circles, int method, double dp, double minDist, double param1=100,double param2=100, int minRadius=0, int maxRadius=0 )
#include
#include
#include
#include
#include
using namespace std;
using namespace cv;
int main(int argc, char **argv)
{
Mat src = imread("2.png");
imshow("1",src);
if(src.empty())
{
cout << "can not open " << endl;
return -1;
}
Mat cimg;
cvtColor(src,cimg,COLOR_BGR2GRAY);
medianBlur(cimg, cimg, 5);
GaussianBlur(cimg, cimg, Size(9, 9), 2, 2);
Canny(cimg,cimg,10,250,5);
imshow("canny",cimg);
vector<Vec3f> circles;
HoughCircles(cimg, circles, HOUGH_GRADIENT, 1, 30,100, 22, 0, 0 );
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]);
int rect_x=(cvRound(circles[i][0])-cvRound(circles[i][2]));
int rect_y=(cvRound(circles[i][1])-cvRound(circles[i][2]));
int rect_width=(cvRound(circles[i][2])+cvRound(circles[i][2]));
int rect_height=(cvRound(circles[i][2])+cvRound(circles[i][2]));
//绘制圆心
circle(src, center, 3, Scalar(0, 255, 0), -1, 8, 0);
//绘制圆轮廓
circle(src, center, radius, Scalar(155, 50, 255), 3, 8, 0);
Rect rect(rect_x,rect_y,rect_width,rect_height);
Mat image_cut = Mat(src, rect); //从img中按照rect进行切割,此时修改image_cut时image中对应部分也会修改,因此需要copy
Mat image_copy = image_cut.clone(); //clone函数创建新的图片
imshow("分割",image_copy);
imshow("图片",src);
cv::waitKey(0);
}
return 0;
}
参考资料:https://blog.csdn.net/sinat_34156619/article/details/79574505