clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 22;
folder = pwd;
baseFileName = 'image.png';
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
grayImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
grayImage = rgb2gray(grayImage);
end
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
hFig = gcf;
hFig.WindowState = 'maximized';
drawnow;
mask = grayImage > 0 & grayImage < 255;
subplot(2, 2, 2);
imshow(mask, []);
impixelinfo;
title('Initial Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
subplot(2, 3, 4);
histogram(grayImage(mask), 0:255);
grid on;
title('Histogram of gray image', 'FontSize', fontSize);
thresh = 255 * graythresh(grayImage(mask))
xline(thresh, 'Color', 'r', 'LineWidth', 2);
brightStuff = grayImage >= thresh & mask;
subplot(2, 3, 5);
imshow(brightStuff, []);
impixelinfo;
title('Bright Pixels in Region', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
subplot(2, 3, 6);
darkStuff = ~brightStuff & mask;
imshow(darkStuff, []);
impixelinfo;
title('Dark Pixels in Region', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
Best Answer