gpt4 book ai didi

matlab - 如何在矩阵或二进制图像中绘制多边形以便使用图像处理功能?

转载 作者:太空宇宙 更新时间:2023-11-03 19:27:35 24 4
gpt4 key购买 nike

我正在开发一个使用多边形(凹面或凸面)的 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)

最佳答案

也许您可以使用 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 就是这张图片。

bw

关于matlab - 如何在矩阵或二进制图像中绘制多边形以便使用图像处理功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/786375/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com