Code covered by the BSD License  

Highlights from
Hough transform for circles

4.52632

4.5 | 20 ratings Rate this file 234 Downloads (last 30 days) File Size: 88.3 KB File ID: #26978
image thumbnail

Hough transform for circles

by

 

16 Mar 2010 (Updated )

A Hough transform function for detecting circles, optimised to allow search over radii.

| Watch this File

File Information
Description

The Hough transform may be used to detect circular shapes in images, after binarisation, for example by an edge detector. Often, functions to do this operation require the radius of the circle to be specified.
The function circle_hough allows a range of radii to be specified, so that the radius does not need to be known exactly in advance. It is likely to be faster than calling a standard function repeatedly for different radii.

Multiple circles may be detected by finding peaks in the 3D accumulator array. A function, circle_houghpeaks, is provided for this. A demonstration of the two functions is included as a script, circle_houghdemo.

The zip file includes an efficient and accurate implementation of circle approximation, without gaps, on an integer grid.

Acknowledgements

Hough Transform For Circle Detection inspired this file.

This file inspired Automated Counting Of Bacterial Colonies.

Required Products Image Processing Toolbox
MATLAB release MATLAB 7.10 (R2010a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (49)
29 Oct 2014 David Young

Hi YIBO, If you change the radius range in the data to start from 16 rather than 15 (so you have "radii = 16:1:40;") then it finds all the coins again.

The demo must have worked with the original radius range when I submitted it. I do not know what has changed to produce the new behaviour. At some point I will update the demo. This does illustrate that the process is verysensitive to the values of the parameters, but this is a property of the algorithm rather than of my particular implementation.

I tried using the image at http://imgur.com/ls65Kwi and my circle detection code works perfectly, detecting all 14 circles without any changes to the parameters in the demo, except of course for the number of circles to find. Is the problem just that you need to increase the number of circles to find from 10 to 14?

24 Oct 2014 YIBO

Hi David! I have tried your program with the coin.png but one of the coin(the one on the down-right) couldn't be detected. And I also tried other images like the one suggested by farhat on 21 Jul 2013(http://imgur.com/ls65Kwi). Still some coins cannot be detected. Is that also happening to you or I did something wrong?

04 Sep 2014 David Young

Thank you, zhan kunhe - you are absolutely right. Now fixed.

03 Sep 2014 zhan kunhe

hi,David,it seems that it has an error in line98 of your houghpeaks.what does two isempty(the same) conbined by '&&',or is isempty(param.nhoodr)&&isempty(param.nhoodxy) instead?I can't understand.please tell me why.

18 Jul 2014 Carsten Killer

Thank you, works great for me!

05 Jun 2014 Nitay Megides  
15 May 2014 David Young

Hi Omar, I think it would be difficult to change this to work for general ellipses. A general ellipse has 5 parameters, and this leads to difficulties with the accumulator array - so methods other than the Hough Transform are probably better. There are some good papers on ellipse fitting - an author to Google is Andrew Fitzgibbon.

14 May 2014 Omar Al Okashi

Thank you very much for great code...
Any hint if I can change it to work on ellipse rather than circle?

With my Best Regards...

08 May 2014 Pappu Murthy

One of the best I have tried among all similar codes. The only one that spotted all the circles in my *.tif file which consists of a micrograph of a composite material with lots of circular fibers in it. The code picked up every single fiber its center and radius correctly. Most other similar programs missed quite a few fibers. Thanks much for such a nice effort. I would also like to see if there is a way to find the concentric circle around each fiber which shows up in the "edge" call but in the end program only picks the fibers and not the concentric rings.

20 Aug 2013 David Boutelier

Hi David

Great tool thanks. Am I correct finding peak location isn't subpixel? How would you find the peak with subpixel precision?

Thanks

04 Aug 2013 David Young

Hi George, Are you sure that the circle_hough function is on your MATLAB path? What is the output from the command "which circle_hough"?

03 Aug 2013 George Jawlakh

Hi David..
I tried the algorithm on my image but Matlab gave me this error :
Undefined function or method 'circle_hough' for input arguments of
type 'double'.

Error in ==> circle_houghdemo at 27
h = circle_hough(e, radii, 'same', 'normalise');
How i can solve this problem
Thx alot

22 Jul 2013 David Young

Hi farhat. I tried it on your image and it worked fine - what's the problem?

21 Jul 2013 farhat

Why this code cannott work for this image http://imgur.com/ls65Kwi

01 Jul 2013 David Young

Hello Abdelmoumen, the code will detect partially hidden circles already. Basically if a fraction P of the circle is visible, the peak for that circle will receive P times the number of votes that the full circle would. Whether a small part of circle is detected depends on the amount of noise in the image - that is, how many other edges are present.

01 Jul 2013 Abdelmoumen

Hello David
Thanks for the answer.I have another question, Could this code be improved to detect patially hidden circles in an image.
Thanks

22 Jun 2013 David Young

Hello setareh, I'm sorry but it is very difficult to answer this kind of query. I'd need to work on the project myself to figure out what kind of code to write - everything depends on the details. I hope you get it figured out.

19 Jun 2013 setareh

Hello David,
I have a question, I used the imfindcircles command to detect one circle in an image, now if I want to use the information of this circle t find a circle in another image, what should I do?
for example if I know the difference between the radius's and the centers.
I really appreciate your time.

18 Jun 2013 David Young

Abdelmoumen, please see my reply dated 30 September 2010.

11 Jun 2013 Abdelmoumen

Hi David, When i excute this code, i get this error:
??? Error: File: circle_houghpeaks.m Line: 113 Column: 11
Expression or statement is incorrect--possibly unbalanced (, {, or [.
Thanks for this code.

24 Apr 2013 David Young

Hi narjes, The call to circle_hough has too many input arguments. Have a close look at it and compare it with the examples in the demo. If you need more help, please post the code that calls circle_hough.

22 Apr 2013 narjes

hi David, when i use this code
some errors appear like :

?? Error using ==> nargchk
Too many input arguments.

Error in ==> C:\MATLAB6p5\work\circle_hough.m
On line 40 ==> error(nargchk(2, 2+length(opts), nargin, 'struct'));
could you help me please??

20 Dec 2012 David Young

Hi Huanhuan. You need to download all the files in the submission and put them in a directory on your MATLAB path. It looks like you didn't download circlepoints.m.

19 Dec 2012 Huanhuan SHEN

Hello, David
when I use this code, some problems come:

Undefined function 'circlepoints' for input arguments of type 'double'.

Error in circle_hough (line 61)
[tR, tC] = circlepoints(rrange(i));

Error in Circle_Detection2 (line 10)
h = circle_hough(e, radii, 'same', 'normalise');

Is that you have any idea about it?

05 Dec 2012 Andri Pranata

thnx david :) i've actually created a code to detect some circle just like yours.. it ran perfectly, yours too.. but i still cant find a hough transform code to detect ellipse. ive search in file exchange, i found only one code. i try to run it, it works, but im not satisfied enough for the result :(

10 Nov 2012 David Young

Hi Vijay. When you look at the image, how do you know which circles are particles and which are not? Can you turn that understanding into code to filter out the circles which you do not want?

07 Nov 2012 vijay mulchandani

hello david,
i want to detect the circular glass particles in the images i get from PIV measuring technique. currently i am using hough transform, but it is showing circles which are not particles. could u help me

19 Oct 2012 David Young

Hi Andri. That's not easy, and the solution depends on what your images look like - for example are the ellipses all very flattened, or are some of them close to being circles? If you have a general set of shapes, you probably need to use some kind of ellipse detector (not necessarily a Hough Transform) to find them. This will detect circles too, but you can then distinguish circles from ellipses simply on the basis of the aspect ration. I'm sorry, but my circle detector will probably not be very useful in the general case.

03 Oct 2012 Andri Pranata

hey i'm andri form indonesia, i need some Hough Transform code that can see the difference between ellipse and circle.. could you help me with that? thank you :)

25 Sep 2012 joy barbosa  
31 May 2012 nourdine  
31 May 2012 nourdine

...

10 May 2012 James

Gracias David Young por este algoritmo, me ayudo mucho para detectar comprimidos en un blister,

09 Feb 2012 Fer  
24 Dec 2011 David Young

Hi Ivan, thanks for the comment. If you set npeaks to 1, you will get the circle with the strongest evidence. If that is not the right circle, then you will need additional code to select the circle you want, using some other selection criteria, such as radius or position.

22 Dec 2011 Ivan

wow, thanks a lot for the code David, helped with my project a lot!

just one question,

for example, if i were to set npeaks to 5, this will result in 5 circles being detected. however, i would only like one of the circle.

hence, is there a parameter i can set to eliminate the other circles? or do i have to add on to your code?

thank you so much!

22 Nov 2011 Nathan Orloff

I tried several of the different Hough Circle detection algorithms. None of them were as easy to use or as robust as this one. It was also very fast in comparison to the editors pick of the week. Even the script circlepoints turns out to be faster than a standard trick in drawing a circle in matlab. My only suggestion is that you be a little nicer to the newbs David... :)

10 Aug 2011 Tristan

Awesome Code David, Thanks Alot.
The patch you just mentioned for older versions of matlab worked perfect for me in R2007b

14 Dec 2010 Arefeh boroomand  
30 Sep 2010 David Young

Hello TaoGe, I think you may be using an old version of Matlab that does not allow the use of ~ for anonymous variables. Please try replacing the ~ in line 113 of the file with a variable name, like this:

[unused, ind] = sort(h(peakind), 'descend');

29 Sep 2010 TaoGe ?

Hello,May Good Job.
But to me,an error found:??? Error: File: circle_houghpeaks.m Line: 113 Column: 11
Expression or statement is incorrect--possibly unbalanced (, {, or [.

How to deal with it?
Thx~

24 Jul 2010 David Young

Mash: Sorry, but I can't help. There could be many different reasons why this technique does not work for your project, and you may need to investigate other methods, as well as experimenting to find good parameter values for the edge detection and the HT itself. You might get more detailed advice by posting a request (with example images) on the newsgroup.

23 Jul 2010 Masih Soltany

Hi

i want to use your code for my project on " eye detection "

i test but it had many miss place circle detection.

how can i optimize your code ?

thanks

09 May 2010 Steve Stefanou  
07 May 2010 Alireza Saberi  
06 May 2010 David Young

Alireza Saberi: As this page shows, the code was built using Matlab 2010a. You should not give a low rating just because you have an old version of Matlab. You can delete calls to validateattributes (though you will lose error checking) and it may then run on your system.

On your second point: this isn't the place to discuss a specific application, and I suggest post the question on the newsgroup.

06 May 2010 Alireza Saberi

Hello David,
It should be nice fast work. Here I have following Qs:
1- by running demo file on my LAB system i can see the following error
??? Undefined command/function 'validateattributes'.

Error in ==> circle_hough at 41
validateattributes(rrange, {'double'}, {'real' 'positive' 'vector'});

Error in ==> circle_houghdemo at 26
h = circle_hough(e, radii, 'same', 'normalise');

2- I have an algorithm for fatigue detection of a driver/pilot
the final steps, I have binarized image around pupil and even if the eye is open, i want and i need a good hough transform detect partials of circle of pupil, Can it do that or circle should be perfect for detection?

06 May 2010 Alireza Saberi  
17 Apr 2010 Truc Phan

Great job! Thanks for sharing

Updates
19 Mar 2010

Added circle_houghpeaks and circle_houghdemo, minor change to circle_hough to allow radius normalisation to be requested.

04 Sep 2014

Fixed bug in options code in circle_hough. Enhanced circlepoints for more generality when used for graphics.

Contact us