我正在处理图片,以便在包含分布颗粒的图像的任何自由区域中检测和适配最大可能的圆形:
(能够检测出颗粒的位置).
一种方法是定义一个接触三个点组合的圆形,检查圆形是否为空,然后在所有空圆形中找到最大的圆。但是,这会导致组合数量巨大,即 C(n,3)
,其中n
是图像中颗粒的总数。
如果有人能够给我提供任何提示或替代方法,我将不胜感激。
我正在处理图片,以便在包含分布颗粒的图像的任何自由区域中检测和适配最大可能的圆形:
(能够检测出颗粒的位置).
一种方法是定义一个接触三个点组合的圆形,检查圆形是否为空,然后在所有空圆形中找到最大的圆。但是,这会导致组合数量巨大,即 C(n,3)
,其中n
是图像中颗粒的总数。
如果有人能够给我提供任何提示或替代方法,我将不胜感激。
让我们做些数学吧,因为数学总会到达终点!
维基百科:
在数学中,沃罗诺伊图是根据平面上特定子集中的点到距离进行划分的区域。
例如:
rng(1)
x=rand(1,100)*5;
y=rand(1,100)*5;
voronoi(x,y);
这张图的好处在于,如果你注意到,所有蓝色区域的边缘/顶点都与它们周围的点等距。因此,如果我们知道顶点的位置,并计算到最近点的距离,那么我们可以选择具有最大距离的顶点作为圆的中心。
有趣的是,沃罗诺伊区域的边缘也被定义为由德劳内三角剖分生成的三角形的外心。
因此,如果我们计算出该区域的德劳内三角剖分及其外心,则...
dt=delaunayTriangulation([x;y].');
cc=circumcenter(dt); %voronoi edges
计算外心与任何定义三角形的点之间的距离:
for ii=1:size(cc,1)
if cc(ii,1)>0 && cc(ii,1)<5 && cc(ii,2)>0 && cc(ii,2)<5
point=dt.Points(dt.ConnectivityList(ii,1),:); %the first one, or any other (they are the same distance)
distance(ii)=sqrt((cc(ii,1)-point(1)).^2+(cc(ii,2)-point(2)).^2);
end
end
然后我们有了所有可能没有任何点在其中的圆的中心 (cc
) 和半径 (distance
)。 我们只需要最大的那个!
[r,ind]=max(distance); %Tada!
现在让我们绘制图表
hold on
ang=0:0.01:2*pi;
xp=r*cos(ang);
yp=r*sin(ang);
point=cc(ind,:);
voronoi(x,y)
triplot(dt,'color','r','linestyle',':')
plot(point(1)+xp,point(2)+yp,'k');
plot(point(1),point(2),'g.','markersize',20);
注意圆心位于Voronoi图的一个顶点上。
注:此方法将在[0-5],[0-5]区间内找到圆心。您可以轻松修改它以更改此限制。您也可以尝试找到完全适合所需区域内的圆(而不仅仅是中心)。这需要在最后添加一个小额外操作来获取最大值。
if
条件),但是圆可以部分越界。如果需要,很容易修复。 - Ander Biguriif
。也许我漏掉了什么。如果可以的话,明天再试一次。 - Ander Biguriwhile
迭代的次数,以及cnt
的最佳初始值。function [xBest,yBest,R] = q42806059
rng(1)
x=rand(1,100)*5;
y=rand(1,100)*5;
%% Find the approximate region(s) where there exists a point farthest from all the rest:
xExtent = linspace(min(x),max(x),numel(x));
yExtent = linspace(min(y),max(y),numel(y)).';
% Create a grid:
[XX,YY] = meshgrid(xExtent,yExtent);
% Compute pairwise distance from grid points to free points:
D = reshape(min(pdist2([XX(:),YY(:)],[x(:),y(:)]),[],2),size(XX));
% Intermediate plot:
% figure(); plot(x,y,'.k'); hold on; contour(XX,YY,D); axis square; grid on;
% Remove irrelevant candidates:
D(D<prctile(D(:),95)) = NaN;
D(D > xExtent | D > yExtent | D > yExtent(end)-yExtent | D > xExtent(end)-xExtent) = NaN;
%% Keep only the region with the largest distance
L = bwlabel(~isnan(D));
[~,I] = max(table2array(regionprops('table',L,D,'MaxIntensity')));
D(L~=I) = NaN;
% surf(XX,YY,D,'EdgeColor','interp','FaceColor','interp');
%% Iterate until sufficient precision:
xExtent = xExtent(~isnan(min(D,[],1,'omitnan')));
yExtent = yExtent(~isnan(min(D,[],2,'omitnan')));
cnt = 1; % increase or decrease according to the nature of the problem
while true
% Same ideas as above, so no explanations:
xExtent = linspace(xExtent(1),xExtent(end),20);
yExtent = linspace(yExtent(1),yExtent(end),20).';
[XX,YY] = meshgrid(xExtent,yExtent);
D = reshape(min(pdist2([XX(:),YY(:)],[x(:),y(:)]),[],2),size(XX));
D(D<prctile(D(:),95)) = NaN;
I = find(D == max(D(:)));
xBest = XX(I);
yBest = YY(I);
if nanvar(D(:)) < 1E-10 || cnt == 10
R = D(I);
break
end
xExtent = (1+[-1 +1]*10^-cnt)*xBest;
yExtent = (1+[-1 +1]*10^-cnt)*yBest;
cnt = cnt+1;
end
% Finally:
% rectangle('Position',[xBest-R,yBest-R,2*R,2*R],'Curvature',[1 1],'EdgeColor','r');
对于Ander的示例数据,我得到的结果是[x,y,r] = [0.7832, 2.0694, 0.7815]
(与原始结果相同)。执行时间约为Ander解决方案的一半。
从一个点到所有提供点的最大(清晰)距离的轮廓:
考虑到边界距离,仅保留最远5%的点,并仅考虑包含最大距离的区域(表示保留值的曲面片段):
最后:
img = imread('AbmxL.jpg');
%convert the image to a binary image
points = img(:,:,3)<200;
%compute the distance transform of the binary image
dist = bwdist(points);
%find the circle that has maximum radius
radius = max(dist(:));
%find position of the circle
[x y] = find(dist == radius);
imshow(dist,[]);
hold on
plot(y,x,'ro');
bwdist
的结果看起来非常奇怪。 - Ander Biguriimread
中可以使用 URL 吗?例如这样可以正常工作:img = imread('https://i.stack.imgur.com/AbmxL.jpg');
。 - Dev-iL事实上,这个问题可以使用“直接搜索”方法来解决(正如另一个答案所示),这意味着我们可以将其视为一个全局优化问题。有各种方法可以解决这样的问题,每种方法适用于特定情况。出于个人好奇心,我决定使用遗传算法来解决此问题。
一般来说,这样的算法要求我们将解决方案看作是一组“基因”,在某种“适应性函数”的作用下进行“进化”。恰巧,在这个问题中很容易识别基因和适应性函数:
x
、y
、r
。r
(或者最小的-r
,因为该算法要求最小化函数)。r
大于提供点的欧几里得距离的最小值(即,圆包含一个点),则该生物“死亡”。以下是这样一个算法的基本实现(“基本”因为它完全未经过优化,而在此问题中有很大的优化空间无意冒犯)。
function [x,y,r] = q42806059b(cloudOfPoints)
% Problem setup
if nargin == 0
rng(1)
cloudOfPoints = rand(100,2)*5; % equivalent to Ander's initialization.
end
%{
figure(); plot(cloudOfPoints(:,1),cloudOfPoints(:,2),'.w'); hold on; axis square;
set(gca,'Color','k'); plot(0.7832,2.0694,'ro'); plot(0.7832,2.0694,'r*');
%}
nVariables = 3;
options = optimoptions(@ga,'UseVectorized',true,'CreationFcn',@gacreationuniform,...
'PopulationSize',1000);
S = max(cloudOfPoints,[],1); L = min(cloudOfPoints,[],1); % Find geometric bounds:
% In R2017a: use [S,L] = bounds(cloudOfPoints,1);
% Here we also define distance-from-boundary constraints.
g = ga(@(g)vectorized_fitness(g,cloudOfPoints,[L;S]), nVariables,...
[],[], [],[], [L 0],[S min(S-L)], [], options);
x = g(1); y = g(2); r = g(3);
%{
plot(x,y,'ro'); plot(x,y,'r*');
rectangle('Position',[x-r,y-r,2*r,2*r],'Curvature',[1 1],'EdgeColor','r');
%}
function f = vectorized_fitness(genes,pts,extent)
% genes = [x,y,r]
% extent = [Xmin Ymin; Xmax Ymax]
% f, the fitness, is the largest radius.
f = min(pdist2(genes(:,1:2), pts, 'euclidean'), [], 2);
% Instant death if circle contains a point:
f( f < genes(:,3) ) = Inf;
% Instant death if circle is too close to boundary:
f( any( genes(:,3) > genes(:,1:2) - extent(1,:) | ...
genes(:,3) > extent(2,:) - genes(:,1:2), 2) ) = Inf;
% Note: this condition may possibly be specified using the A,b inputs of ga().
f(isfinite(f)) = -genes(isfinite(f),3);
%DEBUG:
%{
scatter(genes(:,1),genes(:,2),10 ,[0, .447, .741] ,'o'); % All
z = ~isfinite(f); scatter(genes(z,1),genes(z,2),30,'r','x'); % Killed
z = isfinite(f); scatter(genes(z,1),genes(z,2),30,'g','h'); % Surviving
[~,I] = sort(f); scatter(genes(I(1:5),1),genes(I(1:5),2),30,'y','p'); % Elite
%}
这里是一张典型运行的47代的“时间流逝”图:
(蓝色点代表当前的一代,红叉代表被“即刻杀死”的生物,绿色六芒星代表“非即刻杀死”的生物,红色圆圈标记着目的地)。
我不擅长图像处理,这只是一个想法:
实现类似高斯滤波器(模糊)的东西,将每个粒子(像素)转换为带有r = image_size的圆形渐变(所有重叠)。这样,您应该可以得到一张图片,其中最白的像素应该是最好的结果。不幸的是,Gimp中的演示失败了,因为极端模糊使点消失了。
或者,您可以增量地通过标记区域内所有相邻像素(例如:r = 4)来扩展所有现有像素,剩下的像素将是相同的结果(与任何像素的距离最大的像素)。