图像处理之霍夫变换圆检测算法

2024-06-12 22:48

本文主要是介绍图像处理之霍夫变换圆检测算法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

- created by gloomyfish

图像处理之霍夫变换圆检测算法

之前写过一篇文章讲述霍夫变换原理与利用霍夫变换检测直线, 结果发现访问量还是蛮

多,有点超出我的意料,很多人都留言说代码写得不好,没有注释,结构也不是很清晰,所以

我萌发了再写一篇,介绍霍夫变换圆检测算法,同时也尽量的加上详细的注释,介绍代码

结构.让更多的人能够读懂与理解.

一:霍夫变换检测圆的数学原理


根据极坐标,圆上任意一点的坐标可以表示为如上形式, 所以对于任意一个圆, 假设

中心像素点p(x0, y0)像素点已知, 圆半径已知,则旋转360由极坐标方程可以得到每

个点上得坐标同样,如果只是知道图像上像素点, 圆半径,旋转360°则中心点处的坐

标值必定最强.这正是霍夫变换检测圆的数学原理.

二:算法流程

该算法大致可以分为以下几个步骤


三:运行效果

图像从空间坐标变换到极坐标效果, 最亮一点为圆心.


图像从极坐标变换回到空间坐标,检测结果显示:


四:关键代码解析

个人觉得这次注释已经是非常的详细啦,而且我写的还是中文注释

