繁体   English   中英

使用 Matlab 进行手部检测

[英]Hand Detection Using Matlab

%%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);

这里减去人脸后,找出最大的连接区域。 我想从原始图像中裁剪该区域。

尝试使用此方法裁剪出使用阈值蒙版设计的图像部分。

基本上,代码会根据某个布尔值找到应该提取的图像的一部分——我正在使用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

您可以从这里使用此图像(只需下载并重命名为“sample.jpg”)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM