簡體   English   中英

如何在Matlab中以2D矩陣繪制多邊形

[英]How to draw a polygon in matlab in a 2D matrix

我在matlab中有以下代碼,該代碼應該在圖像上繪制多邊形(必須是2d圖像,只是一個補丁)。

numCorners=8;
dotPos=[];
for rr=1:numCorners
   dotPos(end+1)=(cos(rr/numCorners*2*pi))*100;
   dotPos(end+1)=(sin(rr/numCorners*2*pi))*100;
end


BaseIm=zeros(1000,1000);
dotpos=[500,500];
imageMatrix =drawpolygon(BaseIm, dotPos, 1); or how else do draw a white polygon here?
imshow(imageMatrix);

這不起作用,因為drawpolygon似乎不以這種方式存在,任何想法如何做到這一點?

請注意,結果數據必須是baseIM大小相等的圖像,並且必須是double數組(可以轉換int),因為這是另一種算法的測試數據。

從那以后,我發現了多邊形(xi,yi,xv,yv); 如果我知道如何正確調用它,可以將其與for循環結合使用。

如果只需要繪制兩個多邊形,則可以使用填充功能。

t=0:2*pi;
x=cos(t)*2;
y=sin(t)*2

fill(x,y,'r')
hold on
fill(x/2,y/2,'g')

或者,您可以使用patch函數:

figure
t=0:2*pi;
x=cos(t)*2;
y=sin(t)*2

patch(x,y,'c')
hold on
patch(x/2,y/2,'k')

在此處輸入圖片說明

編輯

fillpatch功能還允許在實際圖像上添加多邊形。

% Load an image on the axes
imshow('Jupiter_New_Horizons.jpg')
hold on
% Get the axis limits (just to center the polygons
x_lim=get(gca,'xlim')
y_lim=get(gca,'ylim')
% Create the polygon's coords
t=0:2*pi;
x=cos(t)*50+x_lim(2)/2;
y=sin(t)*50+y_lim(2)/2
% Add the two polygons to the image
f1_h=fill(x,y,'r')
hold on
f1_h=fill(x/2,y/2,'g')

在此處輸入圖片說明

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM