File Exchange

image thumbnail

Spherical hough transform for 3D images

version (2.4 MB) by Luke Xie

Luke Xie (view profile)

SphericalHough detects spherical structures from 3D images - determines object center and radius


Updated 22 Oct 2014

View License

SPHERICALHOUGH detects spherical structures from 3D images. Determines object centers and radii and outputs image mask of centers and spheres. This hough transform is based on the gradient field of the image. The code is heavily based off of Circular Hough Transform by Tao Peng (see info below).



Run Example.m to see demonstration of SphericalHough on 3D MRI data of a rat kidney. It demonstrates the detection of kidney glomeruli, which are roughly spherical at 31x31x31 um^3 resolution. The expected radii are between 2 to 7 pixels. Example data is from the following article: MRH of age-related nephropathy, Tox Path, 2012 Jul;40(5):764-78 (
Supplemental material is also available on CIVMspace:
Essential support functions are included for the example. Complete support functions can be found at the websites below:

2007_03_07 original Circular Hough Transform by Yao Peng
2010_08_25 Spherical Hough Transform by Brian Hulette
2014_10_20 simpification, filtration, image mapping, classifiers, and example by Luke Xie

Comments and Ratings (16)


Is it possible to extend this routine to detect other shapes, like cylinders in a 3D image?


sally (view profile)

does this algorithm require the object being a perfect square? or just sphere like shape even if a bit distorted or connected to another tabular structure (like lung nodules in 3d CT scans) ? also, dows it require a binary image or gray-scale image, or both can be handled?

GUO bill



Thank you very much.
I replaced the code but now I get the following:

Index exceeds matrix dimensions.

Error in SphericalHough (line486)

Can you help me?
Thanks a lot!
I am facing with the same problem.


Thank you for the quick response.
I replaced the code but now I get the following:

Index exceeds matrix dimensions.

Error in SphericalHough (line486)

Can you help me again?
Thanks a lot!

Luke Xie

Luke Xie (view profile)

Replace that line with the following. I hope to update the code later.

% NEW TO FIX "out of range subscript" prob due to x,y,z 0 or above
% d1,d2,d3
y=round(y); x=round(x); z=round(z);
y(y>d1)=d1; x(x>d2)=d2; z(z>d3)=d3;
y(y<1)=1; x(x<1)=1; z(z<1)=1;
% ==========


Hello Luke Xie,

thank you for your code.
I try to detect spherical grains in an image (3D grayscale). By using the code I get the following error:

Error in using sub2ind (line 73)
Out of range subscripts.
Error in SphericalHough (line489)

Do you know what's wrong?
Thanks for your help!


Carl (view profile)

Hello Lukie Xie,

Just one more question. Suppose a sphere is successfully constructed from the function, the radius and center of the sphere ared estimated, are we able to obtain the list of 3d points corrsponding to the consturction of the sphere?

It is because I would liek to know how good my sphereis when compare to my points.

The most obviously method would be to take the average of sum of different between each point and the contructed sphere (suppose I take the shortest distance between the 3d Points the the spherical surface)

Luke Xie

Luke Xie (view profile)

Hello Carl,
The code is designed for detecting spheres in image space. You will have to place those 3D coordinates into discrete matrix form. If you look at the end of SphericalHough.m you will find how to place coordinate points into a 3D matrix.


Carl (view profile)

Hello Luke Xie,

If I have a list of 3d Point, I would like to construct and detect the sphere. Will this algorithm you suggested here also works?

Luke Xie

Luke Xie (view profile)

Hello Niamul,
Thank you for your comments. The code does need the range of radii as an input. The general guideline is to set a small range for faster computing but a large enough range to cover expected radii. Please post what error you received.

Niamul Quader

Hello Xie,

Thank you for this nice implementation.

I tried your example and ran into some difficulties. In particular, whenever I change the radius range to other values (e.g. 22 and 27), the code results in error notifications. Could you check how I could solve this? Is there a guideline onto how I should chose the range of radius?


Luke Xie

Luke Xie (view profile)

Hello Oli,
Parts of the code can be used to identify the centroid of objects. Determining the radii would be much more difficult. You may want to treat your problem as individual 2D slices and use a circular hough transform:


Oli (view profile)


Would this be able to cope with anisotropic resolution in the images? Typically 3D microscopy data has a higher resoltion in the lateral directions (XY) than in the axial direction (Z) in the range of 2-6 fold.

We can interpolate the data to make it isotropic, but this increases the image size considerably.

Thanks for your time


added more input validation, changed gradient threshold input to a ratio, inverted object center intensity threshold

MATLAB Release Compatibility
Created with R2012b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.

Learn About Live Editor