Extracting "endpoints" from a skeleton image to enable a circle to be defined.
Show older comments
Hi, I am trying to find the centre and radius of the circle (whilst ignoring the cross) so I can locate the centre and display a circle on the image:

After reading another matlab central thread, IA has suggested skeletonising and finding the endpoints, so I am trying this approach
So after binarising my raw tiff image and filling in the holes, I have created the skeleton.
But Im not sure how to extract say the 8 most extreme endpoints and use this to define a circle (or fit a circle to)

figure
subplot(1,4,1)
myImshow(IM,2) %IM is my raw image
title(['Raw Image'])
subplot(1,4,2)
imshow(BI,[0 1]) %BI is my binarised image
title(['Binary: Thresh=',num2str(level,'%.1f')])
%Fill holes
subplot(1,4,3)
bw = imfill(BI, 'holes');
imshow(bw,[0 1])
title(['Fill Holes'])
bw3 = bwmorph(bw2,'skel',Inf);
imshow(bw3,[0 1]); hold on
title(['Skeletonise'])
ep=bwmorph(bw2,'endpoints');
What to do with ep?
I have included the fill holes image incase its useful.
Thanks
Jason
Accepted Answer
More Answers (1)
Rik
on 22 May 2020
This can probably be improved, but this will find an approximate circle. It will slightly underestimate the radius, as it tries to maximize the number of correctly labeled pixels. You may find the perfomance improves if you manage to fill in that crosshair.
Watch out for the differences in convention for the orientation between image space and plotting space: y is flipped. The code below works also if you extend your masked image in one direction, so I'm reasonably confident I didn't make a mistake here.
%convert image back to binary
mask=imread('im154.png');mask=mask(:,:,1)>128;
[x,y,r]=fit_circle(mask);
t=linspace(0,2*pi,200);
x_c=r*cos(t)+x;
y_c=r*sin(t)+y;
figure(1),clf(1)
imshow(mask),hold on
plot(x_c,y_c,'r-*')
plot(x,y,'ro')
function [x,y,r]=fit_circle(mask)
[Y,X]=ndgrid(1:size(mask,1),1:size(mask,2));
count_of_correct_pixels=@(y,x,r) sum(sum( (sqrt((Y-y).^2+(X-x).^2)<=r) == mask));
%initialize as centered
y=size(mask,1)/2;x=size(mask,2)/2;
r=mean([y x]);
p_fitted=fminsearch(@(p) -count_of_correct_pixels(p(1),p(2),p(3)),[y,x,r]);
y=p_fitted(1);
x=p_fitted(2);
r=p_fitted(3);
end
Categories
Find more on Region and Image Properties in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!