📄 face detection.cpp
字号:
// Face detection.cpp : Defines the entry point for the console application.
//
#pragma warning( disable : 4996 )
#include "stdafx.h"
#include "cv.h"
#include "cvaux.h"
#include "highgui.h"
#include "cxcore.h"
#include <stdlib.h>
#include "constants.h"
#include "facedetect.h"
void FaceDetection(char *filename, double scale=SCALE)
{
int status;
int i;
IplImage* img = NULL; // Image to detect faces in.
CvSeq* faces = NULL; // Storage for found face coordinates.
char *WindowName = "Image";
/*
* Initialize the face cascade.
*/
status = init_cascade();
if(status < 0){
fprintf(stderr, "Initializing cascade failed.\n");
exit(1);
}
/*
* Load the image.
*/
img = cvLoadImage(filename, -1);
if(img == NULL){
fprintf(stderr, "Could not load image file: %s\n", filename);
exit(1);
}
/*
* Check the image size.
*/
if(img->width > MAX_WIDTH || img->height > MAX_HEIGHT) {
fprintf(stderr, "Image too big, should be smaller then %d, %d.\n", MAX_WIDTH, MAX_HEIGHT);
fprintf(stderr, "Current size is : width %d, height %d.\n", img->width, img->height);
exit(1);
}
/*
* Detect the coordinates in the face.
*/
faces = analyse_frame(img, scale);
/*
* Print the found coordinates.
*/
for( i = 0; i < (faces ? faces->total : 0); i++ )
{
CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
int x, y, width, height;
x = (int)(r->x * scale);
y = (int)(r->y * scale);
width = (int)(r->width * scale);
height = (int)(r->height * scale);
printf("Face %d: (%d, %d, %d, %d)\n",i+1, x, y, width, height);
CvPoint p1 = cvPoint(x, y);
CvPoint p2 = cvPoint(x + width, y + height);
//cvDrawRect(img, p1, p2, cvScalar(0,0,255), 2);
}
/*
* Create Window and show image.
*/
cvNamedWindow(WindowName,1);
cvShowImage(WindowName,img);
}
void help(char *name, int status)
{
printf ("Usage %s <image> <scale>\n", name);
printf ("Scale factor between 1 and 2, 2 being the best precision.\n");
printf ("The scale flag is optional, the default %.1lf will be used.\n", SCALE);
printf ("When succesfull, coordinates 'x, y, width, height' are printed.\n");
exit(status);
}
int _tmain(int argc, char* argv[])
{
int status;
double scale = 0;
/*
* Some prechecks
*/
if(argc > 3 || argc < 2) {
fprintf(stderr, "Wrong number of argumuments.\n");
help(argv[0], 1);
}
status = strcmp(argv[1], "-h");
if(status == 0){
help(argv[0], 0);
}
if(argc == 2) {
scale = SCALE;
}
if(argc == 3) {
scale = strtod(argv[2], NULL);
}
/*
* Check the scale factor.
*/
if(scale < 1.0 || scale > 2.0){
help(argv[0], 1);
}
FaceDetection(argv[1], scale);
cvWaitKey();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -