c++ 利用霍夫变换实现对图片中圆的检测

#include
#include
using namespace std;
using namespace cv;

int main()
{
	Mat src = imread("d://VC项目//img9.jpg");
	if (!src.data)
	{
		printf("图片路径不存在!");
	}
	//中值滤波
	Mat media_img,gray_img,rgb_img;
	medianBlur(src, media_img, 5);
	cvtColor(media_img, gray_img, COLOR_BGR2GRAY);
	//霍夫圆检测
	vector<Vec4f>circles;
	Scalar color = Scalar(0, 0, 255);
	HoughCircles(gray_img,circles, HOUGH_GRADIENT, 1, 100, 100, 40, 80, 200);
	src.copyTo(rgb_img);
	for (size_t i = 0; i < circles.size(); i++)
	{
		Vec4f c = circles[i];
		circle(rgb_img, Point(c[0], c[1]), c[2], color, 2, LINE_AA);
		circle(rgb_img, Point(c[0], c[1]),1,Scalar(255,0,0), 2, LINE_AA);
	}
	medianBlur(rgb_img,rgb_img, 5);
	imshow("src", src);
	imshow("gray_img", gray_img);
	imshow("rgb_img", rgb_img);
	waitKey(0);
	return 0;
}

c++ 利用霍夫变换实现对图片中圆的检测_第1张图片

你可能感兴趣的:(c++,图像编程)