%%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);
这里在减去面部后,找出最大的连接区域。 我想从原始图像裁剪该区域。
答案 0 :(得分:0)
尝试使用此方法裁剪出使用阈值蒙版设计的图像部分。
基本上,代码会根据某个布尔值找到应该提取的图像的一部分 - 我使用的是subPass
,它具有图像的高度和宽度,并且包含应该提取的像素的1和0应该被忽略的像素。
一旦你有了这个布尔矩阵,你就可以用它来设置你想要忽略的像素为黑色(如果你想做的话)。或者,您可以找到形成要提取的目标零件边界的最小和最大索引,然后根据这些索引裁剪图像(我的代码中为I
和J
)。
% 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”)。