voronoi图matlab实现,Matlab:椭圆的voronoi图算法

根据您最近的问题跟踪,我了解到您一直致力于在RGB图像上绘制rasterized椭圆 . 您希望能够指定椭圆的位置,形状和颜色 . 你希望椭圆在边界处是clipped,也是non-overlapping . 现在,您希望以类似于Voronoi图的方式绘制划分空间的线(但使用省略号而不是点) .

对于这个特殊的问题,如@Jonas所示,解决方案是使用距离变换和分水岭算法 .

我想我继续我之前的例子,并用Jonas的想法扩展它,以展示整个过程 . 希望你觉得它有用..

该代码使用calculateEllipse函数计算构成椭圆的点的坐标,以及imoverlay函数,用于将图像的指定像素设置为某种选定的颜色 .

%# 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')

bec22bdf-686e-40f1-90b2-4ea02e22a08c.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值