How to find center and radius of an arc from binarized image
5 views (last 30 days)
Show older comments
I have image of anulus, I need to find center and radius of inner and outer arc
0 Comments
Answers (1)
Mathieu NOE
on 10 Sep 2024
hello
this is what I can offer you today
you will need the attached function (CircleFitByTaubin.m) , and this Fex submission :
results
also you get the info's in the command window if you prefer
----------------------------
Circle # 1:
Re = 659.0384:
xc = 346.9044:
yc = 674.5627:
----------------------------
----------------------------
Circle # 2:
Re = 392.5529:
xc = 339.0362:
yc = 691.2823:
----------------------------
code :
filename = 'image.png';
%% read png file
% inpict = im2double(rgb2gray(imread(filename))); % for RGB pictures
inpict = imread(filename); % for B&W pictures
[m,n] = size(inpict);
% find values above threshold
[y,x] = find(inpict>0.5);
% flip y direction (on the data, not the plot))
y = m-y;
%% some manual work first
% remove left diagonal segment
ind = (x<300);
x(ind) = [];
y(ind) = [];
% remove top horizontal segment
ind = (y>600);
x(ind) = [];
y(ind) = [];
%% separate both curves
% Run DBSCAN Clustering Algorithm
% see Fex : https://fr.mathworks.com/matlabcentral/fileexchange/52905-dbscan-clustering-algorithm
epsilon=100;
MinPts=5;
X = [x y];
IDX=DBSCAN(X,epsilon,MinPts);
%% Plot Results
k=max(IDX);
Colors=hsv(k);
for i=0:k
Xi=X(IDX==i,:);
if i~=0
Style = 'x';
MarkerSize = 8;
Color = Colors(i,:);
else
Style = 'o';
MarkerSize = 6;
Color = [0 0 0];
end
if ~isempty(Xi)
%%
% circle fit here then plot
%%
par = CircleFitByTaubin(Xi);
% Output: Par = [a b R] is the fitting circle:
% center (a,b) and radius R
xc = par(1);
yc = par(2);
Re = par(3);
% display results in command window
disp(['----------------------------']);
disp([' Circle # ' num2str(i) ':']);
disp([' Re = ' num2str(Re) ':']);
disp([' xc = ' num2str(xc) ':']);
disp([' yc = ' num2str(yc) ':']);
disp(['----------------------------']);
% reconstruct circle from data
n=100;
th = (0:n)/n*2*pi;
xe = Re*cos(th)+xc;
ye = Re*sin(th)+yc;
plot(Xi(:,1),Xi(:,2),Style,'MarkerSize',MarkerSize,'Color',Color)
hold on
plot(xe,ye,'--','Color',Color)
title(' measured fitted circles')
text(xc-Re*0.5,yc + 0.75*Re,sprintf('center (%g , %g ); R=%g',xc,yc,Re))
axis equal
end
end
hold off;
axis equal;
grid on;
title(['DBSCAN Clustering (\epsilon = ' num2str(epsilon) ', MinPts = ' num2str(MinPts) ')']);
2 Comments
Mathieu NOE
on 2 Oct 2024
my pleasure !
ifmy answer has fullfilled your expectations, do you mind accepting it ?
tx
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!