views:

721

answers:

1

I'm developing a matlab program in which I uses polygons(concave or convex). I need to use image processing functions like imdilate or imerode and etc on the polygons. To this end, I should convert my polygons to image. I wonder whether there is a way to sketch a polygon directly in a binary matrix (1's for foreground and 0's for background) ?

Currently, I use 'getframe', then 'frame2im 'and then 'im2bw' functions to do so. but its drawback is that I have no control on the size of the final image(=matrix)(ie. the size of the image in pixels when convert a frame to image)due to the fact that matlab does not displays its plots in pixels(?). So every time that somebody does 'zoom in' or 'zoom out' on the plot, the resulting matrix(=image) would differ.

my code:

Polygon = [ 15    45    33    30  40 23 ; 9    9    24    15 13 13]';
figure(1); clf; patch(Polygon(:,1),Polygon(:,2),'black');
axis off

%convert the plot to binary image
frame = getframe(gca);
im =frame2im(frame);
level = graythresh(im);
bw = ~im2bw(im,level);

%draw the resulting image
imtool(bw)
%dilate the image
SE = strel('square',5);
bw2 = imdilate(bw,SE);

%draw the dilated image
imtool(bw2)
+3  A: 
Azim
modified-->bw = poly2mask(Polygon(:,1),Polygon(:,2),ImageHeight,ImageWidth);
Kamran
@Kamran: can you explain your comment? what do you mean by modified?
Azim
@Azim: I think he was pointing out a small typo. I fixed it.
gnovice
@gnovice: ah thanks.... me blind ;)
Azim