File Exchange Pick of the Week

Our best user submissions

Detecting Faces in Images 6

Posted by Brett Shoelson,

Brett's Pick this week is more of a challenge than a "Pick".

If I were to search the MATLAB Central File Exchange for "face detection" (with the quotation marks) I would get a dazzling--and somewhat overwhelming--array of 44 hits. Trying to detect faces (or anything else*) in images seems to me a reasonable thing to want to do, and in my mind typifies the challenges that the Computer Vision System Toolbox was made to address. In fact, I shudder to think of what classical "image processing" code designed to detect faces might look like.

But how do we navigate the 44 files on the File Exchange that facilitate that detection process? Where do we start? I can sort the results by number of downloads, by rating, by relevancy, ....

Some of those downloads seem to have great potential. But where does one start?

For this post, I'm going to go "out-of-model" and focus not on a submitted File Exchange file, but on core capabilities of the Computer Vision System Toolbox. This Toolbox has a "Cascade Object Detector" that facilitates out-of-the-box detection of faces, eyes, noses, mouths, and upper bodies. In fact, there are two in-product face detectors, and "FrontalFaceCART" (frontal face detection using "Classification And Regression Tree" analysis) is the default tool behavior.

Using the detectors in the Computer Vision System Toolbox is stunningly simple; we simply instantiate an instance of the cascade object detector and, for visualization purposes, a vision.ShapeInserter:

faceDetector = vision.CascadeObjectDetector;
shapeInserter = vision.ShapeInserter('BorderColor','Custom','CustomBorderColor',[0 255 255]);

We then read input image:

I = imread('visionteam.jpg');

Then we simply "step" the instantiated objects on the image of interest. The CascadeObjectDetector returns the bounding boxes of the detected objects, and the ShapeInserter delimits them in the image:

bbox = step(faceDetector, I);
% Draw boxes around detected faces and display results
I_faces = step(shapeInserter, I, int32(bbox));
imshow(I_faces), title('Detected faces');

Detecting faces using MathWorks' functionality is trivially easy. It requires MATLAB, the Image Processing Toolbox, and the Computer Vision System Toolbox, and a few readily reproducible lines of code.

Swag to the first person who shows me how to detect faces in "visionteam.jpg" with a File Exchange submission!

By the way, you can readily train MATLAB to find objects that are not in the "pre-trained" list above. For that, you might find these tools useful:

Comments welcome!

Get the MATLAB code

Published with MATLAB® R2014a


Comments are closed.

6 CommentsOldest to Newest

Dima Lisin replied on : 1 of 6
The Computer Vision System Toolbox also provides the insertShape function for drawing shapes into images. It is a bit more user-friendly than the vision.ShapeInserter object, and it allows you to specify the line width.
Amro replied on : 2 of 6
We could call OpenCV in MATLAB, which includes a face detector implemented as a Haar Feature-based Cascade Classifier. Here is a sample code using mexopencv (a library of MEX-functions to interface with OpenCV): % Load the pre-trained frontal-faces classifier xmlfile = fullfile(mexopencv.root(),'test','haarcascade_frontalface_alt2.xml'); cascade = cv.CascadeClassifier(xmlfile); % Load the source image, and convert to grayscale src = imread('visionteam.jpg'); gray = cv.cvtColor(src, 'RGB2GRAY'); gray = cv.equalizeHist(gray); % Perform the detection, and draw the detected faces faces = cascade.detect(gray, 'ScaleFactor',1.1, 'MinNeighbors',4); for i=1:length(faces) src = cv.rectangle(src, faces{i}, 'Color',[0,255,0], 'Thickness',2); end imshow(src) From what I understand, the Computer Vision System Toolbox also uses OpenCV internally to implement some of its functionality..
Brett replied on : 3 of 6
Thanks, Amro. Without a pretty significant effort of downloading and installing (and learning to use!) OpenCV, I can't test out your code. I'll assume it works, and send you some swag for your thoughtful response. But the challenge I posed is still open to anyone who cares to show me how to do this _with a File Exchange submission_! Cheers, Brett
Brett replied on : 4 of 6
Amro...would you please send me your address so I can send you some swag? My email is: char(cumsum([98 16 -13 15 0 -70 69 -11 7 -10 7 7 -4 -1 -46 45 -12 19 -12 15 -8 3 -7 8 -69 53 12 -2])) :) Thanks, Brett
Edgar Guevara replied on : 6 of 6
@Brett, Nice way to hide your email adress from spambots :) encrypt_email = @(email) [double(email(1)) diff(double(email))]