clc;
close all;
imtool close all;
clear;
workspace;
fontSize = 14;
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
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
grayImage = imread(fullFileName);
[rows columns numberOfColorBands] = size(grayImage);
subplot(2, 2, 1);
imshow(grayImage, []);
title('Grayscale Image', 'FontSize', fontSize);
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
[pixelCount grayLevels] = imhist(grayImage);
subplot(2, 2, 2);
bar(pixelCount);
title('Histogram of image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]);
grid on;
cdf = cumsum(pixelCount) / numel(grayImage);
subplot(2,2,3);
bar(pixelCount);
hold on;
z = zeros(1,length(pixelCount));
plotyy(grayLevels, z, grayLevels, cdf);
title('Histogram and CDF of image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]);
grid on;
Best Answer