how to sketch a polygon in a matrix or binary image in order to use image processing functions?

微笑、不失礼 提交于 2019-12-10 12:56:21

问题


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)

回答1:


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.



来源:https://stackoverflow.com/questions/786375/how-to-sketch-a-polygon-in-a-matrix-or-binary-image-in-order-to-use-image-proces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!