OpenCV (Open Source Computer Vision) is released under a BSD license and hence it’s free for both academic and commercial use. It has C++, C, Python and Java interfaces and supports Windows, Linux, Mac OS, iOS and Android. OpenCV was designed for computational efficiency and with a strong focus on real-time applications. Written in
optimized C/C++, the library can take advantage of multi-core processing.
Adopted all around the world, OpenCV has more than 47 thousand people of user community and estimated number of downloads exceeding 9 million. Usage ranges from interactive art, to mines detection, online maps and advanced robotics.
Head over to https://www.visualstudio.com/products/visual-studio-professional-with-msdn-vs and download Visual Studio Professional 2015. Unzip the downloaded file and double-click the ‘vs_professional.exe’, then the installation process will begin.
1) Head over to the site: http://www.opencv.org and download the latest version of OpenCV (shown in the following figure). Choose the version according to your operating system.
In this tutorial we are going to install OpenCV 3.1 using Visual Studio 2015 professional on a 64-bit system running Windows 10.
2) Extract the downloaded OpenCV file
Double click the downloaded OpenCV file, and then extract it
2.Edit the PATH environment variables and Add a new environment variable, then give it the value of F:\opencv\build\x64\vc14\bin
. Note that change the value depends on the path where you have extracted your OpenCV in step 2.
Select Win32 Console Application in Visual C++, then name your project and select a directory to store it
Select “Include Directories”, and give it the following values:
F:\opencv\build\include
F:\opencv\build\include\opencv
F:\opencv\build\include\opencv2
Remember that change the value depending on the path you have extracted your OpenCV files to in step 2.
F:\opencv\build\x64\vc14\lib
. Remember that changing the values depends on the path where you have extracted your OpenCV in step 24.Add additional dependences
Copy the following item and paste it in additional Dependences blank
opencv_world310d.lib
#include
#include
#include
#include
#include
using namespace std;
using namespace cv;
string harrEye = "..\\..\\..\\resources\\harr\\haarcascade_eye_tree_eyeglasses.xml";//load harr feature xml file
string harrFace = "..\\..\\..\\resources\\harr\\haarcascade_frontalface_alt.xml";
CascadeClassifier faceCascade;
CascadeClassifier EyeCascade;
string windownName = "Capture faces and eyes ";
void detectAndDiapley(Mat frame);
int main()
{
Mat frame;
// load the cascades
if (!EyeCascade.load(harrEye))
cout << "load harrEye failed" << endl;
if (!faceCascade.load(harrFace))
cout << "load harrFace failed" << endl;
// read the video stream
VideoCapture capture(0);
if (capture.isOpened())
{
while (true)
{
capture >> frame;
// apply the cascaders to the frame
if (!frame.empty())
{
detectAndDiapley(frame);
}
else
{
cout << "input video frame is empty" << endl;
}
if (waitKey(30) >= 0)break;
}
}
return 0;
}
void detectAndDiapley(Mat frame)
{
vector faces;
Mat frameGray;
cvtColor(frame, frameGray, CV_BGR2GRAY);
equalizeHist(frameGray, frameGray);
//Detect faces
faceCascade.detectMultiScale(frameGray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
for (int i = 0; i < faces.size(); i++)
{
Point Vertex1(faces[i].x, faces[i].y);
Point Vertex2(faces[i].x + faces[i].width, faces[i].y + faces[i].height);
rectangle(frame, Vertex1, Vertex2, Scalar(0, 0, 255), 2, 8, 0);
Mat faceROI = frameGray(faces[i]);
vector eyes;
// detect eyes in each face
EyeCascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
for (int j = 0; j < eyes.size(); j++)
{
Point center(faces[i].x + eyes[j].x + eyes[j].width / 2, faces[i].y + eyes[j].y + eyes[j].height / 2);
int radius = cvRound((eyes[j].width + eyes[j].height)*0.25);
Size axes(eyes[j].width / 2, 13);
ellipse(frame, center, axes, 0, 0, 360, Scalar(255, 255, 0), 2, 8, 0);
}
}
// show the faces and eyes detected
imshow(windownName, frame);
}
Set two options as following figure shows:
Press F5 to execute the face detection project, your PC camera will turn on and your face and eyes will be highlighted like so:
1.http://www.michaelpsyllakis.com/install-opencv-on-visual-studio-2015-community-tutorial/
2.http://docs.opencv.org/2.4/opencv_tutorials.pdf
3.http://docs.opencv.org/2.4/opencv2refman.pdf