19 OpenCV 霍夫曼变换检测圆

文章目录

cv::HoughCircles

因为霍夫圆检测对噪声比较敏感,所以首先要对图像做中值滤波。 基于效率考虑,Opencv中实现的霍夫变换圆检测是基于图像梯度的实现,分为两步:

  1. 检测边缘,发现可能的圆心
  2. 基于第一步的基础上从候选圆心开始计算最佳半径大小

算子参数

c 复制代码
HoughCircles(
InputArray image, // 输入图像 ,必须是8位的单通道灰度图像
OutputArray circles, // 输出结果,发现的圆信息
Int method, // 方法 - HOUGH_GRADIENT
Double dp, // dp = 1; 
Double mindist, // 10 最短距离-可以分辨是两个圆的,否则认为是同心圆- src_gray.rows/8
Double param1, // canny edge detection low threshold
Double param2, // 中心点累加器阈值 -- 候选圆心
Int minradius, // 最小半径
Int maxradius//最大半径 
)

示例

c 复制代码
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;

Mat src, src_gray, dst;

const char* output_title = "final image";
int main()
{
	src = imread("test.png");//读取图片
	if (src.empty())
	{
		cout << "could not load img...";
		return -1;
	}
	namedWindow(output_title);//设置窗口名称
	imshow("test", src);

	// 中值滤波
	Mat moutput;
	medianBlur(src, moutput, 3);
	cvtColor(moutput, moutput, COLOR_BGR2GRAY);

	// 霍夫圆检测
	vector<Vec3f> pcircles;

	HoughCircles(moutput, pcircles, HOUGH_GRADIENT, 1, 10, 100, 30, 5, 50);
	src.copyTo(dst);
	for (size_t i = 0; i < pcircles.size(); i++) 
	{
		Vec3f cc = pcircles[i];
		circle(dst, Point(cc[0], cc[1]), cc[2], Scalar(0, 0, 255), 2, LINE_AA);//绘制圆心
		circle(dst, Point(cc[0], cc[1]), 2, Scalar(198, 23, 155), 2, LINE_AA);
	}

	imshow(output_title, dst);
	waitKey(0);
	return 0;
}
相关推荐
Uncommon.6 小时前
使用Pytorch自动计算梯度
人工智能·pytorch·python
安逸sgr6 小时前
循环神经网络 RNN、LSTM、GRU 是什么?为什么能处理序列?
人工智能·ai·大模型·agent·智能体
猿小猴子6 小时前
主流 AGENT 实战教程「OpenCodex」与「ChatGPT Work」与「Codex Record & Replay」介绍
人工智能·ai·chatgpt·codex·minimax·deepseek·opencodex
shdwak....sad7 小时前
版本管理&迁移kali-vmware&知识库
人工智能·网络安全
程序员cxuan7 小时前
OpenAI Linux 版来了!
人工智能·后端·程序员
用户5619035069337 小时前
OpenAI兼容API报401/404/429怎么解决?API Key、Base URL、模型名排查
人工智能
lucky_syq7 小时前
专栏目录与学习路线 | 48 篇正文导览
人工智能·学习
TechEdu2026067 小时前
[人工智能]RLlib:基于Ray的可扩展强化学习框架
人工智能·ai
加多7 小时前
DeepSeek Harness 深度分析:用途、问题、架构原理与使用指南
人工智能·架构
风流 少年7 小时前
Spring AI 2.0:Flux
java·人工智能·spring