简体   繁体   English

如何在Matlab中以2D矩阵绘制多边形

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

I have the follow code in matlab which is supposed to draw a polygon on a image (has to be a 2d image, be just a patch). 我在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);

This doesn't work as drawpolygon does not appear to exist in this way any idea how to do this? 这不起作用,因为drawpolygon似乎不以这种方式存在,任何想法如何做到这一点?

Note that the resulting data must be an image of equal size of baseIM and must be an array of doubles (ints can be converted) as this is test data for another algorithm. 请注意,结果数据必须是baseIM大小相等的图像,并且必须是double数组(可以转换int),因为这是另一种算法的测试数据。

I have since found the inpolygon(xi,yi,xv,yv); 从那以后,我发现了多边形(xi,yi,xv,yv); function which I could combine with a for loop if I knew how to properly call it. 如果我知道如何正确调用它,可以将其与for循环结合使用。

If you just need to plot two polygons, you can use the fill function. 如果只需要绘制两个多边形,则可以使用填充功能。

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

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

As an alternative, you can use the patch function: 或者,您可以使用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')

在此处输入图片说明

Edit 编辑

The fill and patch functions allow to add polygons also over an actual image too. 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')

在此处输入图片说明

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM