clc;
close all;
imtool close all;
clear;
workspace;
format long g;
format compact;
fontSize = 16;
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 = pwd;
baseFileName = 'hand palm up.png';
baseFileName = 'hand palm down.png';
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
fullFileName = baseFileName;
if ~exist(fullFileName, 'file')
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
grayImage = rgb2gray(grayImage);
end
subplot(2, 3, 1);
imshow(grayImage);
axis on;
caption = sprintf('Original Image : %s', baseFileName);
title(caption, 'FontSize', fontSize);
impixelinfo;
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0.1, 1, 0.9]);
subplot(2, 3, 2);
imhist(grayImage);
grid on;
title('Histogram of Gray Scale Image', 'FontSize', fontSize);
mask = grayImage > 85;
mask = bwareafilt(mask, 1);
mask = imfill(mask, 'holes');
subplot(2, 3, 3);
imshow(mask);
axis on;
title('Mask Image', 'FontSize', fontSize);
props = regionprops(mask, 'BoundingBox');
xLeft = props.BoundingBox(1)
xRight = xLeft + props.BoundingBox(3)
hold on;
rectangle('Position', props.BoundingBox, 'EdgeColor', 'y', 'LineWidth', 2)
topHalf = mask;
[rows, columns] = size(mask);
yMiddle = floor(rows/2)+1;
yline(yMiddle, 'Color', 'r', 'LineWidth', 2);
topHalf(yMiddle:end, :) = false;
bottomHalf = mask;
bottomHalf(1:yMiddle, :) = false;
subplot(2, 3, 4);
imshow(topHalf);
axis on;
title('Top Half Mask Image', 'FontSize', fontSize);
subplot(2, 3, 5);
imshow(bottomHalf);
axis on;
title('Bottom Half Mask Image', 'FontSize', fontSize);
propsTop = regionprops(topHalf, 'Centroid');
propsBottom = regionprops(bottomHalf, 'Centroid');
subplot(2, 3, 4);
hold on;
plot(propsTop.Centroid(1,1), propsTop.Centroid(1,2), 'r+', 'LineWidth', 2, 'MarkerSize', 20);
subplot(2, 3, 5);
hold on;
plot(propsBottom.Centroid(1,1), propsBottom.Centroid(1,2), 'r+', 'LineWidth', 2, 'MarkerSize', 20);
if propsTop.Centroid(1,1) < propsBottom.Centroid(1,1)
xMiddleFinger = ceil(xLeft);
caption = sprintf('Palm up hand with middle finger at %d pixels', xMiddleFinger);
else
xMiddleFinger = floor(xRight);
caption = sprintf('Palm down hand with middle finger at %d pixels', xMiddleFinger);
end
subplot(2, 3, 6);
imshow(grayImage);
axis on;
title(caption, 'FontSize', fontSize);
hold on;
rectangle('Position', props.BoundingBox, 'EdgeColor', 'y', 'LineWidth', 2)
xline(xMiddleFinger, 'Color', 'r', 'LineWidth', 2);