clc;
close all;
clearvars;
workspace;
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
folder = pwd;
baseFileName = 'ships.jpeg';
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
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Original Color Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
hFig = gcf;
hFig.Units = 'Normalized';
hFig.WindowState = 'maximized';
hFig.Name = 'Demo by Image Analyst';
hFig.NumberTitle = 'Off'
uiwait(msgbox('Draw the guard region'));
guardRect = drawrectangle('Color', 'green');
uiwait(msgbox('Draw the P region'));
pRect = drawrectangle('Color', 'yellow');
gRect = guardRect.Position;
pRect = pRect.Position;
subplot(4, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Original Color Image\n"%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
rectangle('Position', gRect, 'EdgeColor', 'g', 'LineWidth', 2);
rectangle('Position', pRect, 'EdgeColor', 'y', 'LineWidth', 2);
grayImage = rgb2gray(rgbImage);
subplot(4, 2, 2);
imshow(grayImage, []);
axis('on', 'image');
caption = sprintf('Gray Scale Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
rectangle('Position', gRect, 'EdgeColor', 'g', 'LineWidth', 2);
rectangle('Position', pRect, 'EdgeColor', 'y', 'LineWidth', 2);
gRect = int32(gRect);
guardMask = true(rows, columns);
colg1 = gRect(1);
colg2 = gRect(1) + gRect(3);
rowg1 = gRect(2);
rowg2 = gRect(2) + gRect(4);
guardMask(rowg1:rowg2, colg1:colg2) = false;
gMean = mean(grayImage(guardMask))
gStd = std(double(grayImage(guardMask)))
subplot(4, 2, 3);
imshow(guardMask, []);
axis('on', 'image');
caption = sprintf('Guard Mask Image, Mean = %.2f, StDev = %.2f', gMean, gStd);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
pRect = int32(pRect);
pMask = false(rows, columns);
colp1 = pRect(1);
colp2 = pRect(1) + pRect(3);
rowp1 = pRect(2);
rowp2 = pRect(2) + pRect(4);
pMask(rowp1:rowp2, colp1:colp2) = true;
pMean = mean(grayImage(pMask))
pStd = std(double(grayImage(pMask)))
subplot(4, 2, 4);
imshow(pMask, []);
axis('on', 'image');
caption = sprintf('P Mask Image, Mean = %.2f, StDev = %.2f', pMean, pStd);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
P = double(grayImage(rowp1:rowp2, colp1:colp2));
subplot(4, 2, 5);
imshow(P, []);
impixelinfo;
axis('on', 'image');
caption = sprintf('P Image, Mean = %.2f, StDev = %.2f', pMean, pStd);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
ratio = abs(P - gMean) ./ gStd;
subplot(4, 2, 6);
imshow(ratio, []);
impixelinfo;
axis('on', 'image');
caption = sprintf('Ratio Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
kThreshold = 1.01;
shipMask = ratio > kThreshold;
subplot(4, 2, 7);
imshow(shipMask, []);
impixelinfo;
axis('on', 'image');
caption = sprintf('Ship Mask Image with Threshold = %.2f', kThreshold);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
fprintf('Done running %s.m\nThank you Image Analyst!', mfilename);