如何在矩阵或二进制图像中绘制多边形以使用图像处理功能? [英] how to sketch a polygon in a matrix or binary image in order to use image processing functions?

查看:247
本文介绍了如何在矩阵或二进制图像中绘制多边形以使用图像处理功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个matlab程序,其中我使用多边形(凹面或凸面)。我需要在多边形上使用imdilate或imerode等图像处理功能。为此,我应该将我的多边形转换为图像。我想知道是否有一种方法可以直接在二进制矩阵中绘制多边形(1为前景,0为背景)?

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) ?

目前,我使用'getframe',然后'frame2im'然后'im2bw'的功能就是这样做的。但它的缺点是我无法控制最终图像(=矩阵)的大小(即,将帧转换为图像时图像的大小,以像素为单位),因为matlab不会以像素为单位显示其图形(?)。因此,每当有人在绘图上放大或缩小时,结果矩阵(=图像)就会不同。

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.

我的代码:

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 来计算感兴趣的区域,而不是像在脚本中那样使用补丁绘制它。例如

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)

以上代码的结果bw是这张图片。

And the result, bw, of the above code is this image.

bw http://i44.tinypic.com/6td9v8.jpg

这篇关于如何在矩阵或二进制图像中绘制多边形以使用图像处理功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