How can I fit a circle to a region of pixels?

57 views (last 30 days)
Kelsey Hilton on 7 Oct 2016
Answered: Gianluca Iori on 24 Jul 2018
I have a series of 2D CT slices of trabecular bone. I have binarized them and detected the boundaries of the bone, and now I would like to do a bit more processing. I am trying to fit the largest possible circle into each slice in the black pixel regions. I've included an image so you can see what I mean.
I've tried something like this:
stats = regionprops('table',bw,'Centroid','EquivDiam');
where bw is my binary image without the boundaries drawn in.
Any help would be great! I'm struggling to find other examples which match mine.

Massimo Zanetti on 7 Oct 2016
You can manage by exploiting the "shape measurements" and other properties that function regionprops allows you to use:
Massimo Zanetti on 7 Oct 2016
You are considering "on" pixels the white ones. Try negate the input of bwconncomp. For example instead of using:
C = bwconncomp(BW)
use
C = bwconncomp(~BW)
This way you will consider the black areas.

Bert on 7 Oct 2016
I'm not familiar with this toolbox. But have you tried simply scanning all point? you could do something like this:
% create indexes
ix = repmat([1:size(bw,2)],size(bw,1),1);
iy = repmat([1:size(bw,1)]',1,size(bw,1));
% all in vector
bw = bw(:);
ix = ix(:);
iy = iy(:);
% init loop
center = [];
% indexes of all black points, possibally the center
I = 1:length(bw);
I = I(bw==0);
for i = I
% all distances from thi point to white ones
distances = sqrt( (ix(i)-ix(bw==1)).^2 + (iy(i)-iy(bw==1)).^2 );
% smallest distance
distance = min(distances);
if distance>radius % this point has the biggest minimal distance to a white one yet
center = [iy(i) ix(i)];
end
end

Gianluca Iori on 24 Jul 2018
dear Kelsey, the problem is called "Maximum Inscribed Circle". Solution is provided here: https://de.mathworks.com/matlabcentral/fileexchange/30805-maximum-inscribed-circle-using-distance-transform