Code covered by the BSD License  

Highlights from
Detects multiple disks (coins) in an image using Hough Transform

4.42857

4.4 | 7 ratings Rate this file 41 Downloads (last 30 days) File Size: 3.17 KB File ID: #22543
image thumbnail

Detects multiple disks (coins) in an image using Hough Transform

by Yuan-Liang Tang

 

29 Dec 2008 (Updated 07 Jan 2009)

HOUGHCIRCLES detects multiple disks (coins) in an image using Hough Transform.

| Watch this File

File Information
Description

HOUGHCIRCLES detects multiple disks (coins) in an image using Hough Transform. The image contains separating, touching, or overlapping disks whose centers may be in or out of the image.

Syntax
  houghcircles(im, minR, maxR);
  houghcircles(im, minR, maxR, thresh);
  houghcircles(im, minR, maxR, thresh, delta);
  circles = houghcircles(im, minR, maxR);
  circles = houghcircles(im, minR, maxR, thresh);
  circles = houghcircles(im, minR, maxR, thresh, delta);

Inputs:
  - im: input image
  - minR: minimal radius in pixels
  - maxR: maximal radius in pixels
  - thresh (optional): the minimal ratio of the number of detected edge pixels to 0.9 times the calculated circle perimeter (0<thresh<=1, default: 0.33)
  - delta (optional): the maximal difference between two circles for them to be considered as the same one (default: 12); e.g., c1=(x1 y1 r1), c2=(x2 y2 r2), delta = |x1-x2|+|y1-y2|+|r1-r2|

Output
  - circles: n-by-4 array of n circles; each circle is represented by (x y r t), where (x y), r, and t are the center coordinate, radius, and ratio of the detected portion to the circle perimeter, respectively. If the output argument is not specified, the original image will be displayed with the detected circles superimposed on it.

Required Products Image Processing Toolbox
MATLAB release MATLAB 7.5 (R2007b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (12)
07 Jan 2009 Sven

Nice function. Code is well documented and clearly written.
One suggestion is to follow the example of the peaks() function: if no output argument is given, then create a figure and display the image. If it's used with an output argument, assume the user is embedding the function in their own code, and doesn't want a figure to come up automatically.

06 Apr 2009 Idillus

Hi, nice function. I've made some changes that may be interesting. It wold be nice to change the threshold and sigma value for canny edge detection, so, I added a few lines to improve this, changing the location of the definition of the edgeimage

if nargin==3
    
    thresh = 0.33; % One third of the perimeter
    delta = 12; % Each element in (x y r) may deviate approx. 4 pixels
    edgeim = edge(im, 'canny', [0.15 0.2]);

elseif nargin==4

    if ((max(size(canny_th) == 2)) || (max(size(canny_th) == 1)))
        if max(size(canny_th) == 2)
            edgeim = edge(im, 'canny', [canny_th(1) canny_th(2)]);
        end
        if max(size(canny_th) == 1)
            edgeim = edge(im, 'canny', canny_th(1));
        end
    end
    delta = 12;
    
else (nargin==5)

    if ((max(size(canny_th) == 2)) || (max(size(canny_th) == 1)))
        if (max(size(canny_th) == 2))
            edgeim = edge(im, 'canny', [canny_th(1) canny_th(2)],sigma);
        end
        if (max(size(canny_th) == 1))
            edgeim = edge(im, 'canny', canny_th(1),sigma);
        end
    end
    delta = 12;
end

06 Apr 2009 Idillus

Sorry, I've made ma mistake

if (nargin >=3 || nargin <= 6)
    
    if nargin==3
        thresh = 0.33; % One third of the perimeter
        delta = 12; % Each element in (x y r) may deviate approx. 4 pixels
        edgeim = edge(im, 'canny', [0.15 0.2]);
    end
    
    if nargin==4
        edgeim = edge(im, 'canny', [0.15 0.2]);
        delta = 12;
    end
    
    if (nargin==5)
        edgeim = edge(im, 'canny', canny_th);
        delta = 12;
    end
    
    if (nargin==6)
       edgeim = edge(im, 'canny', canny_th,sigma);
       delta=12;
    end
    if (nargin == 7)
         edgeim = edge(im, 'canny', canny_th,sigma);
    end
end

if minR<0 || maxR<0 || minR>maxR || thresh<0 || thresh>1 || delta<0 || canny_th <0 || canny_th >1
  disp('Input conditions: 0<minR, 0<maxR, minR<=maxR, 0<thresh<=1, 0<delta');
  return;
end

16 May 2009 Rezki Al Khairi

i've try to make with GUI...
but i cannot place where the instrustion mace be placed...
can u help me pleasee...
i really need it..

11 Jun 2009 sanjay bhattacharya

i used the houhcircles function. it says 'out of memory' for my test images; but it worked for very small binary images; please help

11 Jun 2009 Yuan-Liang Tang

sanjay,
The most probable cause might be that you invoked the function using inappropriate parameters. The function allocate a 3D matrix of size [size(im,1)+maxR, size(im,2)+maxR, maxR-minR+1], where minR and maxR are the minimal and maximal radii of circles you want to detect, respectively. You may want to check if the size of your input image or the values of minR and maxR are really huge.

15 Jul 2009 TUYEN Nguyen Ba

Thank you Prof. Yuan Liang Tang. This is really great!

22 Jul 2009 Venugopalakrishna

Thank you very much.

30 Jul 2009 Nazatul Naquiah Ahba

hi, i've ran this code and it works. i wonder which part of the code display as accumulation array? can you pls guide me how to create the accumulation array from this code in order to generate the output figure of hough transform accumulation array?

07 Dec 2009 Christoph  
10 Apr 2011 Sushma Bhandari

im unable to detect the circles.what should be the minR and mixR,can anyone help me?

10 Apr 2011 Yuan-Liang Tang

Sushma,
  minR and maxR are the minimal and maximal radii (in pixels), respectively, of the circles that you want to detect in your image. For the image I provided along with the program, I set minR=20 and maxR=40. If you still have problems, you may want to provide your image and I'll look into the problem.

Yuan-Liang Tang

Please login to add a comment or rating.
Updates
29 Dec 2008

Description about the format of function invocation is corrected.

29 Dec 2008

Description of the format of function invocation is corrected.

31 Dec 2008

Major modifications of the program.

07 Jan 2009

Minor modifications according to Sven's suggestions. Thanks a lot, Sven.

Tag Activity for this File
Tag Applied By Date/Time
image processing Yuan-Liang Tang 29 Dec 2008 15:54:57
circles Cristina McIntire 30 Dec 2008 10:29:15
hough transform Cristina McIntire 30 Dec 2008 10:29:15
center ?? 25 Jan 2012 07:36:59

Contact us at files@mathworks.com