clc;
close all;
imtool close all;
workspace;
format long g;
format compact;
fontSize = 18;
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
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', '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');
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)]);
binaryImage = im2bw(grayImage, 0.5);
subplot(2, 2, 2);
imshow(binaryImage, []);
numBlackPixelsTotal = sum(~binaryImage(:));
percentTotal = 100 * numBlackPixelsTotal / (rows * columns);
caption = sprintf('Complete Binary Image has %d Black pixels = %.3f %%', ...
numBlackPixelsTotal, percentTotal);
title(caption, 'FontSize', fontSize);
topHalf = binaryImage(1:floor(rows/2),:);
subplot(2, 2, 3);
imshow(topHalf, []);
numBlackPixelsTop = sum(~topHalf(:));
percentTop = 100 * numBlackPixelsTop / numel(topHalf);
caption = sprintf('Top Half of Binary Image has %d Black pixels = %.3f %%', ...
numBlackPixelsTop, percentTop);
title(caption, 'FontSize', fontSize);
bottomHalf = binaryImage(floor(rows/2)+1:end,:);
subplot(2, 2, 4);
imshow(bottomHalf, []);
numBlackPixelsBottom = sum(~bottomHalf(:));
percentBottom = 100 * numBlackPixelsBottom / numel(bottomHalf);
caption = sprintf('Bottom Half of Binary Image has %d Black pixels = %.3f %%', ...
numBlackPixelsBottom, percentBottom);
title(caption, 'FontSize', fontSize);
Best Answer