clc;
clear;
close all;
imtool close all;
workspace;
fontSize = 16;
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'football.jpg';
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]);
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
redMask = redChannel < 40;
greenMask = greenChannel < 40;
blueMask = blueChannel < 40;
redChannel(redMask) = 255;
greenChannel(greenMask) = 0;
blueChannel(blueMask) = 0;
rgbImage = cat(3, redChannel, greenChannel, blueChannel);
subplot(2, 2, 2);
imshow(rgbImage, []);
title('Masked Color Image', 'FontSize', fontSize);
promptMessage = sprintf('Now we will do it for a gray scale image?');
titleBarCaption = 'Continue with gray scale example?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmp(button, 'Cancel')
return;
end
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
fullFileName = baseFileName;
if ~exist(fullFileName, 'file')
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
[rows columns numberOfColorBands] = size(grayImage);
subplot(2, 2, 3);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
redChannel = grayImage;
greenChannel = grayImage;
blueChannel = grayImage;
mask = grayImage < 40;
redChannel(mask) = 255;
greenChannel(mask) = 0;
blueChannel(mask) = 0;
rgbImage = cat(3, redChannel, greenChannel, blueChannel);
subplot(2, 2, 4);
imshow(rgbImage, []);
title('Masked Color Image', 'FontSize', fontSize);
Best Answer