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)
Perhaps you could use poly2mask
to calculate a region of interest instead of plotting it using patch as in your script. For example
Polygon = [ 15 45 33 30 40 23 ; 9 9 24 15 13 13]';
ImageWidth = 100;
ImageHeight = 50;
bw = poly2mask(Polygon(:,1),Polygon(:,2),ImageHeight,ImageWidth);
imshow(bw)
And the result, bw, of the above code is this image.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With