clc;
close all;
imtool close all;
clear;
workspace;
format longg;
format compact;
fontSize = 22;
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
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);
[pixelCountsR grayLevelsR] = imhist(redChannel, 256);
[pixelCountsG grayLevelsG] = imhist(greenChannel, 256);
[pixelCountsB grayLevelsB] = imhist(blueChannel, 256);
subplot(2, 2, 2);
bar(grayLevelsR, pixelCountsR, 'BarWidth', 1, 'FaceColor', 'r');
grid on;
title('Red Histogram', 'FontSize', fontSize);
subplot(2, 2, 3);
bar(grayLevelsG, pixelCountsG, 'BarWidth', 1, 'FaceColor', 'g');
grid on;
title('Green Histogram', 'FontSize', fontSize);
subplot(2, 2, 4);
bar(grayLevelsB, pixelCountsB, 'BarWidth', 1, 'FaceColor', 'b');
grid on;
title('Blue Histogram', 'FontSize', fontSize);
Best Answer