1

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).

Elsie
  • 283
  • 2
  • 6
  • 15

1 Answers1

2

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
Amro
  • 123,847
  • 25
  • 243
  • 454
  • Short and strong loop. Actually, i've been quite puzzled by the problem of negative values. Once i voronoi(x,y), surely i have some negative coordinates and inf for [vx,vy], and same goes for the ellipse (the function 'calculateEllipse.m' which was suggested by you). The points i get to make up of an ellipse also include nega valuse. For my work, what i need to do is to finally make them display in image. While for image, it accepts **positive integer** only.Do you have any idea about this? – Elsie Oct 19 '11 at 13:13
  • @Ivy: maybe you could use some sort of [line clipping](http://en.wikipedia.org/wiki/Line_clipping) algorithm. As for ellipses, I already suggested a solution in my [previous answer](http://stackoverflow.com/questions/7721255/axis-coordinates-to-pixel-coordinates-matlab/7763771#7763771) – Amro Oct 20 '11 at 04:44