/** * 霍夫变换处理 - 检测半径大小符合的圆的个数 * 1. 将图像像素从2D空间坐标转换到极坐标空间 * 2. 在极坐标空间中归一化各个点强度,使之在0〜255之间 * 3. 根据极坐标的R值与输入参数(圆的半径)相等,寻找2D空间的像素点 * 4. 对找出的空间像素点赋予结果颜色(红色) * 5. 返回结果2D空间像素集合 * @return int [] */  
public int[] process() {  // 对于圆的极坐标变换来说,我们需要360度的空间梯度叠加值  acc = new int[width * height];  for (int y = 0; y < height; y++) {  for (int x = 0; x < width; x++) {  acc[y * width + x] = 0;  }  }  int x0, y0;  double t;  for (int x = 0; x < width; x++) {  for (int y = 0; y < height; y++) {  if ((input[y * width + x] & 0xff) == 255) {  for (int theta = 0; theta < 360; theta++) {  t = (theta * 3.14159265) / 180; // 角度值0 ~ 2*PI  x0 = (int) Math.round(x - r * Math.cos(t));  y0 = (int) Math.round(y - r * Math.sin(t));  if (x0 < width && x0 > 0 && y0 < height && y0 > 0) {  acc[x0 + (y0 * width)] += 1;  }  }  }  }  }  // now normalise to 255 and put in format for a pixel array  int max = 0;  // Find max acc value  for (int x = 0; x < width; x++) {  for (int y = 0; y < height; y++) {  if (acc[x + (y * width)] > max) {  max = acc[x + (y * width)];  }  }  }  // 根据最大值,实现极坐标空间的灰度值归一化处理  int value;  for (int x = 0; x < width; x++) {  for (int y = 0; y < height; y++) {  value = (int) (((double) acc[x + (y * width)] / (double) max) * 255.0);  acc[x + (y * width)] = 0xff000000 | (value << 16 | value << 8 | value);  }  }  // 绘制发现的圆  findMaxima();  System.out.println("done");  return output;  
}  
完整的算法源代码, 已经全部的加上注释
package com.gloomyfish.image.transform.hough;  
/*** *  * 传入的图像为二值图像,背景为黑色,目标前景颜色为为白色 * @author gloomyfish *  */  
public class CircleHough {  private int[] input;  private int[] output;  private int width;  private int height;  private int[] acc;  private int accSize = 1;  private int[] results;  private int r; // 圆周的半径大小  public CircleHough() {  System.out.println("Hough Circle Detection...");  }  public void init(int[] inputIn, int widthIn, int heightIn, int radius) {  r = radius;  width = widthIn;  height = heightIn;  input = new int[width * height];  output = new int[width * height];  input = inputIn;  for (int y = 0; y < height; y++) {  for (int x = 0; x < width; x++) {  output[x + (width * y)] = 0xff000000; //默认图像背景颜色为黑色  }  }  }  public void setCircles(int circles) {  accSize = circles; // 检测的个数  }  /** * 霍夫变换处理 - 检测半径大小符合的圆的个数 * 1. 将图像像素从2D空间坐标转换到极坐标空间 * 2. 在极坐标空间中归一化各个点强度,使之在0〜255之间 * 3. 根据极坐标的R值与输入参数(圆的半径)相等,寻找2D空间的像素点 * 4. 对找出的空间像素点赋予结果颜色(红色) * 5. 返回结果2D空间像素集合 * @return int [] */  public int[] process() {  // 对于圆的极坐标变换来说,我们需要360度的空间梯度叠加值  acc = new int[width * height];  for (int y = 0; y < height; y++) {  for (int x = 0; x < width; x++) {  acc[y * width + x] = 0;  }  }  int x0, y0;  double t;  for (int x = 0; x < width; x++) {  for (int y = 0; y < height; y++) {  if ((input[y * width + x] & 0xff) == 255) {  for (int theta = 0; theta < 360; theta++) {  t = (theta * 3.14159265) / 180; // 角度值0 ~ 2*PI  x0 = (int) Math.round(x - r * Math.cos(t));  y0 = (int) Math.round(y - r * Math.sin(t));  if (x0 < width && x0 > 0 && y0 < height && y0 > 0) {  acc[x0 + (y0 * width)] += 1;  }  }  }  }  }  // now normalise to 255 and put in format for a pixel array  int max = 0;  // Find max acc value  for (int x = 0; x < width; x++) {  for (int y = 0; y < height; y++) {  if (acc[x + (y * width)] > max) {  max = acc[x + (y * width)];  }  }  }  // 根据最大值,实现极坐标空间的灰度值归一化处理  int value;  for (int x = 0; x < width; x++) {  for (int y = 0; y < height; y++) {  value = (int) (((double) acc[x + (y * width)] / (double) max) * 255.0);  acc[x + (y * width)] = 0xff000000 | (value << 16 | value << 8 | value);  }  }  // 绘制发现的圆  findMaxima();  System.out.println("done");  return output;  }  private int[] findMaxima() {  results = new int[accSize * 3];  int[] output = new int[width * height];  // 获取最大的前accSize个值  for (int x = 0; x < width; x++) {  for (int y = 0; y < height; y++) {  int value = (acc[x + (y * width)] & 0xff);  // if its higher than lowest value add it and then sort  if (value > results[(accSize - 1) * 3]) {  // add to bottom of array  results[(accSize - 1) * 3] = value; //像素值  results[(accSize - 1) * 3 + 1] = x; // 坐标X  results[(accSize - 1) * 3 + 2] = y; // 坐标Y  // shift up until its in right place  int i = (accSize - 2) * 3;  while ((i >= 0) && (results[i + 3] > results[i])) {  for (int j = 0; j < 3; j++) {  int temp = results[i + j];  results[i + j] = results[i + 3 + j];  results[i + 3 + j] = temp;  }  i = i - 3;  if (i < 0)  break;  }  }  }  }  // 根据找到的半径R,中心点像素坐标p(x, y),绘制圆在原图像上  System.out.println("top " + accSize + " matches:");  for (int i = accSize - 1; i >= 0; i--) {  drawCircle(results[i * 3], results[i * 3 + 1], results[i * 3 + 2]);  }  return output;  }  private void setPixel(int value, int xPos, int yPos) {  /// output[(yPos * width) + xPos] = 0xff000000 | (value << 16 | value << 8 | value);  output[(yPos * width) + xPos] = 0xffff0000;  }  // draw circle at x y  private void drawCircle(int pix, int xCenter, int yCenter) {  pix = 250; // 颜色值,默认为白色  int x, y, r2;  int radius = r;  r2 = r * r;  // 绘制圆的上下左右四个点  setPixel(pix, xCenter, yCenter + radius);  setPixel(pix, xCenter, yCenter - radius);  setPixel(pix, xCenter + radius, yCenter);  setPixel(pix, xCenter - radius, yCenter);  y = radius;  x = 1;  y = (int) (Math.sqrt(r2 - 1) + 0.5);  // 边缘填充算法, 其实可以直接对循环所有像素,计算到做中心点距离来做  // 这个方法是别人写的,发现超赞,超好!  while (x < y) {  setPixel(pix, xCenter + x, yCenter + y);  setPixel(pix, xCenter + x, yCenter - y);  setPixel(pix, xCenter - x, yCenter + y);  setPixel(pix, xCenter - x, yCenter - y);  setPixel(pix, xCenter + y, yCenter + x);  setPixel(pix, xCenter + y, yCenter - x);  setPixel(pix, xCenter - y, yCenter + x);  setPixel(pix, xCenter - y, yCenter - x);  x += 1;  y = (int) (Math.sqrt(r2 - x * x) + 0.5);  }  if (x == y) {  setPixel(pix, xCenter + x, yCenter + y);  setPixel(pix, xCenter + x, yCenter - y);  setPixel(pix, xCenter - x, yCenter + y);  setPixel(pix, xCenter - x, yCenter - y);  }  }  public int[] getAcc() {  return acc;  }  }  


这篇关于图像处理之霍夫变换圆检测算法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1055564

相关文章

Python如何实现PDF隐私信息检测

《Python如何实现PDF隐私信息检测》随着越来越多的个人信息以电子形式存储和传输,确保这些信息的安全至关重要,本文将介绍如何使用Python检测PDF文件中的隐私信息,需要的可以参考下... 目录项目背景技术栈代码解析功能说明运行结php果在当今,数据隐私保护变得尤为重要。随着越来越多的个人信息以电子形

SpringBoot使用Apache Tika检测敏感信息

《SpringBoot使用ApacheTika检测敏感信息》ApacheTika是一个功能强大的内容分析工具,它能够从多种文件格式中提取文本、元数据以及其他结构化信息,下面我们来看看如何使用Ap... 目录Tika 主要特性1. 多格式支持2. 自动文件类型检测3. 文本和元数据提取4. 支持 OCR(光学

Python中的随机森林算法与实战

《Python中的随机森林算法与实战》本文详细介绍了随机森林算法,包括其原理、实现步骤、分类和回归案例,并讨论了其优点和缺点,通过面向对象编程实现了一个简单的随机森林模型,并应用于鸢尾花分类和波士顿房... 目录1、随机森林算法概述2、随机森林的原理3、实现步骤4、分类案例:使用随机森林预测鸢尾花品种4.1

不懂推荐算法也能设计推荐系统

本文以商业化应用推荐为例,告诉我们不懂推荐算法的产品,也能从产品侧出发, 设计出一款不错的推荐系统。 相信很多新手产品,看到算法二字,多是懵圈的。 什么排序算法、最短路径等都是相对传统的算法(注:传统是指科班出身的产品都会接触过)。但对于推荐算法,多数产品对着网上搜到的资源,都会无从下手。特别当某些推荐算法 和 “AI”扯上关系后,更是加大了理解的难度。 但,不了解推荐算法,就无法做推荐系

康拓展开(hash算法中会用到)

康拓展开是一个全排列到一个自然数的双射(也就是某个全排列与某个自然数一一对应) 公式: X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0! 其中,a[i]为整数,并且0<=a[i]<i,1<=i<=n。(a[i]在不同应用中的含义不同); 典型应用: 计算当前排列在所有由小到大全排列中的顺序,也就是说求当前排列是第

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

综合安防管理平台LntonAIServer视频监控汇聚抖动检测算法优势

LntonAIServer视频质量诊断功能中的抖动检测是一个专门针对视频稳定性进行分析的功能。抖动通常是指视频帧之间的不必要运动,这种运动可能是由于摄像机的移动、传输中的错误或编解码问题导致的。抖动检测对于确保视频内容的平滑性和观看体验至关重要。 优势 1. 提高图像质量 - 清晰度提升:减少抖动,提高图像的清晰度和细节表现力,使得监控画面更加真实可信。 - 细节增强:在低光条件下,抖

【数据结构】——原来排序算法搞懂这些就行,轻松拿捏

前言:快速排序的实现最重要的是找基准值,下面让我们来了解如何实现找基准值 基准值的注释:在快排的过程中,每一次我们要取一个元素作为枢纽值,以这个数字来将序列划分为两部分。 在此我们采用三数取中法,也就是取左端、中间、右端三个数,然后进行排序,将中间数作为枢纽值。 快速排序实现主框架: //快速排序 void QuickSort(int* arr, int left, int rig

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

烟火目标检测数据集 7800张 烟火检测 带标注 voc yolo

一个包含7800张带标注图像的数据集,专门用于烟火目标检测,是一个非常有价值的资源,尤其对于那些致力于公共安全、事件管理和烟花表演监控等领域的人士而言。下面是对此数据集的一个详细介绍: 数据集名称:烟火目标检测数据集 数据集规模: 图片数量:7800张类别:主要包含烟火类目标,可能还包括其他相关类别,如烟火发射装置、背景等。格式:图像文件通常为JPEG或PNG格式;标注文件可能为X