1, identify a picture of the number of faces, where the absolute path is used, can be modified by itself
#include <cstdio> #include <cstdlib> #include <Windows.h> #include <vector> #include <i
ostream> #include <string> #include <opencv2\opencv.hpp> using namespace std;
using namespace CV;
int main (int argc, char* argv[]) {//Load Haar feature detection classifier cascadeclassifier cascade;
Cascade.load ("D:\\Program files\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt2.xml");
Load Image const String pstrimagename = "E:\\1.jpg";
Mat srcimage = Imread (Pstrimagename, Cv_load_image_color);
Mat Grayimage;
Cvtcolor (Srcimage, Grayimage, Cv_bgr2gray);
Face recognition and tagging if (Cascade.empty ()) cout << "classifier not Loaded" << Endl; Cvscalar facecirclecolors[] = {{0, 0, 255}}, {{0, 128, 255}}, {{0, 255, 255}}, {{0, 255, 0}
}, {{255, 128, 0}}, {{255, 255, 0}}, {{255, 0, 0}}, {{255, 0, 255}};
Vector<cv::rect> faces;
DWORD Dwtimebegin, Dwtimeend; Dwtimebegin = GetTickCount ();
Identification of Cascade.detectmultiscale (Grayimage, faces);
Dwtimeend = GetTickCount (); cout << "face Count:" << faces.end ()-Faces.begin () << "Time to identify:" << dwtimeend-dwtimebegin << "
Ms\n ";
Mark int n = 0;
for (Vector<cv::rect>::const_iterator i = Faces.begin (); I <faces.end (); i++, n++) {Point Center;
int radius;
Center.x = Cvround ((i->x + i->width * 0.5));
Center.y = Cvround ((i->y + i->height * 0.5));
Radius = Cvround ((i->width + i->height) * 0.25);
Circle (srcimage, center, RADIUS, facecirclecolors[n% 8], 2);
} imshow ("src", srcimage);
Waitkey (); }
2. Recognize faces from the video stream
#include "opencv2/objdetect/objdetect.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/
Imgproc.hpp "#include <iostream> #include <stdio.h> using namespace std;
using namespace CV;
/** global variable */Cascadeclassifier Face_cascade;
Cascadeclassifier Eyes_cascade; Cvscalar facecirclecolors[] = {{0, 0, 255}}, {{0, 128, 255}}, {{0, 255, 255}}, {{0, 255, 0}}, {{2
55, 128, 0}}, {{255, 255, 0}}, {{255, 0, 0}}, {{255, 0, 255}}; /** @ MAIN function */int main (int argc, const char** argv) {face_cascade.load ("D:\\Program Files\\opencv\\sources\\data\\haarc
Ascades\\haarcascade_frontalface_alt.xml ");
if (Face_cascade.empty ()) cout << "Face Error loading" << Endl;
Videocapture Capture (0);
Mat frame;
while (1) {Capture >> frame;
if (Frame.empty ()) cout << "error" << Endl;
Vector<cv::rect> faces;
Mat Grayimage; Cvtcolor (frame, grayimage, Cv_bgr2gray);
Identification of Face_cascade.detectmultiscale (Grayimage, faces);
Mark int n = 0;
for (Vector<cv::rect>::const_iterator i = Faces.begin (); I!=faces.end (); i++, n++) {Point Center;
int radius;
Center.x = Cvround ((i->x + i->width * 0.5));
Center.y = Cvround ((i->y + i->height * 0.5));
Radius = Cvround ((i->width + i->height) * 0.25);
Circle (frame, center, radius, facecirclecolors[n% 8], 2);
} imshow ("video", frame);
Waitkey (10);
} return 0;
}