简体   繁体   中英

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

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?

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.

I have since found the inpolygon(xi,yi,xv,yv); function which I could combine with a for loop if I knew how to properly call it.

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:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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