Monday, October 28, 2013

FACE DETECTION USING MATLAB

Face Detection:



In this tutorial, I present a face recognition system that attempts to recognize faces using the Skin Segmentation Technique. This tutorial is intended to provide an insight into developing a face recognition system using Skin Detection and hopefully gives a good starting point for those who are interested in developing a face recognition system.


There are other methods of face detection in an image, but i structured my own format and it works very well.


My Method:


1. Histogram Equalization
2. Skin Detection and Segmentation
3. Filling The Holes
4. Eliminating Pixels Below a Threshold
5. Putting Bounding Boxes Around Detected Faces And Counting
Step 1:


Brightness Preserving Dynamic Histogram Equalization:


Code:
close all;
clear all;
clc;
rgbInputImage = imread('Your File Here');
%rgbInputImage=getsnapshot(rgbInputImage);
labInputImage = applycform(rgbInputImage,makecform('srgb2lab'));
Lbpdfhe = fcnBPDFHE(labInputImage(:,:,1));
labOutputImage = cat(3,Lbpdfhe,labInputImage(:,:,2),labInputImage(:,:,3));
rgbOutputImage = applycform(labOutputImage,makecform('lab2srgb'));
figure, imshow(rgbInputImage);
figure, imshow(rgbOutputImage);
img=rgbOutputImage;
final_image = zeros(size(img,1), size(img,2));
This routine calls a function fcnBPDHE.(Function Brightness Preserving Dynamic Histogram Equalization.)
You Need To Have The Function saved in your MATLAB directory.


Step 2:

Detect Skin Regions:

The next step is to detect the skin regions in an image. Photos in which people are fully covered give the best results as the complexity of the code reduces and complex procedures such as neural networks or template matching are not required. But this code does a good job of identifying faces even with some skin shown in the image. 
Code For Skin Detection & Segmentation:
if(size(img, 3) > 1)
for i = 1:size(img,1)
for j = 1:size(img,2)
R = img(i,j,1);
G = img(i,j,2);
B = img(i,j,3);
if(R > 92 && G > 40 && B > 20)
v = [R,G,B];
if((max(v) - min(v)) > 15)
if(abs(R-G) > 15 && R > G && R > B)
%it is a skin
final_image(i,j) = 1;
end
end
end
end
end

Step 3:
Fill In The Holes:

So far so good. The idea is to let MATLAB identify white blobs in an image but because of certain broken blobs they are identified as separate blobs and this makes it difficult to detect faces with accuracy. In the above picture, the chinese women's face has a big black cut running between the two eyes. and the same with the chinese man.
What we would do is we would fill the gaps with white spaces so as to make it a solid white blob.
MATLAB has an inbuilt function for the same known as 'imfill'. Type imfill in MATLAB help to find out more about the function.
Before performing 'imfill' we need to convert our grayscale image into a binary image.
'BW=im2bw(I,level);' converts the grayscale image I to a binary image. The output image BW replaces all pixels in the input image with luminance greater than level with the value 1 (white) and replaces all other pixels with the value 0 (black). You specify level in the range [0,1], regardless of the class of the input image. The function 'greythresh' can be used to compute the level argument automatically. If you do not specify levelim2bw uses the value 0.5.

%Grayscale To Binary.
binaryImage=im2bw(final_image,0.6);
figure, imshow(binaryImage);

%Filling The Holes.
binaryImage = imfill(binaryImage, 'holes');
figure, imshow(binaryImage);


Step 4:
Putting Bounding Boxes Around Detected Blobs and Counting Them
Well in this routine we eliminate those white pixels that fall under a threshold.
For eg: if the face pixel density is lets say 2000 then anything below that is not a face. And such pixels would be discarded and a new binary image would be present with the remaining face pixels.
The Basic Steps Are As Follows:
1. Determine the Connected Components.
2. Computer the Area of Each Component.
3. Remove Small Objects.
Checkout MATLAB Help For More information on these functions...Click On Read More To See The Complete Post.






FINAL IMAGE WITH BOUNDING BOXES AROUND DETECTED FACES:


Code:
binaryImage = bwareaopen(binaryImage,1890);   
figure,imshow(binaryImage);
labeledImage = bwlabel(binaryImage, 8);
blobMeasurements = regionprops(labeledImage, final_image, 'all');
numberOfPeople = size(blobMeasurements, 1)
imagesc(rgbInputImage); title('Outlines, from bwboundaries()'); 
%axis square;
hold on;
boundaries = bwboundaries(binaryImage);
for k = 1 : numberOfPeople
thisBoundary = boundaries{k};
plot(thisBoundary(:,2), thisBoundary(:,1), 'g', 'LineWidth', 2);
end
% hold off;


imagesc(rgbInputImage);
hold on;
title('Original with bounding boxes');
%fprintf(1,'Blob # x1 x2 y1 y2\n');
for k = 1 : numberOfPeople % Loop through all blobs.
% Find the mean of each blob. (R2008a has a better way where you can pass the original image
% directly into regionprops. The way below works for all versionsincluding earlier versions.)
thisBlobsBox = blobMeasurements(k).BoundingBox; % Get list of pixels in current blob.
x1 = thisBlobsBox(1);
y1 = thisBlobsBox(2);
x2 = x1 + thisBlobsBox(3);
y2 = y1 + thisBlobsBox(4);


   % fprintf(1,'#%d %.1f %.1f %.1f %.1f\n', k, x1, x2, y1, y2);
x = [x1 x2 x2 x1 x1];
y = [y1 y1 y2 y2 y1];
%subplot(3,4,2);
plot(x, y, 'LineWidth', 2);
end


%figure, imshow(labeledImage);
%B = bwboundaries(binaryImage);
%imshow(B);
%text(10,10,strcat('\color{green}Objects Found:',num2str(length(B))))
%hold on
%for k = 1:length(B)
%boundary = B{k};
%plot(boundary(:,2), boundary(:,1), 'g', 'LineWidth', 0.2)
%end
end

No comments:

Post a Comment