2015-10-17 62 views
-4
%%skin detection 
[hue,s,v]=rgb2hsv(I); 
cb = 0.148* I(:,:,1) - 0.291* I(:,:,2) + 0.439 * I(:,:,3) + 128; 
cr = 0.439 * I(:,:,1) - 0.368 * I(:,:,2) -0.071 * I(:,:,3) + 128; 
[w h]=size(I(:,:,1)); 
for i=1:w 
    for j=1:h  
     if 128<=cr(i,j) && cr(i,j)<=165 && 140<=cb(i,j) && cb(i,j)<=195 && 0.01<=hue(i,j) && hue(i,j)<=0.1 
      segment(i,j)=1; 
     else  
      segment(i,j)=0; 
     end  
    end 
end 
im(:,:,1)=I(:,:,1).*segment; 
im(:,:,2)=I(:,:,2).*segment; 
im(:,:,3)=I(:,:,3).*segment; 
%imshow(uint8(im)); 
title('My Edge Detection') 
im1 = imclearborder(im2bw(im)); 
    figure 
imshow(im1) 
im_fill = imfill(im1, 'holes'); 
figure 
imshow(im_fill) 
s = regionprops(im_fill, 'Area', 'PixelList'); 
[~,ind] = max([s.Area]); 
pix = sub2ind(size(im), s(ind).PixelList(:,2), s(ind).PixelList(:,1)); 
out = zeros(size(im)); 
out(pix) = im(pix); 
imshow(out); 

這裏減去臉部後,找出最大的連接區域。 我想從原始圖像裁剪該區域。使用Matlab手部檢測

+2

請擴大您的問題的更多信息和/或在您的代碼中添加評論,以便我們能夠更好地爲您提供幫助。 –

+0

@BrianLynch 在通過中提琴瓊斯減去臉部之後的第一部分中,然後我轉換爲Hue以找出膚色,然後找出二值圖像(手)中最大的連接區域。 現在我怎樣才能從原始輸入圖像裁剪連接區域區域。 –

+0

您是否問如何拍攝原始圖像並將連接區域中的所有_not_像素都變成黑色? –

回答

0

嘗試使用這種方法來裁剪用閾值蒙版設計的圖像部分。

基本上,代碼找到應該基於某個布爾值提取的圖像的一部分 - 我使用的是subPass,它具有圖像的高度和寬度,並且包含1應該被提取的像素,0代表像素那應該被忽略。

一旦你有了這個布爾矩陣,你就可以用它來設置你想忽略的像素爲黑色(如果這是你想要做的)。或者,您可以找到圍繞要提取的目標部分形成邊界的最小和最大索引,然後根據這些索引(我的代碼中的IJ)裁剪圖像。

% load image 
img = double(imread('sample.jpg'))/255; 

% find some particular region... say all pixels that are nearly white 
% this will be a matrix with 1's where the pixel passes the test 
subPass = double((img(:,:,1) >= 0.95).*(img(:,:,2) >= 0.95).*(img(:,:,3) > 0.95)); 

% set all the pixels not in the target region to be black 
imgNew = img; 
imgNew(:,:,1) = img(:,:,1).*subPass; 
imgNew(:,:,2) = img(:,:,2).*subPass; 
imgNew(:,:,3) = img(:,:,3).*subPass; 

% plot results 
figure 
image([img imgNew]); 
axis equal; 
axis tight 

% find indices of target region 
[I,J] = find(subPass == 1); 

% crop target region based on min/max indices 
imgNewCrop = imgNew(min(I):max(I),min(J):max(J),:); 

% plot cropped image 
figure 
image(imgNewCrop); 
axis equal 
axis tight 

您可以使用此圖片來自here(只需下載並重新命名爲 「sample.jpg」)。