我正在开发一个使用多边形(凹面或凸面)的matlab程序。我需要在多边形上使用图像处理功能,如imdilate或imerode等。为此,我应该将多边形转换为图像。我想知道是否有一种方法可以直接在二进制矩阵中绘制多边形(1表示前景,0表示背景)?
目前,我使用'getframe',然后是'frame2im‘,然后是'im2bw’函数。但它的缺点是我无法控制最终图像的大小(=矩阵)(即。将帧转换为图像时以像素为单位的图像大小),这是由于matlab不以像素(?)显示其绘图的事实。因此,每当有人在图上“放大”或“缩小”时,得到的矩阵(=image)就会不同。
我的代码:
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)
发布于 2009-04-24 16:27:00
也许您可以使用poly2mask
来计算感兴趣的区域,而不是像脚本中那样使用patch来绘制它。例如
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)
上面代码的结果bw就是这个图像。
https://stackoverflow.com/questions/786375
复制相似问题