MATLAB: How to generate a binary mask for a patch object super imposed on a surface in MATLAB 7.4 (R2007a)

Image Processing Toolbox

I am creating a patch on the top of an image displayed in an axes. I would like to be able to generate a matrix the size of the image, with ones inside and zeros outside the patch area.

Best Answer

The ROIPOLY function in the Image Processing Toolbox is capable of providing the functionality required. This function may be used with the following syntax:
 
I = imread('<filename>');
mask = roipoly(I); %Generates a mask the same size as the image file with 1's in the area of interest
where filename is the name of your image file.
This displays the image 'I' on the screen and lets you specify the polygon using the mouse. Use left button clicks on the mouse to add vertices to the polygon.
Note that pressing the Backspace or Delete keys removes the previously selected vertex. A double-click adds a final vertex to the selection and then starts the fill; pressing Return finishes the selection without adding a vertex.