Skip to main content

Basic Face Detection, Opencv 3 Visual Studio 2015

Basic Opencv Face Detection Tutorial

Basic Opencv C++ tutorial how to detect the face from video image and any source you can achieve.
If you have little bit skills with programing you can just achieve the result under 10 minutes. I would like to reccomend also the installation by NUGET package. It is the fastest approach in Visual Studio how to start coding with opencv.. 

Problem with CascadeClassifier detectMultiScale Opencv 3.0.0. Windows prebuild libs

Problem is that in many cases with 32 bit Opencv 3.0.0 Windows version in debug and in release mode. I thing any of haarcascade_xxx return vector<Rect> Faces (testing number of cascades is 7). The length of the vector is incomprehensibly bigger than I can expect with my parameters and experiences with previous Opencv version..

vector<Rect> of detection is to big 

This is some kind of bug that I try to find in opencv source. What is surprising is fact that 64 bit version of Opencv works fine in my case. 

 I am using Visual Studio 2015 with 64 bit Opencv 3.0.0

 Here is my tutorial how to install Opencv 3.0.0 libs for Visual Studio 2015. 
Install Opencv for Visual Studio 2015
You can simple prepare the project inside the Visual Studio 2015 by Nuget Packages. This approach is easy for beginers and better than standard installation with all the environmental variables problems. Just follow the installation steps inside here 

Face detection basic Opencv C++ code example

There is nothing special, but you can also find the example how to replace the face with transparent mask or any pictures. This is at least fun. 

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/objdetect.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
 // capture from web camera init

 VideoCapture cap(0);;

 Mat img;

// Load cascate classifier placed in sulution folder
 CascadeClassifier detector;
 string cascadeName = "haarcascade_frontalface_alt.xml";
 bool loaded = detector.load(cascadeName);
// Parameters of detectMultiscale Cascade Classifier
 int groundThreshold = 2;
 double scaleStep = 1.1;
 Size minimalObjectSize(80, 80);
 Size maximalObjectSize(200, 200);

// Vector of returned faces
 vector found;

 for (;;)
      // Image from camera to Mat

      cap >> img;
     // Convert input to greyscale 
      Mat image_grey;
      cvtColor(img, image_grey, CV_BGR2GRAY);

      // why not
      // Detect faces
      detector.detectMultiScale(image_greyHead, found, scaleStep, groundThreshold, 0         | 2, minimalObjectSize,maximalObjectSize);
      // Draw the results into mat retrieved from webcam
      if (found.size() > 0) {
            for (int i = 0; i <= found.size() - 1; i++) {
             rectangle(img, found[i].br(), found[i].tl(), Scalar(0, 0, 0), 1, 8, 0);

    //Show the results

     imshow("wooohooo", img);
     int key2 = waitKey(20);

 return 0;


  1. I have got this error:xxx.cpp(56): error C2065: 'image_greyHead': undeclared identifier

    Can you share the value of this identifier?

  2. 'image_greyHead': undeclared identifier what should i do?

    1. Replace image_greyHead to image_grey lol

  3. also point to the correct folder for the haar classifer

  4. The throw : Read access to offense.

    this이(가) 0x445FF8였습니다.

    If you have a program for this exception handler can continue safely.

    template inline
    Point_<_Tp> Rect_<_Tp>::br() const
    return Point_<_Tp>(x + width, y + height); <<error part

  5. I got exception like "Unhandled exception at 0x000007FEFD619E5D in OpenCVTest.exe: Microsoft C++ exception: cv::Exception at memory location 0x00000000001BF1F0."
    what should I do

  6. mine gives error on vector. I fixed it vector found. but why it cant detect my face?

  7. This comment has been removed by the author.

    1. This comment has been removed by the author.


  8. 'std::vector': use of class template requires template argument list this is error
    can you provide me some idea

    1. write vector found; instead of vector found;
      Worked for me. Probably not gonna be useful for you since you might have already solved it but I hope it can help someone one day

    2. welp sorry, meant that you have to enter Rect into brackets next to vector. Idk why I can't right it in comment section

  9. The information in this blog is handy for the people. Face Recognition App

  10. This comment has been removed by the author.

  11. fixed file,you can download it from:


Post a Comment