clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 25;
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
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
referenceGrayImage = imread(fullFileName);
[rows, columns, numberOfColorBands] = size(referenceGrayImage);
if numberOfColorBands > 1
referenceGrayImage = referenceGrayImage(:, :, 2);
end
subplot(2, 2, 1);
imshow(referenceGrayImage, []);
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(referenceGrayImage);
subplot(2, 2, 2);
bar(grayLevels, pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]);
srcFiles = dir(fullfile(folder, '*.jpg'));
Im1 = im2double(referenceGrayImage);
hn1 = imhist(referenceGrayImage)./numel(referenceGrayImage);
for i = 1 : length(srcFiles)
filename = fullfile(folder,srcFiles(i).name)
thisImage = imread(filename);
subplot(2, 2, 3);
imshow(thisImage);
title(filename, 'FontSize', fontSize);
drawnow;
Im2=rgb2gray(thisImage);
Im2 = im2double(Im2);
hn2 = imhist(Im2)./numel(Im2);
f(i) = sum((hn1 - hn2).^2);
f(i)
promptMessage = sprintf('f=%f\nDo you want to Continue processing,\nor Cancel to abort processing?',f(i));
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
break;
end
end
Best Answer