clc;
close all;
clear;
workspace;
format longg;
format compact;
fontSize = 15;
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
button = menu('Use which demo image?', 'CameraMan', 'Moon', 'Eight', 'Coins', 'Pout');
if button == 1
baseFileName = 'cameraman.tif';
elseif button == 2
baseFileName = 'moon.tif';
elseif button == 3
baseFileName = 'eight.tif';
elseif button == 4
baseFileName = 'coins.png';
else
baseFileName = 'pout.tif';
end
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = baseFileName;
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, 1);
imshow(grayImage, []);
title('Original 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);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]);
numberOfGrayLevels = 4;
quantizedImage = uint8(mat2gray(grayImage) * (numberOfGrayLevels-1));
subplot(2, 2, 3);
imshow(quantizedImage, []);
caption = sprintf('Image quantized into %d gray levels', numberOfGrayLevels);
title(caption, 'FontSize', fontSize);
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
[pixelCount grayLevels] = imhist(quantizedImage);
subplot(2, 2, 4);
bar(pixelCount);
grid on;
caption = sprintf('Histogram of image quantized into %d gray levels', numberOfGrayLevels);
title(caption, 'FontSize', fontSize);
lastGL = find(pixelCount>0, 1, 'last');
xlim([0 lastGL+1]);
Best Answer