2017-06-29 81 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) 
相關問題