View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Hough transform for circles

4.5 | 25 ratings Rate this file 152 Downloads (last 30 days) File Size: 187 KB File ID: #26978 Version: 1.2
image thumbnail

Hough transform for circles


David Young (view profile)


16 Mar 2010 (Updated )

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

| Watch this File

File Information

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.


Hough Transform For Circle Detection inspired this file.

This file inspired Automated Counting Of Bacterial Colonies.

Required Products Image Processing Toolbox
MATLAB release MATLAB 9.0 (R2016a)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (55)
04 Jan 2017 Muhammad Billah

Hello David Young,
whenever i am running the code cicular_houghpeck.m it is showing error: ??? Input argument "h" is

Error in ==> circle_houghpeaks at
params = checkargs(h, radii,
i really dont get it why? can you please help me out.

Comment only
23 Dec 2016 Ousama ELIDRISSI

15 Aug 2016 TOAN NGUYEN

It works the best in my application

29 Mar 2016 Krishna Kumar P

Krishna Kumar P (view profile)

Can you suggest some thing to avoid false detection.

26 Aug 2015 Muthu Kumar

02 Jan 2015 Omar Al Okashi

29 Oct 2014 David Young

David Young (view profile)

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 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?

Comment only
24 Oct 2014 YIBO

YIBO (view profile)

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( Still some coins cannot be detected. Is that also happening to you or I did something wrong?

Comment only
04 Sep 2014 David Young

David Young (view profile)

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

Comment only
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

David Young (view profile)

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.

Comment only
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?


04 Aug 2013 David Young

David Young (view profile)

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"?

Comment only
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

Comment only
22 Jul 2013 David Young

David Young (view profile)

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

Comment only
21 Jul 2013 farhat

farhat (view profile)

Why this code cannott work for this image

Comment only
01 Jul 2013 David Young

David Young (view profile)

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.

Comment only
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.

Comment only
22 Jun 2013 David Young

David Young (view profile)

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.

Comment only
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.

Comment only
18 Jun 2013 David Young

David Young (view profile)

Abdelmoumen, please see my reply dated 30 September 2010.

Comment only
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.

Comment only
24 Apr 2013 David Young

David Young (view profile)

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.

Comment only
22 Apr 2013 narjes

narjes (view profile)

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??

Comment only
20 Dec 2012 David Young

David Young (view profile)

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.

Comment only
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?

Comment only
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 :(

Comment only
10 Nov 2012 David Young

David Young (view profile)

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?

Comment only
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

Comment only
19 Oct 2012 David Young

David Young (view profile)

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.

Comment only
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


Comment only
10 May 2012 James

James (view profile)

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

09 Feb 2012 Fer

Fer (view profile)

24 Dec 2011 David Young

David Young (view profile)

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.

Comment only
22 Dec 2011 Ivan

Ivan (view profile)

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

Comment only
14 Dec 2010 Arefeh boroomand

30 Sep 2010 David Young

David Young (view profile)

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');

Comment only
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?

24 Jul 2010 David Young

David Young (view profile)

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.

Comment only
23 Jul 2010 Masih Soltany


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 ?


09 May 2010 Steve Stefanou

07 May 2010 Alireza Saberi

06 May 2010 David Young

David Young (view profile)

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.

Comment only
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?

Comment only
06 May 2010 Alireza Saberi

17 Apr 2010 Truc Phan

Great job! Thanks for sharing

19 Mar 2010 1.1

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

04 Sep 2014 1.2

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

04 Mar 2016 1.2

MathWorks update: Added Live Script.

Contact us