title: Hough Transform - Finding the center of the hough transform circles
body: <p>I have been reading up quite a bit on Hough circles and how it can be used to detect the center and radius of a circular object. Although I understood how the transform works I am still not able to to understand how to get the center of a circle. The code snippet below is customized purely for coins.png in MATLAB. I tried it with a slightly more complex picture and it fails miserably. I am fixing the radius and then getting the accumulator matrix. </p>
  
  <p>Code: </p>
  
  <pre><code>temp = accum;
  temp(find(temp&lt;40))=0; %Thresholding
  imshow(temp,[0 max(temp(:))])
  temp = imdilate(temp, ones(6,6)); %Dilating
  imshow(temp,[0 max(max(temp))]);
  temp = imerode(temp,ones(3,3)); %Eroding
  imshow(temp,[0 max(max(temp))]);
  s = regionprops(im2bw(temp),'centroid'); %Computing centroid
  centroids = cat(1, s.Centroid);
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/eJKnp.jpg" alt="Hough Transform of coins.png with radius = 28"></p>
  
  <p>I wanted to test the code out on a different picture and found <a href="http://www.earlylearninghq.org.uk/wp-content/uploads/2010/06/coins4.jpg" rel="nofollow noreferrer">this one</a> on google. The Hough Transform produced a favorable result, but it's more overlapping than the previous case and my method fails. </p>
  
  <p><img src="https://i.stack.imgur.com/723ej.jpg" alt="Fairly decent Hough transform of the picture from Google"></p>
  
  <p>Can someone suggest what the best method is to compute the centroid of the hough circle?</p>
  
  <hr>
  
  <p>Original image:</p>
  
  <p><img src="https://i.stack.imgur.com/tIEbQ.jpg" alt="enter image description here"></p>
  
  <p>Full  Code: </p>
  
  <pre><code>%%Read and find edges using a filter
  i = imread('coins4.jpg');
  i = rgb2gray(i);
  i = imresize(i,0.125);
  i_f = edge(i, 'canny',[0.01 0.45]);
  imshow(i_f)
  
  %%
  
  [x y] = find(i_f&gt;0); % Finds where the edges are and stores the x and y coordinates
  edge_index = size(x);
  radius = 30; %Fixed radius value
  theta = 0:0.01:2*pi;
  accum = zeros(size(i,1), size(i,2)); %Create an accumulator matrix.
  
  r_co = radius*cos(theta);
  r_si = radius*sin(theta);
  
  x1 = repmat(x, 1, length(r_co));
  y1 = repmat(y, 1, length(r_si));
  x_r_co = repmat(r_co, length(x),1);
  y_r_si = repmat(r_si, length(y),1);
  
  %% Filling the accumulator
  a = x1 - x_r_co;
  b = y1 - y_r_si;
  
  for cnt = 1:numel(a)
      a_cnt = round(a(cnt));
      b_cnt = round(b(cnt));
      if(a_cnt&gt;0 &amp;&amp; b_cnt&gt;0 &amp;&amp; a_cnt&lt;=size(accum,1) &amp;&amp; b_cnt&lt;=size(accum,2))
          accum(a_cnt,b_cnt) = accum(a_cnt,b_cnt) + 1;
      end
  end
  
  imshow(accum,[0 max(max(accum))]);
  
   %% Thresholding and get the center of the circle.
  close all;
  temp = accum;
  temp(find(temp&lt;40))=0;
  imshow(temp,[0 max(temp(:))])
  temp = imdilate(temp, ones(6,6));
  imshow(temp,[0 max(max(temp))]);
  temp = imerode(temp,ones(3,3));
  imshow(temp,[0 max(max(temp))]);
  s = regionprops(im2bw(temp),'centroid');
  centroids = cat(1, s.Centroid);
  imshow(i);
  hold on;
  plot(centroids(:,1), centroids(:,2),'*b')
  </code></pre>