OpenCV simple face recognition program
- 2020-06-19 11:08:26
- OfStack
This article shares the specific code of OpenCV face recognition program for your reference. The specific content is as follows
//Haar Feature detection, face recognition algorithm, is used xml As a trained classifier
#include<opencv2\opencv.hpp>
#include<cstdio>
#include<cstdlib>
#include<Windows.h>
using namespace std;
int main()
{
// loading Haar Feature detection classifier
// haarcascade_frontalface_alt.xml Department of OpenCV Built-in classifier
//
//C++ There are still a lot of Pointers,
// Remember the newline usage, \ No Spaces after it
const char *pstrCascadeFileName = \
"D:\\opencv2.4.9\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml";
CvHaarClassifierCascade *pHaarCascade = NULL; // define 1 A result of the training Hal has had
// Here, cascade mean 1 drops 1 Drops falling --- This is my English solution, so you can ignore it.
pHaarCascade = (CvHaarClassifierCascade *)cvLoad(pstrCascadeFileName); // Load existing xml file
// Load the image, and I'm going to use the pointer again
const char *pstrImageName = "E:\\testpictures\\meizi.jpg";
IplImage *pSrcImage = cvLoadImage(pstrImageName, CV_LOAD_IMAGE_UNCHANGED); // Load the original image unchanged.
IplImage *pGrayImage = cvCreateImage(cvGetSize(pSrcImage), IPL_DEPTH_8U, 1);// create 8 So the size 1 Channel empty image
cvCvtColor(pSrcImage, pGrayImage, CV_BGR2GRAY);// Copy the image
// Face recognition and marking
if (pHaarCascade != NULL) // If you have xml File, then proceed
{
//CvScalar : contains 4 a double Member, can be used to indicate B . G . R . alpha----alpha It's used to represent the transparency of the image
CvScalar FaceCircleColors[]= // This is a 1 These columns of colors can be viewed as palettes
{
{ {0,0,255} },
{ {0,128,255} },
{ {0,255,255} },
{ {0,255,0} },
{ {255,128,0} },
{ {255,255,0} },
{ {255,0,0} },
{ {255,0,255} }
};
// The memory memory is 1 Can be used to store such as sequences, Outlines, graphics , Subdivision and other dynamic growth of the underlying structure of the data structure.
CvMemStorage *pcvMStorage = cvCreateMemStorage(0);
cvClearMemStorage(pcvMStorage); // Initialize memory
// identify
DWORD dwTimeBegin, dwTimeEnd; //DWORD is Double Word . each word for 2 The length of two bytes
// in Release Version of this function from 0 Start the timer and return the number of milliseconds since the device started (excluding system pause time).
// in Debug In version 2, the timer is subtracted from the device when it starts 180 Seconds. This makes it easy to test the correct overflow handling of code using this function.
dwTimeBegin = GetTickCount();
// Dense sequences are derived from CvSeq , they are used to represent extensible 1 Dimensional array - Vectors, stacks, queues, and double-ended queues.
// The sparse sequence derives from CvSet . CvSet Is based on CvSeq They are all made up of nodes, each 1 The nodes are either occupied, then empty, by the flag bit flag Decision.
// function cvHaarDetectObjects Using the cascade classifier trained for a target object, the rectangular regions containing the target object are found in the image, and these regions are taken as 1 Returns the rectangle of the sequence.
CvSeq *pcvSeqFaces = cvHaarDetectObjects(pGrayImage, pHaarCascade, pcvMStorage);// Get it here 1 Series rectangular box
dwTimeEnd = GetTickCount();
printf(" Number of faces: %d Identification time: %d ms \n", pcvSeqFaces->total, dwTimeEnd - dwTimeBegin);// Calculate the time and the number of rectangular boxes containing faces
// tag
for (int i = 0; i < pcvSeqFaces->total; i++)
{
CvRect* r = (CvRect*)cvGetSeqElem(pcvSeqFaces, i);// Extract each from the rectangular box dense sequence 1 A rectangular box
CvPoint center;// define 1 A midpoint
int radius;// define 1 A radius
center.x = cvRound((r->x + r->width*0.5));// Get the center point of the center of the circle x,y coordinates
center.y = cvRound((r->y + r->height*0.5));
radius = cvRound((r->width + r->height)*0.25);// Simplify the calculation. It should be high 1 Half of the square and the width 1 Half a square. Take the square root
cvCircle(pSrcImage, center, radius, FaceCircleColors[i / 8], 2);// Circle the picture, color it in,
}
cvReleaseMemStorage(&pcvMStorage);// Free memory
}
// The new window displays the image, and the destroy window.
const char *pstrWindowTitle = " Face recognition ";
cvNamedWindow(pstrWindowTitle, CV_WINDOW_AUTOSIZE);
cvShowImage(pstrWindowTitle, pSrcImage);
cvWaitKey(0);
cvDestroyWindow(pstrWindowTitle);
cvReleaseImage(&pSrcImage);
cvReleaseImage(&pGrayImage);
return 0;
}