我已经和MATLAB合作了几个星期了。但是我无法获得输入面部图像的精确内边界。
我的代码涉及使用Haar级联来获取面部和鼻子周围的盒子。然后我使用鼻子盒的中点作为鼻尖[nx, ny]
。从那时起,我试图通过以下方式获得面部边界:
[rows, cols]
的面具限定的方框搜索,绘制面部的“活动计数”。这个活动轮廓在脸部周围提供粗略的图像imerode
“侵蚀”图像。接下来是bwboundaries
。我还评论了使用bwmorph
和bwtraceboundaries
。imgradient, imfilter
可能是不必要的,但我正在玩它以了解一切将如何发挥作用。当边界进入面部时,我想到用imdilate
对图像进行透视。我不知道这样做是否是通常的做法,但边界是mehh,但非常丑陋。这是原始图片(没有标记):http://images.wisegeek.com/passport-photo.jpg
这是一个无边框的图像:
支持此功能的代码:
clear all;
%Crop face part from Haar
I = imread('images/photo_1.jpg');
I = imresize(I,0.3);
face_detector = vision.CascadeObjectDetector;
nose_detector = vision.CascadeObjectDetector('Nose');
face_detector.MergeThreshold = 4;
nose_detector.MergeThreshold = 20;
fbox = step(face_detector, I); %holds coords of boxed image
nbox = step(nose_detector, I); %holds coords of boxed image
%find center of nose Haar box
nx = nbox(1) + nbox(3)/2;
ny = nbox(2) + nbox(4)/2;
%out = insertObjectAnnotation(I, 'rectangle',fbox, 'face', 'Color','cyan');
imshow(I);
hold on;
title('Original Image');
%plot tip of nose
plot(nx,ny, 'Marker','+','Color','red','MarkerSize',10);
factor = 20; %number of px before and after Haar boundary
rows = fbox(2)-factor:fbox(2)+fbox(4)+factor;
cols = fbox(1)-factor:fbox(1)+fbox(3)+factor;
%% Plot mask
mask = false(size(I(:,:,1)));
mask(rows, cols) = true;
visboundaries(mask, 'Color','b');
%%
I_gray = rgb2gray(I);
I_contrast = imadjust(I_gray);
f = fspecial('disk',1);
I_filtered = imfilter(I_contrast, f);
[Gmag, Gdir] = imgradient(I_filtered,'prewitt');
bw1 = activecontour(Gmag, mask, 600, 'edge');
se = strel('disk',15);
small_bw1 = imerode(bw1, true(60));
small_bw1 = imdilate(small_bw1,se);
small_boundary = bwboundaries(small_bw1);
% small_bw1 = bwmorph(bw1, 'thin', Inf);
% small_boundary = bwboundaries(small_bw1);
visboundaries(small_bw1,'Color','r');
title('Final is in red');
figure;
imshow(bw1), title('bw1');
%% Get Coordinates of face boundary
figure;
[B, L] = bwboundaries(bw1, 'noholes');
imshow(label2rgb(L, @jet, [.5 .5 .5]));
hold on;
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2);
end;
以下是我正在使用的一些示例图片:
最终目标是在脸部周围绘制完美的边界,但没有头发。只有从他/她的下巴到前额的皮肤部分应该被包围在边界内。任何建议,将不胜感激。
答案 0 :(得分:2)
我将代码扩展为检测面部,如下所示。
CascadeObjectDetector
检测鼻子和眼睛。我将这些区域扩展到分别包括嘴和眉毛。这些区域被迫成为最终面部区域的一部分。 通过对灰度图像的二阶导数进行阈值处理来完成面部边界的检测。特别是下巴很难正确检测。通过应用大的侵蚀步骤突出了下巴和颈部之间的边界。由于此步骤对面部区域的噪声敏感,因此首先通过应用小的扩张和侵蚀步骤非线性地过滤图像。
使用侵蚀图像确定面部区域,否则下巴和颈部之间的边界可能不清楚。然后,通过应用(反向)扩张步骤来消除大的侵蚀。
对于简单的方法,结果非常好,但并不完美。通过迭代地改变用于二阶导数的阈值,可以获得更好的结果。如果从较大的阈值开始,颈部将包含在面部区域中。您可以通过假设鼻子区域和脸部底部之间的最大距离来检测它。然后,您可以降低阈值,直到不再包括颈部。使该方法更稳健的另一种方法可能是规范化LoG。
clear all;
close all;
I = imread('passport-001.jpg');
I = imresize(I,240/size(I, 1)); % resize all the images to the same size
nose_detector = vision.CascadeObjectDetector('Nose');
eye_detector = vision.CascadeObjectDetector('EyePairSmall');
nose_detector.MergeThreshold = 20;
nbox = step(nose_detector, I); % box around the nose
nbox = nbox(1,:); % guess the first box is correct
% extend the box to include the mouth
nbox(1) = nbox(1) - 0.1*nbox(3);
nbox(3) = 1.2*nbox(3);
nbox(4) = 1.5*nbox(4);
ebox = step(eye_detector, I); % box around the eyes
% extend the eye box to include the eyebrows
ebox(2) = ebox(2) - 0.5*ebox(4);
ebox(4) = 1.5*ebox(4);
%find center of nose Haar box
nx = nbox(1) + nbox(3)/2;
ny = nbox(2) + nbox(4)/2;
% plot the original image
figure
subplot(2,3,1);
imshow(I);
hold on;
title('Original Image');
% indicate the nose (with mouth) and eye regions
rectangle('Position',nbox,'EdgeColor', 'r')
rectangle('Position',ebox,'EdgeColor', 'r')
% create a filter for the detected parts of the face (eye, mouth and nose)
maskFilter = uint8(ones(size(I(:,:,1))));
maskFilter(nbox(2):(nbox(2)+nbox(4)), nbox(1):(nbox(1)+nbox(3))) = 0;
maskFilter(ebox(2):(ebox(2)+ebox(4)), ebox(1):(ebox(1)+ebox(3))) = 0;
% convert to grayscale
I_gray = rgb2gray(I);
% filter high frequency noise
I_gray = imfilter(I_gray, fspecial('gaussian', [3,3], 0.5));
% plot the filtered grayscale image
subplot(2,3,2); imshow(I_gray);
title('Gray');
% calculate second order derivatives (laplacian of gaussians)
f = fspecial('log',[5 5], 0.3);
I_filtered = imfilter(I_gray, f);
I_filtered = I_filtered.*maskFilter; % exclude the detected parts of the face
% plot the laplacian of gaussians
subplot(2,3,3); imshow(I_filtered);
title('LoG');
% apply thresshold to LoG
I_bin = I_filtered < 40;
seDiskNoise = strel('disk',1);
seDiskClose = strel('disk',10);
I_bin1 = imerode(imdilate(I_bin,seDiskNoise), seDiskNoise); % remove noise from the face
I_bin2 = imerode(I_bin1,seDiskClose); % close the boundaries of the face
I_bin3 = imdilate(I_bin2,seDiskClose); % reverse the erode (not used for processing)
subplot(2,3,4); imshow(I_bin1); title('LoG > 50');
subplot(2,3,5); imshow(I_bin2); title('eroded');
subplot(2,3,6); imshow(I_bin3); title('dilated');
CC = bwconncomp(I_bin2); % calculate the regions in the binary image
% search the region containing the nose
ni = sub2ind(size(I_gray), round(ny), round(nx));
for i=1:length(CC.PixelIdxList)
if any(CC.PixelIdxList{i}==ni)
iPhase = i;
end
end
% create a mask for the full face
maskFace = zeros(size(I_gray));
maskFace(CC.PixelIdxList{iPhase}) = 1;
% undo the erosion
maskFace = imdilate(maskFace, seDiskClose);
% visualise the face region
subplot(2,3,1);
visboundaries(maskFace,'Color','b');
% remove all the extrusions and inner regions of the face region
seDisk2 = strel('disk',20);
maskFace = imerode(imdilate(maskFace, seDisk2), seDisk2);
% draw the final face region in red
subplot(2,3,1);
visboundaries(maskFace,'Color','r');
title('Final is in red');