Search code examples
matlabcontourgesture-recognitionconvex-hullcentroid

Convhull() is not giving desired result.


I want to get the convex hull of hand palm by using convhull() function. I am working on an image which is having only a hand palm in it. First I converted it to a binary image and then I applied convhull function. But it is not giving me the desired result. Kindly find the bug in my code. Here is my code:

thresh1 = 0;
thresh2 = 20;
image = imread('C:\Users\...\1_depth.png');
subplot(3,3,1)
imshow(image)
image_bin1 = (image < thresh2);
image_bin2 = (thresh1 < image);
image_bin = abs(image_bin2- image_bin1);
image_bin_filt = medfilt2(image_bin, [18,18]);
subplot(3,3,2)
imshow(imcomplement(image_bin_filt));

BW = im2bw(image_bin_filt, 0.5);
BW = imcomplement(BW);
subplot(3,3,3)
imshow(BW)
title('Binary Image of Hand');

BW2 = bwareaopen(BW, 1000);
subplot(3,3,4)
imshow(BW2)
[y,x] = find(BW2);
k = convhull(x,y);
subplot(3,3,5)
imshow(BW2,'InitialMagnification', 'fit')
hold on;
plot(x,y, 'b.')
plot(x(k), y(k), 'r', 'LineWidth', 2)
title('Objects Convex Hull');

% Find centroid.
labeledImage = bwlabel(BW2);
measurements = regionprops(labeledImage, 'Centroid', 'BoundingBox');
%xCentroid = measurements.Centroid(1);
% yCentroid = measurements.Centroid(2);
centroids = cat(1, measurements.Centroid);
subplot(3, 3, 6);
imshow(BW2);
title('Binary Image with Centroid Marked', 'FontSize', 12); 
hold on;
plot(centroids(:,1),centroids(:,2), 'b*')

% Crop the image and display it in a new figure.
boundingBox = measurements.BoundingBox;
croppedImage = imcrop(BW2, boundingBox);
% Get rid of tool bar and pulldown menus that are along top of figure.
%set(gcf, 'Toolbar', 'none', 'Menu', 'none');
subplot(3,3,7)
imshow(croppedImage, []);
title('Cropped Image', 'FontSize', 12, 'Interpreter', 'None');

% Again trying to plot the convex hull
CH_objects = bwconvhull(BW);
subplot(3,3,8)
imshow(CH_objects);
title('Objects Convex Hull');

[r,c]=find(CH_objects);
CH=convhull(r,c);
subplot(3,3,9)
imshow(CH_objects)
hold on;
plot(r(CH),c(CH),'*-');

Here is the result of the code I am getting: https://ibb.co/gLZ555 But it is not the desired result. The convex hull is not proper, it should include only palm and not free space. Also, I am getting two centroids instead of one. Why it is so? Input image I used is: https://ibb.co/hk28Q5

I want to calculate convex hull of palm containing only palm in it and then want to calculate centroid of the palm. It will be helpful in detecting palm shape.

Kindly reply with the solution for the desired output.


Solution

  • Do you mean the image should contain only palm which is with white pixles, not the black pixles. If it is so then you need to crop the image by considering largest blob area as extracted by 'regionprops'. Then apply convexhull on that only.