Matlab: How to derive the vertices (coordinates) o

2019-02-27 20:06发布

问题:

I have a created function file in hand which is to draw lines in image,[img]=drawline(point1,point2,color,img). It's used to connect any two points that are inside the image. I'm asked to create the voronoi diagram in image (not using plot function). For the moment, i'm trying to show the lines in image, but i don't know how to get the vertices of the polygon edges.

I've been using some test codes:

x=[50 70 70 30 40 ];% this is just some simple values for testing, 
y=[50 30 90 30 80 ];% in further stage, i plan to use `x=rand(n,1)*200`.
img=zeros(200,200,3);
color=[255 0 0];
[vx,vy]=voronoi(x,y); 

I only know till above, next i think i need to use for loop to line the vertices up. just no idea how to start.And i'm also stuck in how to solve the negative and infinite issues, if i need to display them in image(pixels coordinates).

回答1:

Assuming you have this drawline function that draws lines in images, this is how you loop over the edges of Voronoi diagram of a set of points:

%# set of points and voronoi diagram
X = rand(10,1)*200; Y = rand(10,1)*200;
[vx,vy] = voronoi(X,Y);

%# vertices connecting the edges
p1 = [vx(1,:);vy(1,:)];     % columns are "from" points
p2 = [vx(2,:);vy(2,:)];     % columns are "to" points

%# draw edges on top of image matrix
img = zeros(200,200,3);
clr = [255 0 0];
for i=1:size(vx,2)
    img = drawline(p1(:,i), p2(:,i), clr, img);
end