Face Detection - MATLAB CODE

                                  Lets see how to detect face, nose, mouth and eyes using the MATLAB built-in class and function. Based on Viola-Jones face detection algorithm, the computer vision system toolbox contains vision.CascadeObjectDetector System object which detects objects based on above mentioned algorithm.
  
Prerequisite: Computer vision system toolbox

FACE DETECTION:

clear all
clc
%Detect objects using Viola-Jones Algorithm

%To detect Face
FDetect = vision.CascadeObjectDetector;

%Read the input image
I = imread('HarryPotter.jpg');

%Returns Bounding Box values based on number of objects
BB = step(FDetect,I);

figure,
imshow(I); hold on
for i = 1:size(BB,1)
    rectangle('Position',BB(i,:),'LineWidth',5,'LineStyle','-','EdgeColor','r');
end
title('Face Detection');
hold off;




The step(Detector,I) returns Bounding Box value that contains [x,y,Height,Width] of the objects of interest.


BB =

    52    38    73    73
   379    84    71    71
   198    57    72    72

NOSE DETECTION:


%To detect Nose
NoseDetect = vision.CascadeObjectDetector('Nose','MergeThreshold',16);



BB=step(NoseDetect,I);


figure,
imshow(I); hold on
for i = 1:size(BB,1)
    rectangle('Position',BB(i,:),'LineWidth',4,'LineStyle','-','EdgeColor','b');
end
title('Nose Detection');
hold off;





EXPLANATION:


To denote the object of interest as 'nose', the argument  'Nose' is passed.

vision.CascadeObjectDetector('Nose','MergeThreshold',16);

The default syntax for Nose detection :
vision.CascadeObjectDetector('Nose');

Based on the input image, we can modify the default values of the parameters passed to vision.CascaseObjectDetector. Here the default value for 'MergeThreshold' is 4.

When default value for 'MergeThreshold' is used, the result is not correct.
Here there are more than one detection on Hermione.




To avoid multiple detection around an object, the 'MergeThreshold' value can be overridden. 


MOUTH DETECTION:



%To detect Mouth
MouthDetect = vision.CascadeObjectDetector('Mouth','MergeThreshold',16);

BB=step(MouthDetect,I);


figure,
imshow(I); hold on
for i = 1:size(BB,1)
 rectangle('Position',BB(i,:),'LineWidth',4,'LineStyle','-','EdgeColor','r');
end
title('Mouth Detection');
hold off;



EYE DETECTION:


%To detect Eyes
EyeDetect = vision.CascadeObjectDetector('EyePairBig');

%Read the input Image
I = imread('harry_potter.jpg');

BB=step(EyeDetect,I);



figure,imshow(I);
rectangle('Position',BB,'LineWidth',4,'LineStyle','-','EdgeColor','b');
title('Eyes Detection');
Eyes=imcrop(I,BB);
figure,imshow(Eyes);







Cropped Image


I will discuss more about object detection and how to train detectors to identify object of our interest in my upcoming posts. Keep reading for updates.

