2011-10-19 120 views
1

我有一個創建的函數文件,它是在圖像中繪製線條,[img]=drawline(point1,point2,color,img)。它用於連接圖像內部的任意兩點。我被要求在圖像中創建voronoi圖(不使用繪圖功能)。目前,我試圖在圖像中顯示線條,但我不知道如何獲取多邊形邊的頂點。Matlab:如何導出voronoi圖中多邊形的頂點(座標)?

我已經使用了一些測試代碼:

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

我只知道,直到上面,接下來我想我需要使用for loop排隊的頂點了。只是不知道如何開始。而且我還陷入如何解決負面和無限的問題,如果我需要在圖像(像素座標)中顯示它們。

回答

2

假設你有這個drawline功能,吸引了圖像中的線條,你這是怎麼遍歷一個點集的Voronoi圖的邊緣:

%# 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 
+0

短而結實循環。其實,我一直對負值問題感到困惑。一旦我voronoi(x,y),當然我有一些負面的座標和inf [vx,vy],同樣的橢圓(函數'calculateEllipse.m'這是你所建議的)。我得到的要點包括橢圓也包括nega valuse。對於我的工作,我需要做的是最終讓他們在圖像中顯示。而對於圖像,它只接受**正整數**。您對此有任何想法嗎? – Elsie

+0

@Ivy:也許你可以使用某種[line clipping](http://en.wikipedia.org/wiki/Line_clipping)算法。至於橢圓,我已經在我的[先前的答案]中建議了一個解決方案(http://stackoverflow.com/questions/7721255/axis-coordinates-to-pixel-coordinates-matlab/7763771#7763771) – Amro