Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. %318810637 Perov Danny
  2. %318455813 Eden Klainer
  3. %
  4. %The function finds circles in the given grayscale image using Hough transform.
  5. %It also prints a list of the circles in the image.
  6. %Input: grayscale image
  7. %Output: list of circles, image which contains the original image with the
  8. %marked circles.
  9. function [circles,cImg] = findCircles(img)
  10.  
  11. %Detect the edges in the image.
  12. edges = edgeDetect(img);
  13.  
  14. [rows, cols] = size(edges);
  15.  
  16. %Create the count matrix.
  17. Rm = round(min(rows, cols) / 2);
  18. Count = zeros(cols, rows, Rm);
  19.  
  20. %Go over all the pixels, and for the ones that are turned on, calculate the
  21. %radius, and increment the Count matrix.
  22. for colIndex = 1 : cols
  23. for rowIndex = 1 : rows
  24. if(edges(rowIndex, colIndex) == 1)
  25. for Cx = 1 : cols
  26. for Cy = 1 : rows
  27. radius = round(sqrt((colIndex - Cx)^2 + (rowIndex - Cy)^2));
  28. if(radius > 0 && radius < Rm)
  29. Count(Cx, Cy, radius) = Count(Cx, Cy, radius) + 1;
  30. end
  31. end
  32. end
  33. end
  34. end
  35. end
  36.  
  37. %TODO add explanation to the threshold
  38. threshold = max(max(max(Count))) * 0.85; % there is a good result when it's 60
  39.  
  40. result = [];
  41.  
  42. %Go over the Count matrix, detect the circles, and add them to the list.
  43. for Cx = 1 : cols
  44. for Cy = 1 : rows
  45. for radius = 1 : min(rows, cols)
  46.  
  47. %threshold = pi * radius;
  48.  
  49. if(radius > 0 && radius < Rm && Count(Cx, Cy, radius) > threshold)
  50. if(isMaximal(Count, Cx, Cy, radius))
  51. result = [result ; Cx, Cy, radius];
  52. end
  53. end
  54. end
  55. end
  56. end
  57.  
  58. circles = filterCircles(result);
  59.  
  60. %Print the list into the screen.
  61. printList(circles);
  62.  
  63. %Mark the circles in the original image.
  64. cImg = addCircles(img, circles);
  65.  
  66. end
  67.  
  68. %The function filters the circles list.
  69. %Input: circles list
  70. %Output: filtered list
  71. function filteredList = filterCircles(circles)
  72.  
  73. [rows, cols] = size(circles);
  74. filteredList = [];
  75. counter = 1;
  76.  
  77. for r1 = 1 : rows
  78. for r2 = 1 : rows
  79. if(r1 ~= r2)
  80. x1 = circles(r1, 1);
  81. y1 = circles(r1, 2);
  82. x2 = circles(r2, 1);
  83. y2 = circles(r2, 2);
  84. distance = sqrt((x1 - x2)^2 + (y1 - y2)^2);
  85. if(distance < 5)
  86. %if(y1 < y2)
  87. %filteredList = [filteredList ; x1, y1, circles(r1, 3)];
  88. %counter = counter + 1;
  89. circles(r2, 1) = 0;
  90. circles(r2, 2) = 0;
  91. circles(r2, 3) = 0;
  92. % else
  93. % circles(r1, 1) = 0;
  94. % circles(r1, 2) = 0;
  95. % circles(r1, 3) = 0;
  96. % end
  97. end
  98. end
  99. end
  100. end
  101.  
  102. for r = 1 : size(circles, 1)
  103. if(circles(r, 3) ~= 0)
  104. filteredList = [filteredList ; circles(r, 1), circles(r, 2), circles(r, 3)];
  105. end
  106. end
  107.  
  108. end
  109.  
  110. %The function prints a given list according to the stated format.
  111. %Input: circles list.
  112. function [] = printList(circles)
  113.  
  114. rows = size(circles, 1);
  115.  
  116. for r = 1 : rows
  117. fprintf('Circle %d: %d, %d, %d\n',r ,circles(r, 1), circles(r, 2), circles(r, 3));
  118. end
  119. end
  120.  
  121. %The function checks if the given value is maximal when compared to its
  122. %neighbours.
  123. %Input: Count matrix, Cx, Cy, radius.
  124. %Output: boolean
  125. function answer = isMaximal(Count, Cx, Cy, radius)
  126.  
  127. currentValue = Count(Cx, Cy, radius);
  128. [rows, cols, depth] = size(Count);
  129.  
  130. if(Cx > 1 &&Count(Cx - 1, Cy, radius) > currentValue)
  131. answer = 0;
  132. elseif (Cx < cols && Count(Cx + 1, Cy, radius) > currentValue)
  133. answer = 0;
  134. elseif(Cy > 1 && Count(Cx, Cy - 1, radius) > currentValue)
  135. answer = 0;
  136. elseif(Cy < rows && Count(Cx, Cy + 1, radius) > currentValue)
  137. answer = 0;
  138. elseif(radius > 1 && Count(Cx, Cy, radius - 1) > currentValue)
  139. answer = 0;
  140. elseif(radius < depth && Count(Cx, Cy, radius + 1) > currentValue)
  141. answer = 0;
  142. else
  143. answer = 1;
  144. end
  145. end
  146.  
  147. %The function draws circles on the given image, using the given list.
  148. function imageWithCircles = addCircles(img, circles)
  149.  
  150. rows = (size(circles, 1));
  151.  
  152. if(rows > 0)
  153.  
  154. circleParameters = circles(1, :);
  155. imageWithCircles = insertShape(img,'circle',circleParameters,'LineWidth',2);
  156.  
  157. for r = 2 : rows
  158. circleParameters = circles(r, :);
  159. imageWithCircles = insertShape(imageWithCircles,'circle',circleParameters,'LineWidth',2);
  160. end
  161. end
  162. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement