This is the first of several blogs about the algorithm behind poly2mask and roipoly. poly2mask takes a set of polygon vertices and produces a binary mask image. The binary mask has 1s for pixels inside the polygon and 0s for pixels outside.
Here's a quick peek at what it does.
x1 = [40 225 160 40]; y1 = [230 170 45 230]; mask = poly2mask(x1, y1, 300, 300); imshow(mask) hold on plot(x1,y1,'LineWidth',3,'Color','r'); hold off
Earlier versions of the Image Processing Toolbox just had roipoly. It contained both the core algorithm as well as a simple GUI for specifying the polygon vertices with mouse clicks. A few releases ago, we separated the core algorithm into its own function, poly2mask. Now roipoly handles image display and grabbing mouse clicks, and it calls poly2mask for the computation.
You can see that the computation might be nontrivial if you use a more complicated polygon, and if you zoom in a bit so you can see what's going on along the polygon edges. My second example computes a 25-by-25 mask, displays the mask using high magnification, and displays a gray grid showing the edges of the pixels.
x2 = [10 24 5 15.5 10]; y2 = [2 5 15 20.5 2]; M = 25; N = 25; mask = poly2mask(x2,y2,25,25); map = [255 255 255; 218 157 30] / 255; imshow(mask,map,'InitialMagnification',1000) hold on xx = 0.5:1:N+0.5; yy = 0.5:1:M+0.5; for k = 1:numel(xx) plot([xx(k) xx(k)], [0.5 M+0.5], 'Color', [.8 .8 .8]); end for k = 1:numel(yy) plot([0.5 N+0.5], [yy(k) yy(k)], 'Color', [.8 .8 .8]); end plot(x2,y2,'LineWidth',2,'Color',[189 50 21]/255); hold off
The interesting pixels are those which are only partially inside the polygon. How do we decide the mask value for those pixels?
In the computer graphics space, I believe the most common term for this operation is polygon scan conversion. If you do a Web search on this term you'll find lots of links, many of which go to computer graphics course notes.
My own educational background is in electrical engineering, specializing in digital image and signal processing. When I left academia to become a software developer, implementing image processing algorithms, I found that many things I really needed to know were in the computer graphics literature, not in the image processing literature. (That's why the image processing team has Graphics Gems volumes 1-5 on our bookshelf.) That is most certainly the case for polygon scan conversion.
In my next blogs on this topic, I'll explain the desirable properties that a polygon scan conversion algorithm should have, the special cases that tend to trip up simple implementations, and the details of the Image Processing Toolbox implementation.
Get
the MATLAB code
Published with MATLAB® 7.3



Hi Steve,
I think there’s a typo in your code.
In the second example there’re two declarations: x2 and y2
while the following two lines use x and y instead:
mask = poly2mask(x,y,25,25);
plot(x,y,’LineWidth’,2,’Color’,[189 50 21]/255);
Thanks,
-Sagiv.
Sagiv - Thanks, I fixed the problem. I didn’t notice it when I published from MATLAB because I happened to have x and y variables in my workspace with the same values.
Dear Steve,
I have a question smiliar to deciding the mask values along the edge. I hope it’s okay to ask here. How is the motion blur (obtained by fspecial(’motion’,len,theta)) actually defined? I couldn’t find the formula in the Uers’ Guide. Thanks.
Andy
Andy - The output values are computed as the nearest distance between the element center and the idealized line segment. I couldn’t find this anywhere in the doc, so we’ll make a note to add this information in a future release.
Dear Steve,
I am actually looking for an algorithm that does the reverse of this script; a mask2poly so to speak. So, the input is a mask and the output a number of vertices. Would you know if there is any script in Matlab that does this?
Regards,
Olivier.
Olivier - look at bwboundaries and bwtraceboundary in the Image Processing Toolbox.
Steve,
Thanks for the advice, but I think I need a vertex reduction script now. Any advice on that?
Thanks again,
Olivier.
Olivier - I don’t have a polygon vertex reduction function to offer you, but I’ll add it to my list of possible blog topics. Fortunately, for the output of bwboundaries or bwtraceboundary, the vertices all lie on a regular axes-oriented grid, so there are no numerical problems with determining colinearity. I think you might be able to eliminate vertices efficiently in iterations in which you alternate between considering odd-numbered and even-numbered vertices.
Hi Steve,
I have a polygon which i coverted to a mask by using poly2mask command. Now i have to color the mask by using patch, i.e. i want to superimpose a color mask on an image such that I can adjust the transparency of the mask.
In short I would like to know if there is some command to fill specific pixels with a color such that its transparency can be adjusted.
Sridharan—Use your polygon vertices to create a patch object that is superimposed over the image. Then you can adjust the transparency of the patch.
I have one question about the ROIPOLY:
I have an image with stripes, I use the “edge” command for finding the edges, is it possible to use the output of edge as input to ROIPOLY and just extract the information that is included in the edges lines?
MoHDa—It might be possible. You’ll need to use one of the options that produces closed edge contours, or work out some way of closing them yourself.