即使不使用内置函数 imfindcircles,您也可以完成此任务。我遵循以下方法。使用 find 获取所有非零像素。现在你有了所有的点,我想将它们分类到不同的圈子中,为此我将使用 histogram。我可以在这里修复垃圾箱的数量(即圈数)。一旦我把所有的点分开,我就会沿着一个圆圈分散点......有了这些点,我将沿着这些点拟合一个圆圈。您可以查看以下代码:
load Sample.mat ;
I = Immm ;
[y,x] = find(I) ;
%% Get Bounding box
x0 = min(x) ; x1 = max(x) ;
y0 = min(y) ; y1 = max(y) ;
% length and breadth
L = abs(x1-x0) ;
B = abs(y1-y0) ;
% center bounding box
C = [x0+B/2 y0+L/2] ;
%% Get distances of the points from center of bounding box
data = repmat(C,[length(x),1])-[x,y] ;
dist = sqrt(data(:,1).^2+data(:,2).^2);
%% Classify the points into circles
nbins = 4 ; % number of circles you want
[N,edges,bin] = histcounts(dist,nbins) ;
% plot the classified circle points for check
figure(1)
imshow(I)
hold on
for i = 1:nbins
plot((x(bin==i)),y(bin==i),'.','color',rand(1,3)) ;
end
%% Circle's radii and center
Circ = cell(nbins,1) ;
for i = 1:nbins
[xc1,yc1,R] = circfit(x(bin==i),y(bin==i)) ;
Circ{i} = [xc1 yc1 R] ;
end
figure(2)
imshow(I)
hold on
th = linspace(0,2*pi) ;
for i = 1:nbins
xc = Circ{i}(1)+Circ{i}(3)*cos(th) ;
yc = Circ{i}(2)+Circ{i}(3)*sin(th) ;
plot(xc,yc,'color',rand(1,3),'linewidth',3) ;
end
Circ 是圆的圆心和半径的单元格。您可以从以下链接下载函数 circfit:http://matlab.wikia.com/wiki/FAQ#How_can_I_fit_a_circle_to_a_set_of_XY_data.3F