I have an image that looks like the one below: enter image description here

I'm trying to find the radius (or diameter) of the circle. I have tried using circular Hough transform (via matlab's imfindcircles(bw,[rmin rmax],'ObjectPolarity','bright')) , and by fitting to a circle or an ellipse (home made function that works pretty well for less noisy data, see below).

I've also tried some image processing to get a clearer circle, for example, see below:

se = strel('disk', 2);
bw = imdilate(bw, se);
bw = bwareaopen(bw,100000); 
bw =  edge(bw); 

enter image description here

However, when I feed the processed image to either techniques (Hough and circle\ellipse fitting) neither of them manage to detect the circle in a decent manner.

Here's a code snippet of the circle finder I wrote (matlab) [row col]=find(bw); contour = bwtraceboundary(bw, row(1), col(1)], 'N', connectivity, num_points);

    x = contour(:,2);
    y = contour(:,1);

    % solve for parameters a, b, and c in the least-squares sense by
    % using the backslash operator
    abc = [x y ones(length(x),1)] \ -(x.^2+y.^2);
    a = abc(1); b = abc(2); c = abc(3);

    % calculate the location of the center and the radius
    xc = -a/2;
    yc = -b/2;
    radius  =  sqrt((xc^2+yc^2)-c);

Alternative approaches will be appreciated...

  • $\begingroup$ Hough transform looks for a circle, not a filled disc. you would need to do edge detection first to convert the filled disc to an empty circle. what are the properties of your circles? is the size constant? can they be ellipses? can the dots be distributed differently? $\endgroup$
    – endolith
    Nov 6, 2012 at 22:16
  • $\begingroup$ I tried (see edited example), it is either too noisy or, not circular enough? Additionally, the size is constant, and it may have minute ellipticity (though in reality it is a perfectly circular window) due to camera angle error. $\endgroup$
    – bla
    Nov 6, 2012 at 23:13
  • $\begingroup$ if the size and shape is constant, you might try something like cross-correlation of a filled disc template with the original dot image $\endgroup$
    – endolith
    Nov 7, 2012 at 4:11
  • $\begingroup$ Beside my answer, I think that you might be trying to do this in a too later stage of your image processing pipe. Could you tell us more about the problem, and show some prior steps? $\endgroup$ Nov 7, 2012 at 22:23

2 Answers 2


Here is my solution, it is close to @Yoda's idea, but I changed some steps.

  • Mark all pixels such that there are at least 6 pixels in their 7x7 neighborhood
  • Remove all blobs, but the largest
  • Fill holes
  • Apply edge detection
  • Find circle using Hough transform

enter image description here enter image description here enter image description here enter image description here enter image description here

Here is the relevant Matlab code. I am using Hough transform for circles .m file in my code.

function FindCircle()
    close all;
    im = imread('C:\circle.png');
    im = im(:,:,2);

    ims = conv2(double(im), ones(7,7),'same');
    imbw = ims>6;
    figure;imshow(imbw);title('All pixels that there are at least 6 white pixels in their hood');

    props = regionprops(imbw,'Area','PixelIdxList','MajorAxisLength','MinorAxisLength');
    [~,indexOfMax] = max([props.Area]);
    approximateRadius =  props(indexOfMax).MajorAxisLength/2;

    largestBlobIndexes  = props(indexOfMax).PixelIdxList;
    bw = false(size(im));
    bw(largestBlobIndexes) = 1;
    bw = imfill(bw,'holes');
    figure;imshow(bw);title('Leaving only largest blob and filling holes');
    figure;imshow(edge(bw));title('Edge detection');

    radiuses = round ( (approximateRadius-5):0.5:(approximateRadius+5) );
    h = circle_hough(edge(bw), radiuses,'same');
    [~,maxIndex] = max(h(:));
    [i,j,k] = ind2sub(size(h), maxIndex);
    radius = radiuses(k);
    center.x = j;
    center.y = i;

    figure;imshow(edge(bw));imellipse(gca,[center.x-radius  center.y-radius 2*radius 2*radius]);
    title('Final solution (Shown on edge image)');

    figure;imshow(im);imellipse(gca,[center.x-radius  center.y-radius 2*radius 2*radius]);
    title('Final solution (Shown on initial image)');

  • 1
    $\begingroup$ What is the Hough transform doing here that makes it solve and find the blue circle? Is it projecting many circles of different radii at different positions on the image and finding the one that best fits? $\endgroup$
    – Spacey
    Nov 8, 2012 at 2:26
  • 1
    $\begingroup$ @Mohammad, it is the usual circle detector. It uses binning and voting. $\endgroup$ Nov 8, 2012 at 8:18
  • $\begingroup$ You can also use Fast Radial Symmetry Transform (FRST) after the first step in this answer. $\endgroup$ Oct 21, 2014 at 15:28

It's fairly straightforward to do it using image processing. The following is a proof of concept in Mathematica. You'll have to translate it to MATLAB.

  • First, trim the axes and keep only the image part of it. I call this variable img.
  • Binarize the image and dilate it, followed by a filling transform. I also remove stray small components that are not connected to the main blob. It should give you something like the following:

    filled = Binarize@img ~Dilation~ 3 // FillingTransform // DeleteSmallComponents

  • Next, find the centroid of this blob and the equivalent disk radius of the blob (openCV, MATLAB all have equivalent commands to do this)

    {center, radius} = 1 /. ComponentMeasurements[filled, {"Centroid", "EquivalentDiskRadius"}]
  • That's it! Now plot the original image and a circle with the above center and radius to see how it fits:

    Show[img, Graphics[{Red, Circle[center, radius]}]]

  • $\begingroup$ Awesome answer! Can you please expand on the dilation & filling transform? $\endgroup$
    – Spacey
    Nov 7, 2012 at 3:13
  • $\begingroup$ @Mohammad Dilation is a basic operation and would be easily explained by the wiki article. Filling transform fills in "holes" or in other words, sets of pixels that are surrounded by pixels of higher value. See the "More information" section here $\endgroup$ Nov 7, 2012 at 3:20
  • $\begingroup$ Ah sorry, I mis-typed. I am somewhat familiar with dilation transform, I was actually wondering if you could expand on the 'filling transform'. What rule set it is using exactly? I cant seem to find information related to that. Perhaps it goes by another name? $\endgroup$
    – Spacey
    Nov 7, 2012 at 3:44
  • $\begingroup$ @yoda, thank you for the answer, but if you read the question you'd notice I've tried dilation, and fitting. The image produced before I detect edges is similar to yours. I get some fit, it is not accurate. The same is for your fit, you can see that the top part of the fitted circle is too big, presumably because you take into account the noisy point on the top part above the circle. I have also tried to fit an ellipse (as stated in the question), the problem is that the fit isn't good enough. I think the maybe a better way would be to use the better part of the circle (an arc) to do the fit. $\endgroup$
    – bla
    Nov 7, 2012 at 5:11
  • $\begingroup$ @nate I don't understand what you mean by "top part of the fitted circle" and "better part of the circle". You can use different metrics... bounding box, major axis length, minor axis length, mean distance from centroid, median distance from centroid, etc. It all depends on what you want. $\endgroup$ Nov 7, 2012 at 6:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.