我正在开发一个使用多边形(凹面或凸面)的 matlab 程序。我需要在多边形上使用 imdilate 或 imerode 等图像处理功能。为此,我应该将我的多边形转换为图像。我想知道是否有办法直接在二进制矩阵中绘制多边形(1 代表前景,0 代表背景)?
目前,我使用“getframe”,然后使用“frame2im”,然后使用“im2bw”函数来执行此操作。但它的缺点是我无法控制最终图像的大小(=矩阵)(即,将帧转换为图像时图像的大小(以像素为单位),因为 matlab 不以像素为单位显示其图(?)。因此,每次有人在绘图上“放大”或“缩小”时,生成的矩阵(=图像)都会有所不同。
我的代码:
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)