clc;
close all;
clearvars;
workspace;
fontSize = 20;
format compact;
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
fullFileName = fullfile(folder, baseFileName);
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
fullFileName = baseFileName;
if ~exist(fullFileName, 'file')
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
[rows columns numberOfColorBands] = size(rgbImage);
subplot(2, 2, 1);
imshow(rgbImage, []);
title('Original Color Image', 'FontSize', fontSize);
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
mask = zeros(rows, columns, 'uint8');
mask(30:300, 50:150) = 1;
mask = mask / 255;
subplot(2, 2, 2);
imshow(mask, []);
title('Mask', 'FontSize', fontSize);
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask,class(rgbImage)));
subplot(2, 2, 3);
imshow(maskedRgbImage, []);
title('MaskedImage', 'FontSize', fontSize);
Best Answer