Algorithm Matlab:椭圆voronoi图的算法
是否有任何算法可以实现限制椭圆的Voronoi图?这个图表看起来像这里的图片 任何人都可以分享一些与之相关的链接、教程、代码等吗Algorithm Matlab:椭圆voronoi图的算法,algorithm,math,matlab,ellipse,voronoi,Algorithm,Math,Matlab,Ellipse,Voronoi,是否有任何算法可以实现限制椭圆的Voronoi图?这个图表看起来像这里的图片 任何人都可以分享一些与之相关的链接、教程、代码等吗 提前谢谢。我不知道你说的“省略号”是什么意思。但有一个实现的Voronoi图在C++中由斯蒂芬福斯恩/奥沙利文, 这里有一个算法,它使用和算法一起绘制椭圆的Voronoi图 %# first, define some ellipses (for simplicity, I use 0/90 orientation) ellipses = [10,20,5,10;30
提前谢谢。我不知道你说的“省略号”是什么意思。但有一个实现的Voronoi图在C++中由斯蒂芬福斯恩/奥沙利文,
这里有一个算法,它使用和算法一起绘制椭圆的Voronoi图
%# first, define some ellipses (for simplicity, I use 0/90 orientation)
ellipses = [10,20,5,10;30,10,10,7;40,40,8,3];
%# put the ellipses into an image (few pixels, therefore pixelated)
img = false(50);
[xx,yy]=ndgrid(1:50,1:50);
for e = 1:size(ellipses,1),img = img | (xx-ellipses(e,1)).^2/ellipses(e,3)^2 + (yy-ellipses(e,2)).^2/ellipses(e,4)^2 <= 1;end
以防万一,这是Mathematica帮助系统中的一个示例:
(*Generate ellipses*)
p= Rasterize@Graphics@Table[
Rotate[
Disk[RandomReal[10, 2], (*Rnd position*)
RandomReal[{.3, 1.5}, 2]], (*Rnd radii*)
RandomReal[Pi]], {i, 10}] (*Rnd rotation*)
(*Compute Voronoi*)
LaplacianGaussianFilter[DistanceTransform[p], 2] // ImageAdjust
这不是一个精确的计算,但对于实际应用来说已经足够公平了。根据你最近提出的问题,我知道你一直在研究在RGB图像上绘制椭圆。您希望能够指定椭圆的位置、形状和颜色。您希望椭圆位于边界处,也希望椭圆位于边界处。现在,您希望以类似于Voronoi图的方式绘制分隔空间的线(但使用椭圆代替点) 如图所示,对于这个特殊问题,解决方案是将距离变换与分水岭算法结合使用 我想我继续上一个例子,并用乔纳斯的想法扩展它,以展示整个过程。希望你觉得有用 代码使用该函数计算组成椭圆的点的坐标,以及将图像的指定像素设置为某种选定颜色的函数
%# color image (canvas to draw on)
I = imread('pears.png');
sz = size(I);
%# random ellipses
num = 20;
centers = bsxfun(@times, rand(num,2), sz([2 1])); %# center x/y-coords
radii = bsxfun(@times, rand(num,2), [300 50])+10; %# major/minor axis length
angles = rand(num,1) .* 360; %# angle of rotation
ex = cell(num,1); %# vertices x-coords
ey = cell(num,1); %# vertices y-coords
%# label image, used to hold rasterized ellipses
L = zeros(sz(1),sz(2));
%# randomly place ellipses one-at-a-time, skip if overlaps previous ones
flag = false(num,1);
for i=1:num
%# ellipse we would like to draw directly on image matrix
[ex{i},ey{i}] = calculateEllipse(centers(i,1),centers(i,2), ...
radii(i,1),radii(i,2), angles(i), 100);
%# create mask for image pixels inside the ellipse polygon
mask = poly2mask(ex{i},ey{i}, sz(1),sz(2));
%# check if there is no existing overlapping ellipse
if all( L(mask)==0 )
%# use the mask to place the ellipse in the label image
L(mask) = sum(flag)+1; %# assign value using an increasing counter
flag(i) = true;
end
end
%# filter ellipses to only those that made through the overlap test
num = sum(flag);
centers = centers(flag,:);
radii = radii(flag,:);
angles = angles(flag);
ex = ex(flag);
ey = ey(flag);
%# rasterized voroni diagram of the ellipses [Jonas]
E = (L ~= 0); %# ellipses as binary image
WS = watershed( bwdist(E) ); %# distance transform + watershed
WS = (WS == 0); %# WS==0 corresponds voronoi diagram
WS = bwmorph(WS, 'thicken',1); %# thicken the lines
%# set pixels corresponding to voronoi diagram to white
II = I;
II = imoverlay(II, WS, [1 1 1]); %# you can customize the color here
%# set pixels corresponding to ellipses using specified colors
clr = hsv(num); %# color of each ellipse
for i=1:num
mask = bwperim(L==i,8); %# get perimeter of the ellipse mask
mask = bwmorph(mask, 'thicken',1); %# thicken the ellipse perimeter
II = imoverlay(II, mask, clr(i,:)); %# set those pixels with RGB color
end
%# show final rasterized image (image + ellipses + voronoi diagram)
figure, imshow(II, 'InitialMagnification',100, 'Border','tight')
省略号代替了线所包围的“点”(x,y)。我可以对代码做些什么来控制线和省略号的颜色?@Ivy:
rgb=repmat(ws==0,1,1,3);rgb(:,:,1)=rgb(:,:,1)| img;imshow(rgb)
如何在图像中同时显示椭圆和线条?因为imshow(dt)只显示椭圆,如果imshow(ws)只显示线。我尝试了rgb,“repmat”有一个错误:输入参数太多。我能做什么?@Ivy:对不起,打字错误。它应该是rgb=repmat(ws==0[13])代码>:抱歉,我仍然不知道如何使用“imshow(??)”显示图像。假设背景是黑色,椭圆的颜色是红色,线条的颜色是黄色。你能详细加上一些代码吗?非常感谢。我试着画实心椭圆,所以我拿出了线'mask=bwperim(L==I,8);'这是为了得到椭圆的周长,但输出结果只有一个实心椭圆。为什么会发生这种情况?这是某种逻辑错误吗?@Ivy:如果你想画实心椭圆,你还应该在它后面立即删除线(加厚周长),这样你就只有:mask=(L==i)代码>是,删除两行后,更改'II=imoverlay(II,mask,clr(i,:);'to’II=iOverlay(II,L,clr(i,:);’也会起作用。顺便问一下,如果我的I是“I=0(500500,3)”,如何将背景色从黑色更改为(假设)青色[1 0 1]?我需要再次申请imoverlay吗?或者有一个快速的方法?@Ivy:准确地说,应该是:II=imoverlay(II,L==i,clr(i,:)代码>。现在,如果您从一个黑色图像开始I(:)=0
,您可以使用I=imoverlay(I,true(sz(1),sz(2)),[1 0 1])
将其背景色更改为青色。我无法将头绕在零件周围检查重叠的椭圆,尤其是这一行,如果all(L(mask)==0)。这是什么意思?
(*Generate ellipses*)
p= Rasterize@Graphics@Table[
Rotate[
Disk[RandomReal[10, 2], (*Rnd position*)
RandomReal[{.3, 1.5}, 2]], (*Rnd radii*)
RandomReal[Pi]], {i, 10}] (*Rnd rotation*)
(*Compute Voronoi*)
LaplacianGaussianFilter[DistanceTransform[p], 2] // ImageAdjust
%# color image (canvas to draw on)
I = imread('pears.png');
sz = size(I);
%# random ellipses
num = 20;
centers = bsxfun(@times, rand(num,2), sz([2 1])); %# center x/y-coords
radii = bsxfun(@times, rand(num,2), [300 50])+10; %# major/minor axis length
angles = rand(num,1) .* 360; %# angle of rotation
ex = cell(num,1); %# vertices x-coords
ey = cell(num,1); %# vertices y-coords
%# label image, used to hold rasterized ellipses
L = zeros(sz(1),sz(2));
%# randomly place ellipses one-at-a-time, skip if overlaps previous ones
flag = false(num,1);
for i=1:num
%# ellipse we would like to draw directly on image matrix
[ex{i},ey{i}] = calculateEllipse(centers(i,1),centers(i,2), ...
radii(i,1),radii(i,2), angles(i), 100);
%# create mask for image pixels inside the ellipse polygon
mask = poly2mask(ex{i},ey{i}, sz(1),sz(2));
%# check if there is no existing overlapping ellipse
if all( L(mask)==0 )
%# use the mask to place the ellipse in the label image
L(mask) = sum(flag)+1; %# assign value using an increasing counter
flag(i) = true;
end
end
%# filter ellipses to only those that made through the overlap test
num = sum(flag);
centers = centers(flag,:);
radii = radii(flag,:);
angles = angles(flag);
ex = ex(flag);
ey = ey(flag);
%# rasterized voroni diagram of the ellipses [Jonas]
E = (L ~= 0); %# ellipses as binary image
WS = watershed( bwdist(E) ); %# distance transform + watershed
WS = (WS == 0); %# WS==0 corresponds voronoi diagram
WS = bwmorph(WS, 'thicken',1); %# thicken the lines
%# set pixels corresponding to voronoi diagram to white
II = I;
II = imoverlay(II, WS, [1 1 1]); %# you can customize the color here
%# set pixels corresponding to ellipses using specified colors
clr = hsv(num); %# color of each ellipse
for i=1:num
mask = bwperim(L==i,8); %# get perimeter of the ellipse mask
mask = bwmorph(mask, 'thicken',1); %# thicken the ellipse perimeter
II = imoverlay(II, mask, clr(i,:)); %# set those pixels with RGB color
end
%# show final rasterized image (image + ellipses + voronoi diagram)
figure, imshow(II, 'InitialMagnification',100, 'Border','tight')