MATLAB: Masking out non-roi area

mask roi

I have a digital elevation model as an image file. I created a binary mask of the roi using roipoly but I can't see how to apply it to the orignal image to cut out the area I'm not interested in.
The help pages seem to suggest roifilt2 but when I applied that it didn't cut out the zero areas. Perhaps I'm using the wrong filter as h? It doesn't seem clear what to use as h – I don't want to alter the roi (1s), just cut out the non-roi (0s).
maskeddem1 = roifilt2 (h, DEM, BW);
Thank you

Best Answer

The simplest way is just to index or multiply by the mask of the ROI:
I = imread('cameraman.tif');
R = roipoly(I);
I2 = I;
I2(~R) = 0; %set things not in the mask to zero
Or
I2 = I.*cast(R,class(I)); %multiply
imshow(I2)