MATLAB: Selection of non-regular ROI and then use only ROI area for segmentation

image processingImage Processing Toolboximage segmentationmasking

I have a large image and there only small area is ROI and that I have to use for segmentation I use
if true
% code
*start
grayImageOrig = imread(fullFileName);
c=[150,181,263,384,443,440,302,183];
r=[273,245,317,361,367,423,401,331];
maskImg=roipoly(grayImageOrig,c,r);
*finished
end
Extract the area and work over that is difficult because of non regular shape. I want to use original image and then like freehand selection I select area and then rest calculation.

Best Answer

Use poly2mask(), not roipoly():
grayImageOrig = imread('concordorthophoto.png');
[rows, columns, numberOfColorChannels] = size(grayImageOrig)
subplot(2,2,1);
imshow(grayImageOrig);
axis on;
% Create the mask
c=[150,181,263,384,443,440,302,183];
r=[273,245,317,361,367,423,401,331];
maskImage = poly2mask(r, c, rows, columns);
subplot(2,2,2);
imshow(maskImage);
% Do the masking
maskedImage = grayImageOrig .* uint8(maskImage);
subplot(2,2,3);
imshow(maskedImage);