Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. function [circles,cImg] = findCircles(img)
  2.  
  3. edges = edgeDetect(img);
  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 = max(max(max(Count))) * 0.85; % there is a good result when it's 60
  26.  
  27. result = [];
  28.  
  29. for Cx = 1 : cols
  30. for Cy = 1 : rows
  31. for radius = 1 : min(rows, cols)
  32.  
  33. %threshold = 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. circles = filterCircles(result);
  45.  
  46. %Print the list into the screen.
  47. printList(circles);
  48.  
  49. %Mark the circles in the original image.
  50. cImg = addCircles(img, circles);
  51.  
  52. end
  53.  
  54.  
  55. function filteredList = filterCircles(circles)
  56.  
  57. [rows, cols] = size(circles);
  58. filteredList = [];
  59. counter = 1;
  60.  
  61. for r1 = 1 : rows
  62. for r2 = 1 : rows
  63. if(r1 ~= r2)
  64. x1 = circles(r1, 1);
  65. y1 = circles(r1, 2);
  66. x2 = circles(r2, 1);
  67. y2 = circles(r2, 2);
  68. distance = sqrt((x1 - x2)^2 + (y1 - y2)^2);
  69. if(distance < 5)
  70. % if(y1 > y2)
  71. %filteredList = [filteredList ; x1, y1, circles(r1, 3)];
  72. %counter = counter + 1;
  73. circles(r2, 1) = 0;
  74. circles(r2, 2) = 0;
  75. circles(r2, 3) = 0;
  76. % else
  77. % circles(r1, 1) = 0;
  78. % circles(r1, 2) = 0;
  79. % circles(r1, 3) = 0;
  80. % end
  81. end
  82. end
  83. end
  84. end
  85.  
  86. for r = 1 : size(circles, 1)
  87. if(circles(r, 3) ~= 0)
  88. filteredList = [filteredList ; circles(r, 1), circles(r, 2), circles(r, 3)];
  89. end
  90. end
  91.  
  92. end
  93.  
  94.  
  95. function [] = printList(circles)
  96.  
  97. rows = size(circles, 1);
  98.  
  99. for r = 1 : rows
  100. fprintf('Circle %d: %d, %d, %d\n',r ,circles(r, 1), circles(r, 2), circles(r, 3));
  101. end
  102. end
  103.  
  104. function answer = isMaximal(Count, Cx, Cy, radius)
  105.  
  106. currentValue = Count(Cx, Cy, radius);
  107. [rows, cols, depth] = size(Count);
  108.  
  109. if(Cx > 1 &&Count(Cx - 1, Cy, radius) > currentValue)
  110. answer = 0;
  111. elseif (Cx < cols && Count(Cx + 1, Cy, radius) > currentValue)
  112. answer = 0;
  113. elseif(Cy > 1 && Count(Cx, Cy - 1, radius) > currentValue)
  114. answer = 0;
  115. elseif(Cy < rows && Count(Cx, Cy + 1, radius) > currentValue)
  116. answer = 0;
  117. elseif(radius > 1 && Count(Cx, Cy, radius - 1) > currentValue)
  118. answer = 0;
  119. elseif(radius < depth && Count(Cx, Cy, radius + 1) > currentValue)
  120. answer = 0;
  121. else
  122. answer = 1;
  123. end
  124. end
  125.  
  126.  
  127. function imageWithCircles = addCircles(img, circles)
  128.  
  129. rows = (size(circles, 1));
  130.  
  131. if(rows > 0)
  132.  
  133. circleParameters = circles(1, :);
  134. imageWithCircles = insertShape(img,'circle',circleParameters,'LineWidth',2);
  135.  
  136. for r = 2 : rows
  137. circleParameters = circles(r, :);
  138. imageWithCircles = insertShape(imageWithCircles,'circle',circleParameters,'LineWidth',2);
  139. end
  140. end
  141. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement