# How to detect, using matlab, the center of the biggest circle?

The goal is to preprocess an image in a way that outside the circle isn't in the region-of-interest so set its value to 0 (black). That way the followed step of segmentation will be easier to analyze all the blobs that exists inside that circle.

My instance isn't this one in particular, but this example is somewhat close enough

I was thinking of applying an X across the whole picture, and detecting the outer most intersection of the the X and O, then from there calculate the center, assuming there's no noise on the edges of the rectangle in which the circle is in.

OR is there a way in Matlab that can already assist me with this?

• Could you clarify this question? What exactly do you want to achieve? Commented Mar 14, 2014 at 9:25
• @tbirdal edited Commented Mar 14, 2014 at 11:06

Would the built-in function imfindcircles work for you? The radius and center are both returned. The examples are included in the same page. The function is based on Hough transform so it might be an overkill.

Another much faster method is regionprops. You may use this function only after binarizing the image first, and that means there must be a contrast difference between the region inside and outside the circle. You may try something like this (copied from this post):

st = regionprops( ~BW, 'Area', 'Centroid', 'PixelIdxList' );

sel = [st.Area] > numel(BW)*0.5; % exclude the small regions
st = st(sel);

cntr = .5 * [size(BW,2) size(BW,1)]; % Compute region distance to center of image
d = sqrt( sum( bsxfun(@minus,vertcat( st.Centroid ), cntr ).^2, 2 ) );

[mn idx] = min(d); % pick the region closest to center

res = false(size(BW));
res( st(idx).PixelIdxList ) = true; % the region outside the circle is
set to false, thus excluded


Note that in the code above, cntr = .5 * [size(BW,2) size(BW,1)]; assumes the circle is in the center of the whole image. You need to estimate the rough position of the circle in your case as a prior knowledge and change this line.

Several assumptions mentioned above make the regionprops option more tricky than imfindcircles.

Ok, I still do not get the "applying an X" part, but will still answer :

There is a circle generation algorithm called Breshenham's Circle Algorithm : http://en.wikipedia.org/wiki/Midpoint_circle_algorithm. This would generate you a circle with integer coordinates.

If you are to go with floating point, there is also a single pass algorithm:

• Start traversing the image
• At each pixel compute the radius to the desired circle: $sqrt(d_x^2+d_y^2)$ where $d_x$ and $d_y$ are the differences of current pixel from the center of the circle. If this value is smaller than your circle you are within the circle and can assign this pixel to 1.