图像处理之霍夫变换圆检测算法.docx
- 文档编号:30253116
- 上传时间:2023-08-13
- 格式:DOCX
- 页数:19
- 大小:124.48KB
图像处理之霍夫变换圆检测算法.docx
《图像处理之霍夫变换圆检测算法.docx》由会员分享,可在线阅读,更多相关《图像处理之霍夫变换圆检测算法.docx(19页珍藏版)》请在冰豆网上搜索。
图像处理之霍夫变换圆检测算法
图像处理之霍夫变换圆检测算法
一:
霍夫变换检测圆的数学原理
根据极坐标,圆上任意一点的坐标可以表示为如上形式,所以对于任意一个圆,假设中心像素点p(x0,y0)像素点已知,圆半径已知,则旋转360由极坐标方程可以得到每个点上得坐标同样,如果只是知道图像上像素点,圆半径,旋转360°则中心点处的坐标值必定最强.这正是霍夫变换检测圆的数学原理.
二:
算法流程
该算法大致可以分为以下几个步骤
三:
运行效果
图像从空间坐标变换到极坐标效果,最亮一点为圆心.
图像从极坐标变换回到空间坐标,检测结果显示:
四:
关键代码解析
个人觉得这次注释已经是非常的详细啦,而且我写的还是中文注释
[java] viewplaincopy
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] viewplaincopy
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];
28. output = new int[width * height];
29. input = inputIn;
30. for (int y = 0; y < height; y++) {
31. for (int x = 0; x < width; x++) {
32. output[x + (width * y)] = 0xff000000; //默认图像背景颜色为黑色
33. }
34. }
35. }
36.
37. public void setCircles(int circles) {
38. accSize = circles; // 检测的个数
39. }
40.
41. /**
42. * 霍夫变换处理 - 检测半径大小符合的圆的个数
43. * 1. 将图像像素从2D空间坐标转换到极坐标空间
44. * 2. 在极坐标空间中归一化各个点强度,使之在0〜255之间
45. * 3. 根据极坐标的R值与输入参数(圆的半径)相等,寻找2D空间的像素点
46. * 4. 对找出的空间像素点赋予结果颜色(红色)
47. * 5. 返回结果2D空间像素集合
48. * @return int []
49. */
50. public int[] process() {
51.
52. // 对于圆的极坐标变换来说,我们需要360度的空间梯度叠加值
53. acc = new int[width * height];
54. for (int y = 0; y < height; y++) {
55. for (int x = 0; x < width; x++) {
56. acc[y * width + x] = 0;
57. }
58. }
59. int x0, y0;
60. double t;
61. for (int x = 0; x < width; x++) {
62. for (int y = 0; y < height; y++) {
63.
64. if ((input[y * width + x] & 0xff) == 255) {
65.
66. for (int theta = 0; theta < 360; theta++) {
67. t = (theta * 3.14159265) / 180; // 角度值0 ~ 2*PI
68. x0 = (int) Math.round(x - r * Math.cos(t));
69. y0 = (int) Math.round(y - r * Math.sin(t));
70. if (x0 < width && x0 > 0 && y0 < height && y0 > 0) {
71. acc[x0 + (y0 * width)] += 1;
72. }
73. }
74. }
75. }
76. }
77.
78. // now normalise to 255 and put in format for a pixel array
79. int max = 0;
80.
81. // Find max acc value
82. for (int x = 0; x < width; x++) {
83. for (int y = 0; y < height; y++) {
84.
85. if (acc[x + (y * width)] > max) {
86. max = acc[x + (y * width)];
87. }
88. }
89. }
90.
91. // 根据最大值,实现极坐标空间的灰度值归一化处理
92. int value;
93. for (int x = 0; x < width; x++) {
94. for (int y = 0; y < height; y++) {
95. value = (int) (((double) acc[x + (y * width)] / (double) max) * 255.0);
96. acc[x + (y * width)] = 0xff000000 | (value << 16 | value << 8 | value);
97. }
98. }
99.
100. // 绘制发现的圆
101. findMaxima();
102. System.out.println("done");
103. return output;
104. }
105.
106. private int[] findMaxima() {
107. results = new int[accSize * 3];
108. int[] output = new int[width * height];
109.
110. // 获取最大的前accSize个值
111. for (int x = 0; x < width; x++) {
112. for (int y = 0; y < height; y++) {
113. int value = (acc[x + (y * width)] & 0xff);
114.
115. // if its higher than lowest value add it and then sort
116. if (value > results[(accSize - 1) * 3]) {
117.
118. // add to bottom of array
119. results[(accSize - 1) * 3] = value; //像素值
120. results[(accSize - 1) * 3 + 1] = x; // 坐标X
121. results[(accSize - 1) * 3 + 2] = y; // 坐标Y
122.
123. // shift up until its in right place
124. int i = (accSize - 2) * 3;
125. while ((i >= 0) && (results[i + 3] > results[i])) {
126. for (int j = 0; j < 3; j++) {
127. int temp = results[i + j];
128. results[i + j] = results[i + 3 + j];
129. results[i + 3 + j] = temp;
130. }
131. i = i - 3;
132. if (i < 0)
133. break;
134. }
135. }
136. }
137. }
138.
139. // 根据找到的半径R,中心点像素坐标p(x, y),绘制圆在原图像上
140. System.out.println("top " + accSize + " matches:
");
141. for (int i = accSize - 1; i >= 0; i--) {
142. drawCircle(results[i * 3], results[i * 3 + 1], results[i * 3 + 2]);
143. }
144. return output;
145. }
146.
147. private void setPixel(int value, int xPos, int yPos) {
148. /// output[(yPos * width) + xPos] = 0xff000000 | (value << 16 | value << 8 | value);
149. output[(yPos * width) + xPos] = 0xffff0000;
150. }
151.
152. // draw circle at x y
153. private void drawCircle(int pix, int xCenter, int yCenter) {
154. pix = 250; // 颜色值,默认为白色
155.
156. int x, y, r2;
157. int radius = r;
158. r2 = r * r;
159.
160. // 绘制圆的上下左右四个点
161. setPixel(pix, xCenter, yCenter + radius);
162. setPixel(pix, xCenter, yCenter - radius);
163. setPixel(pix, xCenter + radius, yCenter);
164. setPixel(pix, xCenter - radius, yCenter);
165.
166. y = radius;
167. x = 1;
168. y = (int) (Math.sqrt(r2 - 1) + 0.5);
169.
170. // 边缘填充算法, 其实可以直接对循环所有像素,计算到做中心点距离来做
171. // 这个方法是别人写的,发现超赞,超好!
172. while (x < y) {
173. setPixel(pix, xCenter + x, yCenter + y);
174. setPixel(pix, xCenter + x, yCenter - y);
175. setPixel(pix, xCenter - x, yCenter + y);
176. setPixel(pix, xCenter - x, yCenter - y);
177. setPixel(pix, xCenter + y, yCenter + x);
178. setPixel(pix, xCenter + y, yCenter - x);
179. setPixel(pix, xCenter - y, yCenter + x);
180. setPixel(pix, xCenter - y, yCenter - x);
181. x += 1;
182. y = (int) (Math.sqrt(r2 - x * x) + 0.5);
183. }
184. if (x == y) {
185. setPixel(pix, xCenter + x, yCenter + y);
186. setPixel(pix, xCenter + x, yCenter - y);
187. setPixel(pix, xCenter - x, yCenter + y);
188.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 图像 处理 变换 检测 算法