84 comments:

  1. I have MATLAB of version R2010a which does not have the Computer vision system toolbox.How to detect eyes,mouth,nose in that case?

    ReplyDelete
  2. @Rohini Bhargava

    You need to install 'Computer vision system toolbox' . For more information, visit http://www.mathworks.in/products/computer-vision/ .

    ReplyDelete
    Replies
    1. i have requested for the toolbox but how to install it.???

      Delete
  3. @AaronAngel_ hi ...i am doing a mini project on matlab....pls help me... i need to capture image ,recognize the customer ,display his names.

    ReplyDelete
  4. i hav matlab version 7. how do i perform face recognition on that.. please help..

    ReplyDelete
  5. can some0ne tell me how to install computer vivsion toolbox

    ReplyDelete
  6. i m doing a major projct on matlab... plz tell me how to campare or recognize captured image with the image stored in database and will display the name

    ReplyDelete
  7. i m doing my major project on matlab..plz tell me how to campare or recognize captured image with the image stored in database and will be display the name after successful matching...

    ReplyDelete
  8. thnk u very much....i was wondering how to detect the facial features.....it is interesting.

    ReplyDelete
  9. @nishant agrawalhaan yr ye he humein samajh nhe aa rhe tujhe pta chale to reply zarur karna yaad se bhoolna mat.....

    ReplyDelete
  10. Dear friend
    what your Principle on ur mouth and eyes detection?

    ReplyDelete
  11. Dear friend
    What Theory or Principle on your mouth and eyes ?
    I want to do research in this area thanks more^^

    ReplyDelete
  12. how to separate left eye and right eye?

    ReplyDelete
  13. hey !!! can you help me in Iris localaization
    ?

    ReplyDelete
  14. Dear Friend. How to identify a face in snap?? Please response me

    ReplyDelete
  15. How to identify a face in snap???

    ReplyDelete
  16. how can i find the centre of the squares that detects eyes or nythiing???

    ReplyDelete
  17. Hello!!!
    I am an assistant professor in an engg college and need an account on MATLAB. Please tell me the procedure

    ReplyDelete
  18. but i dnt hv control vision toolbox,witout that vision cascade object detector method is not possible?wat can i do?

    ReplyDelete
  19. computer vision system tool box in matlab which verision

    ReplyDelete
  20. There's something wrong with mouth detection. It detects everything like eyes and nose. On the other great tutorial thanks!

    ReplyDelete
  21. Could you please tell me are you using Viola and jones algorithm only or you used Adaboost beside ?

    ReplyDelete
  22. It's very useful, thank you!

    ReplyDelete
  23. Hi i m so interested in face reconition. Pls can you tell me how to capture the watch on the wrist of a person and to display the current time on that watch

    ReplyDelete
  24. Hi i m interested in face and object recogonition. Pls can you tellme how to capture the image of the watch in the wrist of a person and to display the current time on the watch. Pls rply me its very urgent for the part of my project in matlab

    ReplyDelete
  25. Hi i m so interested in face reconition. Pls can you tell me how to capture the watch on the wrist of a person and to display the current time on that watch

    ReplyDelete
  26. thanks. I need to detect eyes, mouth and nose in different images simultaneousely. how can I set Mergethreshold value?

    ReplyDelete
  27. I need to detect eyes, mouth and nose in many different images. but Mergethreshold value must be different in different images. what can J do?

    ReplyDelete
  28. hey this is awesome thanks for sharing this
    can you help me out to detect the ears from the image using any matlab algorithm

    thank you ..

    ReplyDelete
  29. How it can be trained by our own images????

    ReplyDelete
  30. How can We train it by our own images??
    plx Reply

    ReplyDelete
  31. I am wondering whether the same can be applied for the detection of say, number plates?

    ReplyDelete
  32. hai..I would like to know whether we can detect the eyes and nose in same window?? for that what we have to do for multiple detection in single image

    ReplyDelete
  33. hello!
    I'm learning about identifying and catching face,. Can you help me?

    ReplyDelete
  34. how can i create a code for facial expression

    ReplyDelete
  35. How can i fit spectacles in an eyes using matlab.

    ReplyDelete
  36. showing error in FDetect = vision.CascadeObjectDetector;
    Plz tell me the solution

    ReplyDelete
  37. just search in mathworks+Computer vision toolkit+trial version. After registering they will give you one password to access it.

    ReplyDelete
  38. very nice explanation. But in nose detect and mouth detect only one or two persons detection only working. No error. Everything working fine.

    Can you provide same code in GUI mode?? How can i build a button and Image_window based on this code??

    ReplyDelete
  39. @Rohini Bhargava Download a Trial version of Toolkit from mathworks.com website.

    ReplyDelete
  40. how can i detect all the features on one image like the nose,mouth ,eyes and the face on the same image not separately

    ReplyDelete
  41. how can i detect all the features on one image like the nose,mouth ,eyes and the face on the same image not separately

    ReplyDelete
  42. how can i detect all the features on one image like the nose,mouth ,eyes and the face on the same image.

    ReplyDelete
  43. how can i detect points on face///

    ReplyDelete
  44. how to code image matching in android studio with opencv

    ReplyDelete
  45. Eye detection is giving error :


    Error using rectangle
    Value must be a 4 element vector

    Error in extend_ex (line 12)
    rectangle('Position',BB,'LineWidth',4,'LineStyle','-','EdgeColor','b');

    ReplyDelete
  46. hi can you suggest me how to detect facial feature point?

    ReplyDelete
  47. Hi, can you suggest a solution how to detect facial feature points. Thank you

    ReplyDelete
  48. How To detect nose of Animals?
    plz give me a code for Detection of nose of animal...
    Thanx

    ReplyDelete
  49. need code for NOse detection Of Animal
    urgent

    ReplyDelete
  50. Good, Matlab 2014 has these facilities......

    ReplyDelete
  51. I am doing a research on comparing image using elliptic biharmonic equation in face recognition, I already derived the equation and get the matrix and combined this matrix with images successful get the difference between the image so I need how to you this information to classify between the images if any image has the individual parameter
    thank you

    ReplyDelete
  52. Hi, plz.how can i detect the mouth region ?

    ReplyDelete
  53. i used this code but some extra boxes is drawa in face
    so pleas give me a solution and if i watns to detect a realtime face the how to detect

    ReplyDelete
  54. how to read the values of those nose , eyes, mouth since i want to store the all the values in database .

    ReplyDelete
  55. do you have face recognition code

    ReplyDelete
  56. Anyone can provide me face swapping code

    ReplyDelete
  57. how do i match two image (face same but different image).please help me!!!!!!!!!!!!!

    ReplyDelete
  58. sir please we want to a face spoofing detection using chrome components in matlab code

    ReplyDelete
  59. please send me the code of 'PCB fault detection using matlab'.

    ReplyDelete
  60. someone please send me matlab code for PCB fault detection using matlab.

    ReplyDelete
  61. To avoid the error in this step, try to use BB(1,:) instead of BB in the last two lines

    ReplyDelete
  62. how to detect face for a single image from different angles, plz

    ReplyDelete
  63. how can i detect face from different angles?

    ReplyDelete
  64. I want to recognize facial expressions in matlab some help plz

    ReplyDelete
  65. how to detect hand gesture? help me plzz

    ReplyDelete
  66. is the any response from anybody here? How can I then make the detected region black so that I can hide the user face?

    ReplyDelete
  67. how about detection acne in faces? what the command in vision.CascadeObjectDetector?

    ReplyDelete
  68. aoa... how can i detect the eye from real time video ??

    ReplyDelete
  69. aoa... how can i detect the eye from real time video ??

    ReplyDelete
  70. @nishantagrawal i also want the same as you were told "plz tell me how to campare or recognize captured image with the image stored in database and will display the name" please anybody do know everything about this project reply me or send me the link or anything that is resembles with this project send to ankityadavv786@gmail.com

    ReplyDelete
  71. hi iam doing my major project in image processing,plz tell me how to detect line in mural image by template orientation

    ReplyDelete
  72. hi iam doing my major project in image processing please tell me how to detect line from mural image by template orientation process using matlab

    ReplyDelete
  73. Can you give me code for ear detection along the face detection

    ReplyDelete
  74. Hi dear
    I want to use the backpropagation algorithm for face recognition. How can I get this coding?

    I hope to give me ur feedback

    kind regards

    Sarhan

    ReplyDelete
  75. hello, can anyone provide me Matlab code for skin disease detection
    thank you,,,,

    ReplyDelete