clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 22;
folder = pwd;
baseFileName = 'image.jpeg';
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);
[rows, columns, numberOfColorChannels] = size(rgbImage);
hFig = figure;
subplot(2, 2, 1);
imshow(rgbImage, []);
impixelinfo;
title('Original RGB Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
hFig.WindowState = 'maximized';
drawnow;
grayImage = min(rgbImage, [], 3);
subplot(2, 2, 2);
imshow(grayImage, []);
impixelinfo;
title('Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
hFig.WindowState = 'maximized';
drawnow;
threshold = 240;
mask = grayImage < threshold;
mask = bwareafilt(mask, 1);
mask = imfill(mask, 'holes');
subplot(2, 2, 3);
imshow(mask, []);
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
meanGLR = mean(rgbImage(redChannel > threshold))
meanGLG = mean(rgbImage(greenChannel > threshold))
meanGLB = mean(rgbImage(blueChannel > threshold))
redChannel(~mask) = meanGLR;
greenChannel(~mask) = meanGLG;
blueChannel(~mask) = meanGLB;
rgbImage2 = cat(3, redChannel, greenChannel, blueChannel);
subplot(2, 2, 4);
imshow(rgbImage2, []);
title('Final Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
Best Answer