2017-06-29 171 views
0

我想通过使用convhull()函数来获得手掌的凸包。我正在制作一个只有手掌的图像。首先我将它转换为二进制图像,然后我应用了convhull函数。但它没有给我想要的结果。请在我的代码中找到该错误。这里是我的代码:Convhull()没有给出想要的结果。

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),'*-'); 

下面是我收到的代码的结果:https://ibb.co/gLZ555 但它不是理想的结果。凸包不适合,它应该只包括手掌,而不是自由空间。另外,我得到两个质心而不是一个。为什么是这样? 我使用的输入图像是:https://ibb.co/hk28Q5

我想计算只包含手掌的手掌的凸包,然后想要计算手掌的质心。这将有助于检测手掌形状。

请回答所需输出的解决方案。

回答

0

你的意思是图像应该只包含与白色像素相关的手掌,而不是黑色像素。如果是这样,那么你需要通过考虑'regionprops'提取的最大斑点区域来裁剪图像。然后只应用凸面。

+0

谢谢!有效。 :) – Prachi

0

我刚刚在应用convhull()之前添加了这段代码,它解决了我的问题。

roi = regionprops(BW2, 'Area'); 
BW2 = bwareafilt(BW2,1); 
subplot(3,3,5) 
imshow(BW2) 
相关问题