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

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

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

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

根据极坐标,圆上任意一点的坐标可以表示为如上形式, 所以对于任意一个圆, 假设中心像素点p(x0, y0)像素点已知, 圆半径已知,则旋转360由极坐标方程可以得到每个点上得坐标同样,如果只是知道图像上像素点, 圆半径,旋转360°则中心点处的坐标值必定最强.这正是霍夫变换检测圆的数学原理.

二:算法流程

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

三:运行效果

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

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

四:关键代码解析

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

1./**

2. * 霍夫变换处理 - 检测半径大小符合的圆的个数

3. * 1. 将图像像素从2D空间坐标转换到极坐标空间

4. * 2. 在极坐标空间中归一化各个点强度,使之在0〜255之间

5. * 3. 根据极坐标的R值与输入参数(圆的半径)相等,寻找2D空间的像素点

6. * 4. 对找出的空间像素点赋予结果颜色(红色)

7. * 5. 返回结果2D空间像素集合

8. * @return int []

9. */

10.public int[] process() {

11.

12.// 对于圆的极坐标变换来说,我们需要360度的空间梯度叠加值

13. acc = new int[width * height];

14.for (int y = 0; y < height; y++) {

15.for (int x = 0; x < width; x++) {

16. acc[y * width + x] = 0;

17. }

18. }

19.int x0, y0;

20.double t;

21.for (int x = 0; x < width; x++) {

22.for (int y = 0; y < height; y++) {

23.

24.if ((input[y * width + x] & 0xff) == 255) {

25.

26.for (int theta = 0; theta < 360; theta++) {

27. t = (theta * 3.14159265) / 180; // 角度值0 ~ 2*PI

28. x0 = (int) Math.round(x - r * Math.cos(t));

29. y0 = (int) Math.round(y - r * Math.sin(t));

30.if (x0 < width && x0 > 0 && y0 < height && y0 > 0) {

31. acc[x0 + (y0 * width)] += 1;

32. }

33. }

34. }

35. }

36. }

37.

38.// now normalise to 255 and put in format for a pixel array

39.int max = 0;

40.

41.// Find max acc value

42.for (int x = 0; x < width; x++) {

43.for (int y = 0; y < height; y++) {

44.

45.if (acc[x + (y * width)] > max) {

46. max = acc[x + (y * width)];

47. }

48. }

49. }

50.

51.// 根据最大值,实现极坐标空间的灰度值归一化处理

52.int value;

53.for (int x = 0; x < width; x++) {

54.for (int y = 0; y < height; y++) {

55. value = (int) (((double) acc[x + (y * width)] / (double) max) *

255.0);

56. acc[x + (y * width)] = 0xff000000 | (value << 16 | value << 8 |

value);

57. }

58. }

59.

60.// 绘制发现的圆

61. findMaxima();

62. System.out.println("done");

63.return output;

64.}

完整的算法源代码, 已经全部的加上注释

[java]view plaincopy

1.package com.gloomyfish.image.transform.hough;

2./***

3. *

4. * 传入的图像为二值图像,背景为黑色,目标前景颜色为为白色

5. * @author gloomyfish

6. *

7. */

8.public class CircleHough {

9.

10.private int[] input;

11.private int[] output;

12.private int width;

13.private int height;

14.private int[] acc;

15.private int accSize = 1;

16.private int[] results;

17.private int r; // 圆周的半径大小

18.

19.public CircleHough() {

20. System.out.println("Hough Circle Detection...");

21. }

22.

23.public void init(int[] inputIn, int widthIn, int heightIn, int radius) {

24. r = radius;

25. width = widthIn;

26. height = heightIn;

27. input = new int[width * height];

相关文档
最新文档