OpenCV:Hough霍夫变换(HoughCircles)

转自:https://blog.csdn.net/wsds_mzm/article/details/79058406

#include<iostream>  
#include <opencv2/core/core.hpp>  
#include <opencv2/highgui/highgui.hpp>  
  
//包含头文件
#include <opencv2/opencv.hpp>
//命名空间
using namespace cv;
using namespace std;
//全局函数声明部分
 
//主函数
int main()
{
	//【1】载入图像
	Mat image = imread("C:\\Users\\Administrator\\Desktop\\111.jpg");
	//【2】检查是否载入成功
	if (image.empty())
	{
		printf("读取图片错误,请确认目录下是否有imread函数指定图片存在! \n ");
		return 0;
	}
	//【3】转换为灰度图像
	Mat grayImage;
	cvtColor(image, grayImage, COLOR_BGR2GRAY);
	Mat contours;
	Canny(grayImage, contours, 125, 350);
	threshold(contours, contours, 128, 255, THRESH_BINARY);
	//【4】图像平滑降噪
	//GaussianBlur(grayImage, grayImage, Size(9,9), 2, 2 );
	//【5】霍夫圆变换、圆检测
	vector<Vec3f> circles;
	HoughCircles(contours, circles, CV_HOUGH_GRADIENT, 1, 20, 80, 100);
	//【6】迭代器遍历向量,circle()函数绘制圆
	std::vector<Vec3f>::const_iterator it = circles.begin();
	while (it != circles.end())
	{
		Point center((*it)[0], (*it)[1]);
		int radius = (*it)[2];
		//绘制圆心
		circle(image, center, 3, Scalar(0,255,0), -1, 8, 0);
		//绘制圆轮廓
		circle(image, center, radius, Scalar(0,0,255), 3, 8, 0);
		++it;
	}
	//【7】显示图像
	imshow("19-圆检测图", image);
	//【8】保持窗口显示
	waitKey(0);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/ben121_/article/details/88822618