clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 15;
baseFileName = 'originalimage.jpg';
folder = pwd
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
fullFileNameOnSearchPath = baseFileName;
if ~exist(fullFileNameOnSearchPath, 'file')
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
[imageRows, imageColumns, numberOfColorChannels] = size(rgbImage);
subplot(3, 3, 1);
imshow(rgbImage, []);
axis on;
caption = sprintf('Original Color Image, %s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
hp = impixelinfo();
rgbImage = rgbImage(29:391, 87:762, :);
hsvImage = rgb2hsv(rgbImage);
sImage = hsvImage(:, :, 2);
subplot(3, 3, 2);
imshow(sImage, []);
axis on;
caption = sprintf('Saturation Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
subplot(3, 3, 3);
histogram(sImage);
grid on;
title('Histogram of Saturation Image', 'FontSize', fontSize);
saturationThreshold = 0.15;
mask = sImage > saturationThreshold;
mask = imfill(mask, 'holes');
mask = bwareafilt(mask, 2);
line([saturationThreshold, saturationThreshold], ylim, 'Color', 'r', 'LineWidth', 3);
subplot(3, 3, 4);
imshow(mask, []);
axis on;
caption = sprintf('Mask Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
labeledImage = bwlabel(mask);
topMask = ismember(labeledImage, 1);
bottomMask = ismember(labeledImage, 2);
subplot(3, 3, 5);
imshow(topMask, []);
axis on;
caption = sprintf('Top Mask Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
subplot(3, 3, 6);
imshow(bottomMask, []);
axis on;
caption = sprintf('Bottom Mask Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
topMaskedRgbImage = bsxfun(@times, rgbImage, cast(topMask, 'like', rgbImage));
bottomMaskedRgbImage = bsxfun(@times, rgbImage, cast(bottomMask, 'like', rgbImage));
subplot(3, 3, 7);
imshow(topMaskedRgbImage, []);
axis on;
caption = sprintf('Top Mask Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
subplot(3, 3, 8);
imshow(bottomMaskedRgbImage, []);
axis on;
caption = sprintf('Bottom Mask Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
Best Answer