Viola-Jones Algorithm | Computer Vision

Viola-Jones Algorithm | Explained

Vila-Jones algorithm is widely used in Computer Vision for facial feature extraction. It can also be used for detecting other objects.  

Viola-Jones algorithm is a combination of the following techniques

1)   Haar like Features     
2)   Integral image     3)   Adaboost     4)   Cascading

Haar Features

Haar features are digital image features used in object recognition. Each feature result in a single value, calculated by subtracting the sum of pixel under white region from black region as shown in figure below. There are a lot of features eg in case of face there can be upto 150,000 features of different shapes.

Haar features in viola jones algorithm


Haar features


Integeral Image

In integral image the value of pixel (x,y) is sum of pixels above and to left of pixel(x,y). Example can be seen in the figure below. 
Integral image in viola jones
Integeral Image


To increase the computation speed we sum up only the corner pixels. Lets have an example. Suppose we have an image as shown below. If you add values of the diagonal pixels and subtract it from the summation of the other diagonal pixels then you have the sum of all the pixels, Mathematically 
  D    =    ( 1 + 4 )  -  ( 2 + 3 )
                        =   A+(A+B+C+D)-(A+C+A+B)
 =   D                      

integral image in viola jones

AdaBoost

AdaBoost is a machine learning algorithm for finding best features from the list of available features. The use of Adaboost decrease computation and hence increase speed.
In Adaboost there are two types of classifiers, i.e Strong classifier and week classifier. Weak classifier is one which detect more than 50% faces. A number of week classifier makes strong classifier. The weak Classifier output is 0 or 1.

Mathematically it can be represented as,

F(x)   =   a1*f1(x)   +    a2*f2(x)   +   a3*f3(x)..... 

Strong Classifier                                    Weak Classifier

Cascading

After adaboosting the features are reduced to 25000 features, but still it requires a lot of computations. So we reduce it by cascading. A Cascade consist of stages, each consist of strong classifier. An image is declared as a non Eyes if any of the classifier return 0.

Cascading in viola jones

 How can I apply this in Matlab??

_________________________________________________________________________________
Example
 image = imread("yourimage.jpg");
 faceDetector = vision.CascadeObjectDetector; % Load calssifier
 bboxes = step(faceDetector, Img);
 figure, imshow(Img), title('Detected faces');hold on
 for i=1:size(bboxes,1)      rectangle('Position',bboxes(i,:),'LineWidth',2,'EdgeColor','y');
 end
 end
_________________________________________________________________________________

 Is there any way to do it in OpenCV??

Yes here is an example.
_________________________________________________________________________________
Example
#include<opencv\cv.h>
#include<opencv\highgui.h>
#include<opencv2\objdetect\objdetect.hpp>
#include<vector>
using namespace cv;
using namespace std;
int main()
{
 CascadeClassifier face_cascade;
 if(!face_cascade.load("C:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_default.xml")) 
    { printf("Error loading cascade file for face"); return 1; }
 VideoCapture capture(0); //-1, 0, 1 device id
 if(!capture.isOpened())
    { printf("error to initialize camera");return 1; }
 Mat cap_img,gray_img;
 vector<Rect> faces;
 while(1)
 {
  capture >> cap_img;
  cvtColor(cap_img, gray_img, CV_BGR2GRAY);
 face_cascade.detectMultiScale(gray_img,faces,1.7,3,CV_HAAR_SCALE_IMAGE|CV_HAAR_DO_CANNY_PRUNING,cvSize(0,0), cvSize(300,300));
  for(int i=0; i < faces.size();i++)
   {
     Point pt1(faces[i].x+faces[i].width, faces[i].y+faces[i].height);
     Point pt2(faces[i].x,faces[i].y);
     Mat faceROI = gray_img(faces[i]);
     rectangle(cap_img, pt1, pt2, cvScalar(0,255,0), 2, 8, 0);
   }
  imshow("Result", cap_img);
  if(waitKey(30) >= 0) break;
 }
 return 0;
}
_________________________________________________________________________________

No comments:

Post a Comment

I FEEL AWESOME WHEN YOU COMMENT.