Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 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 = 80;
  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. %filteredList = [filteredList ; x1, y1, circles(r1, 3)];
  71. %counter = counter + 1;
  72. circles(r2, 1) = 0;
  73. circles(r2, 2) = 0;
  74. circles(r2, 3) = 0;
  75. end
  76. end
  77. end
  78. end
  79.  
  80. for r = 1 : size(circles, 1)
  81. if(circles(r, 3) ~= 0)
  82. filteredList = [filteredList ; circles(r, 1), circles(r, 2), circles(r, 3)];
  83. end
  84. end
  85.  
  86. end
  87.  
  88.  
  89. function [] = printList(circles)
  90.  
  91. rows = size(circles, 1);
  92.  
  93. for r = 1 : rows
  94. fprintf('Circle %d: %d, %d, %d\n',r ,circles(r, 1), circles(r, 2), circles(r, 3));
  95. end
  96. end
  97.  
  98. function answer = isMaximal(Count, Cx, Cy, radius)
  99.  
  100. currentValue = Count(Cx, Cy, radius);
  101. [rows, cols, depth] = size(Count);
  102.  
  103. if(Cx > 1 &&Count(Cx - 1, Cy, radius) > currentValue)
  104. answer = 0;
  105. elseif (Cx < cols && Count(Cx + 1, Cy, radius) > currentValue)
  106. answer = 0;
  107. elseif(Cy > 1 && Count(Cx, Cy - 1, radius) > currentValue)
  108. answer = 0;
  109. elseif(Cy < rows && Count(Cx, Cy + 1, radius) > currentValue)
  110. answer = 0;
  111. elseif(radius > 1 && Count(Cx, Cy, radius - 1) > currentValue)
  112. answer = 0;
  113. elseif(radius < depth && Count(Cx, Cy, radius + 1) > currentValue)
  114. answer = 0;
  115. end
  116.  
  117. answer = 1;
  118.  
  119. end
  120.  
  121.  
  122. function imageWithCircles = addCircles(img, circles)
  123.  
  124. rows = (size(circles, 1));
  125.  
  126. if(rows > 0)
  127.  
  128. circleParameters = circles(1, :);
  129. imageWithCircles = insertShape(img,'circle',circleParameters,'LineWidth',2);
  130.  
  131. for r = 2 : rows
  132. circleParameters = circles(r, :);
  133. imageWithCircles = insertShape(imageWithCircles,'circle',circleParameters,'LineWidth',2);
  134. end
  135. end
  136. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement