Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. function [circles,cImg] = findCircles(img)
  2.  
  3. edges = edge(img, 'Canny');
  4.  
  5. [rows, cols] = size(edges);
  6.  
  7. Rm = round(min(rows, cols) / 2);
  8. Count = zeros(rows, cols, Rm);
  9.  
  10. for colIndex = 1 : cols
  11. for rowIndex = 1 : rows
  12. if(edges(rowIndex, colIndex) == 1)
  13. for Cx = 1 : cols
  14. for Cy = 1 : rows
  15. radius = round(sqrt((colIndex - Cx)^2 + (rowIndex - Cy)^2));
  16. if(radius > 0 && radius < Rm)
  17. Count(Cx, Cy, radius) = Count(Cx, Cy, radius) + 1;
  18. end
  19. end
  20. end
  21. end
  22. end
  23. end
  24.  
  25. threshold = 5;
  26.  
  27. result = [];
  28.  
  29. for Cx = 1 : cols
  30. for Cy = 1 : rows
  31. for radius = 1 : min(rows, cols)
  32.  
  33. threshold = 80;%0.7*pi * radius;
  34.  
  35. if(radius > 0 && radius < Rm && Count(Cx, Cy, radius) > threshold)
  36. if(isMaximal(Count, Cx, Cy, radius))
  37. result = [result ; Cx, Cy, radius];
  38. end
  39. end
  40. end
  41. end
  42. end
  43.  
  44.  
  45. circles = filterCircles(result);
  46. cImg = addCircles(img, circles);
  47.  
  48. end
  49. % [rows, cols] = size(img);
  50. %
  51. % %Find edges using edge detection algorithm.
  52. % edges = edge(img, 'Canny');%TODO return this: edges = edgeDetect(img);
  53. %
  54. % Rm = round(min(rows, cols) / 2);%round(sqrt(rows^2 + cols^2)); round(sqrt(rows^2 + cols^2));
  55. % Count = zeros(cols, rows, Rm);
  56. %
  57. % for y = 1: rows
  58. % for x = 1 : cols
  59. % if edges(y, x) == 1
  60. % for Cy = 1 : rows
  61. % for Cx = 1 : cols
  62. % r = round(sqrt((x - Cx)^2 + (y - Cy)^2));
  63. % if(r > 0 && r < Rm)
  64. % Count(Cx, Cy, r) = Count(Cx, Cy, r) + 1;
  65. % end;
  66. % end
  67. % end
  68. % end
  69. % end
  70. % end
  71. %
  72. % % for x = 1 : rows
  73. % % for y = 1 : cols
  74. % % if (edges(y, x) == 1)
  75. % % for cX = 1 : cols
  76. % % for cY = 1 : rows
  77. % % r = round(sqrt((x - cX)^2 + (y - cY)^2));
  78. % % %X = sprintf('r = %d x = %d y = %d cX = %d cY = %d', r, x, y, cX, cY);
  79. % % %disp(X);
  80. % % if(r > 0 && r < Rm)
  81. % % Count(cX, cY, r) = Count(cX, cY, r) + 1;
  82. % % end
  83. % % end
  84. % % end
  85. % % end
  86. % % end
  87. % % end
  88. %
  89. % T = 5; % TODO give a correct value
  90. %
  91. % [c_YMax, c_XMax, r_max] = size(Count);
  92. % counter = 1;
  93. %
  94. % %Stores the circles in the format [x, y, r]
  95. % circlesList = zeros(c_YMax, 3);
  96. %
  97. % for c_x = 1 : c_XMax
  98. % for c_y = 1 : c_YMax
  99. % for r = 1 : r_max
  100. % %Check if found a circle
  101. % if(Count(c_x, c_y, r) > T)
  102. % circlesList(counter, 1) = c_x;
  103. % circlesList(counter, 2) = c_y;
  104. % circlesList(counter, 3) = r;
  105. % counter = counter + 1;
  106. % end
  107. % end
  108. % end
  109. % end
  110. %
  111. % [rows, cols] = size(circlesList);
  112. %
  113. % for r = 1 : rows
  114. % for c = 1 : cols
  115. % CircleNumber = r;
  116. % OriginX = circlesList(r, 1);
  117. % OriginY = circlesList(r, 2);
  118. % Radius = circlesList(r, 3);
  119. % fprintf('Circle %d: %d, %d, %d\n',CircleNumber,OriginX,OriginY,Radius);
  120. % end
  121. % end
  122. %
  123. % circles = circlesList;
  124. % cImg = 0;
  125. %
  126.  
  127. function filteredList = filterCircles(circles)
  128.  
  129. [rows, cols] = size(circles);
  130. filteredList = [];
  131. counter = 1;
  132.  
  133. for r1 = 1 : rows
  134. for r2 = 1 : rows
  135. if(r1 ~= r2)
  136. x1 = circles(r1, 1);
  137. y1 = circles(r1, 2);
  138. x2 = circles(r2, 1);
  139. y2 = circles(r2, 2);
  140. distance = sqrt((x1 - x2)^2 + (y1 - y2)^2);
  141. if(distance < 5)
  142. %filteredList = [filteredList ; x1, y1, circles(r1, 3)];
  143. %counter = counter + 1;
  144. circles(r2, 1) = 0;
  145. circles(r2, 2) = 0;
  146. circles(r2, 3) = 0;
  147. end
  148. end
  149. end
  150. end
  151.  
  152. for r = 1 : size(circles, 1)
  153. if(circles(r, 3) ~= 0)
  154. filteredList = [filteredList ; circles(r, 1), circles(r, 2), circles(r, 3)];
  155. end
  156. end
  157.  
  158. end
  159.  
  160. function answer = isMaximal(Count, Cx, Cy, radius)
  161.  
  162. currentValue = Count(Cx, Cy, radius);
  163. [rows, cols, depth] = size(Count);
  164.  
  165. if(Cx > 1 &&Count(Cx - 1, Cy, radius) > currentValue)
  166. answer = 0;
  167. elseif (Cx < cols && Count(Cx + 1, Cy, radius) > currentValue)
  168. answer = 0;
  169. elseif(Cy > 1 && Count(Cx, Cy - 1, radius) > currentValue)
  170. answer = 0;
  171. elseif(Cy < rows && Count(Cx, Cy + 1, radius) > currentValue)
  172. answer = 0;
  173. elseif(radius > 1 && Count(Cx, Cy, radius - 1) > currentValue)
  174. answer = 0;
  175. elseif(radius < depth && Count(Cx, Cy, radius + 1) > currentValue)
  176. answer = 0;
  177. end
  178.  
  179. answer = 1;
  180.  
  181. end
  182.  
  183.  
  184. function imageWithCircles = addCircles(img, circles)
  185.  
  186. rows = (size(circles, 1));
  187.  
  188. if(rows > 0)
  189.  
  190. circleParameters = circles(1, :);
  191. imageWithCircles = insertShape(img,'circle',circleParameters,'LineWidth',2);
  192.  
  193. for r = 2 : rows
  194. circleParameters = circles(r, :);
  195. imageWithCircles = insertShape(imageWithCircles,'circle',circleParameters,'LineWidth',2);
  196. end
  197. end
  198. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement