MATLAB: How to detect hand from an image

already accepted and solvedgesturehandImage Processing Toolboxskin

I have to detect hand from an image. First when I tried this code that is given in below link: https://www.mathworks.com/matlabcentral/answers/112917-how-to-detect-hand then it detect my hand with an arm area as shown in given figure. Can you please tell me how can I detect hand from an arm.
Here is the code:
folder=('C:\Users\Tayaba Abro\Desktop');
baseFileName=('hand.jpg');
fullFileName=fullfile(folder,baseFileName);
format long g;
format compact;
fontSize = 20;
%IMAGE SEGMENTATION
img=imread(fullFileName);
img=rgb2ycbcr(img);
for i=1:size(img,1)
for j= 1:size(img,2)
cb = img(i,j,2);
cr = img(i,j,3);
if(~(cr > 132 && cr < 173 && cb > 76 && cb < 126))
img(i,j,1)=235;
img(i,j,2)=128;
img(i,j,3)=128;
end
end
end
img=ycbcr2rgb(img);
subplot(2,2,1);
image1=imshow(img);
axis on;
title('Skin Segmentation', 'FontSize', fontSize);
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
%SEGMENTED IMAGE TO GRAYIMAGE
grayImage=rgb2gray(img);
subplot(2,2,2);
image2=imshow(grayImage);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize);
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
%GRAY TO BINARY IMAGE
binaryImage = grayImage < 245;
subplot(2, 2, 3);
axis on;
image3=imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
% Label the image
labeledImage = bwlabel(binaryImage); % label the connected components in an image and assigning each one a unique label
measurements = regionprops(labeledImage, 'BoundingBox', 'Area');
for k = 1 : length(measurements)
thisBB = measurements(k).BoundingBox;
rectangle('Position', [thisBB(1),thisBB(2),thisBB(3),thisBB(4)],...
'EdgeColor','r','LineWidth',2 )
end
% Let's extract the second biggest blob - that will be the hand.
allAreas = [measurements.Area];
[sortedAreas, sortingIndexes] = sort(allAreas, 'descend');
handIndex = sortingIndexes(2); % The hand is the second biggest, face is biggest.
% Use ismember() to extact the hand from the labeled image.
handImage = ismember(labeledImage, handIndex);
% Now binarize
handImage = handImage > 0;
% Display the image.
subplot(2, 2, 4);
image4=imshow(handImage, []);
title('Hand Image', 'FontSize', fontSize);

Best Answer