Matlab Convhull()未给出所需的结果。

Matlab Convhull()未给出所需的结果。,matlab,contour,gesture-recognition,convex-hull,centroid,Matlab,Contour,Gesture Recognition,Convex Hull,Centroid,我想用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 = (

我想用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),'*-');
thresh1=0;
阈值2=20;
image=imread('C:\Users\…\1_depth.png');
子地块(3,3,1)
imshow(图片)
图像_bin1=(图像<阈值2);
图像_bin2=(阈值1<图像);
image\u bin=abs(image\u bin2-image\u bin1);
image_bin_filt=medfilt2(image_bin[18,18]);
子地块(3,3,2)
imshow(imcomplete(image_bin_filt));
BW=im2bw(图像过滤器,0.5);
BW=补码(BW);
子地块(3,3,3)
imshow(BW)
标题(“手的二进制图像”);
BW2=bwareaopen(BW,1000);
子地块(3,3,4)
imshow(BW2)
[y,x]=find(BW2);
k=convhull(x,y);
子地块(3,3,5)
imshow(BW2、“初始放大”、“拟合”)
等等
图(x,y,b.)
绘图(x(k),y(k),'r','LineWidth',2)
标题(“凸面外壳对象”);
%找到质心。
labeledImage=bwlabel(BW2);
测量=区域属性(标签图像、“质心”、“边界框”);
%xCentroid=测量值。质心(1);
%质心=测量值。质心(2);
质心=类别(1,测量值。质心);
子地块(3、3、6);
imshow(BW2);
标题(“带质心标记的二值图像”,“FontSize”,12);
等等
绘图(质心(:,1),质心(:,2),“b*”)
%裁剪图像并在新图形中显示。
boundingBox=测量值。boundingBox;
croppedImage=imcrop(BW2,边界框);
%去掉位于图顶部的工具栏和下拉菜单。
%设置(gcf、“工具栏”、“无”、“菜单”、“无”);
子地块(3,3,7)
imshow(cropedimage,[]);
标题(“裁剪图像”、“字体大小”、“12”、“解释器”、“无”);
%再次尝试绘制凸面外壳
CH_objects=bwconvhull(BW);
子地块(3,3,8)
imshow(chu对象);
标题(“凸面外壳对象”);
[r,c]=查找(Chu对象);
CH=convhull(r,c);
子地块(3,3,9)
imshow(CH_对象)
等等
地积(r(CH),c(CH),"*-";;
下面是我得到的代码的结果: 但这并不是预期的结果。凸面外壳是不合适的,它应该只包括手掌而不是自由空间。另外,我得到的是两个质心,而不是一个。为什么会这样? 我使用的输入图像是:

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


请回复所需输出的解决方案

您的意思是图像应该只包含带有白色像素的手掌,而不是黑色像素。如果是这样,那么您需要考虑由“regionprops”提取的最大斑点区域来裁剪图像。然后仅对其应用ConvXhull。

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

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