clc;
workspace;
format long g;
format compact;
fontSize = 20;
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
return;
end
end
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
button = menu('Use which demo image?', 'CameraMan', 'Cell', 'Eight', 'Coins', 'Pout');
if button == 1
baseFileName = 'cameraman.tif';
elseif button == 2
baseFileName = 'cell.tif';
elseif button == 3
baseFileName = 'eight.tif';
elseif button == 4
baseFileName = 'coins.png';
else
baseFileName = 'pout.tif';
end
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
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);
if numberOfColorBands > 1
grayImage = grayImage(:, :, 2);
end
subplot(2, 1, 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, 1, 2);
bar(pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]);
[fullHeight, indexOfMax] = max(pixelCount);
halfHeight = fullHeight / 2;
index1 = indexOfMax;
index2 = indexOfMax;
for k = indexOfMax-1 : -1 : grayLevels(1)
if pixelCount(k) < halfHeight
break;
end
index1 = k;
end
for k = indexOfMax+1 : grayLevels(end)
if pixelCount(k) < halfHeight
break;
end
index2 = k;
end
yl = ylim();
line([index1, index1], [yl(1), yl(2)], 'Color', 'r');
line([index2, index2], [yl(1), yl(2)], 'Color', 'r');
message = sprintf('Max index at %d.\nHalf height indexes at %d and %d',...
indexOfMax, index1, index2);
uiwait(helpdlg(message));