繁体   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