📄 opencvfacedetect.cpp
字号:
// OpenCVFaceDetect.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
// OpenCV Sample Application: facedetect.c
// Include header files
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <float.h>
#include <limits.h>
#include <time.h>
#include <ctype.h>
#include "videoInput.h"
// Create memory for calculations
static CvMemStorage* storage = 0;
// Create a new Haar classifier
static CvHaarClassifierCascade* cascade = 0;
// Function prototype for detecting and drawing an object from an image
void detect_and_draw( IplImage* image );
// Create a string that contains the cascade name
const char* cascade_name =
"haarcascade_frontalface_alt.xml";
/* "haarcascade_profileface.xml";*/
// Main function, defines the entry point for the program.
int main( int argc, char** argv )
{
// Structure for getting video from camera or avi
CvCapture* capture = 0;
// Images to capture the frame from video or camera or from file
IplImage *frame, *frame_copy = 0;
// Used for calculations
int optlen = strlen("--cascade=");
// Input file name for avi or image file.
const char* input_name;
// Check for the correct usage of the command line
if( argc > 1 && strncmp( argv[1], "--cascade=", optlen ) == 0 )
{
cascade_name = argv[1] + optlen;
input_name = argc > 2 ? argv[2] : 0;
}
else
{
fprintf( stderr,
"Usage: facedetect --cascade=\"<cascade_path>\" [filename|camera_index]\n" );
return -1;
/*input_name = argc > 1 ? argv[1] : 0;*/
}
// Load the HaarClassifierCascade
cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
// Check whether the cascade has loaded successfully. Else report and error and quit
if( !cascade )
{
fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
return -1;
}
// Allocate the memory storage
storage = cvCreateMemStorage(0);
//create a videoInput object
videoInput VI;
//Prints out a list of available devices and returns num of devices found
int numDevices = VI.listDevices();
int device1 = 0; //this could be any deviceID that shows up in listDevices
int device2 = 1; //this could be any deviceID that shows up in listDevices
//setup the first device - there are a number of options:
VI.setupDevice(device1, 320,240); //setup the first device with the default settings
//VI.setupDevice(device1, 640,480); //setup the first device with the default settings
//As requested width and height can not always be accomodated
//make sure to check the size once the device is setup
int width = VI.getWidth(device1);
int height = VI.getHeight(device1);
int size = VI.getSize(device1);
// Create a window in which the captured images will be presented
cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );
// Create a new named window with title: result (for face detection)
cvNamedWindow( "result", 1 );
//IplImage * frame;
frame = cvCreateImage( cvSize(width, height), 8, 3 );
IplImage * flipframe ;
flipframe = cvCreateImage( cvSize(width, height), 8, 3 );
//IplImage * frame_copy ;
frame_copy = cvCreateImage( cvSize(width, height), 8, 3 );
unsigned char * img1 = new unsigned char[VI.getSize(device1)]; //optional (DEVICE NUMBER) - gets the buffer size of the image handed back
unsigned char * img2 = new unsigned char[VI.getSize(device1)]; //optional (DEVICE NUMBER) - gets the buffer size of the image handed back
//to get the data from the device first check if the data is new
char key = 0;
while( key != 27)// 'ESC'
{
key = (char) cvWaitKey(10);
if(VI.isFrameNew(device1))
{
VI.getPixels(device1, img1, false); //returns pixels as a BGR (for openCV) unsigned char array
//VI.getPixels(device1, frame, true); //returns pixels as a RGB (for openGL) unsigned char array
frame->imageData = (char *)img1;
// On Macbook Pro and Apple iSight, the image grabbed is upside down, so we need to invert it.
//cvConvertImage(frame, flipframe, CV_CVTIMG_FLIP);
//cvShowImage( "mywindow", flipframe );
// Alternative:
// Check the origin of image. If top left, copy the image frame to frame_copy.
//if( frame->origin == IPL_ORIGIN_TL )
// cvCopy( frame, frame_copy, 0 );
// Else flip and copy the image
//else
cvFlip( frame, frame_copy, 0 );
cvShowImage( "mywindow", frame_copy );
detect_and_draw( frame_copy );
}
}
//to get a settings dialog for the device
//VI.showSettingsWindow(device1);
//getchar();
cvDestroyWindow( "mywindow" );
//Shut down devices properly
VI.stopDevice(device1);
//VI.stopDevice(device2);
return 0;
}
// Function to detect and draw any faces that is present in an image
void detect_and_draw( IplImage* img )
{
int scale = 1;
// Create a new image based on the input image
IplImage* temp = cvCreateImage( cvSize(img->width/scale,img->height/scale), 8, 3 );
// Create two points to represent the face locations
CvPoint pt1, pt2;
int i;
// Clear the memory storage which was used before
cvClearMemStorage( storage );
// Find whether the cascade is loaded, to find the faces. If yes, then:
if( cascade )
{
// There can be more than one face in an image. So create a growable sequence of faces.
// Detect the objects and store them in the sequence
CvSeq* faces = cvHaarDetectObjects( img, cascade, storage,
1.1, 2, CV_HAAR_DO_CANNY_PRUNING,
cvSize(40, 40) );
// Loop the number of faces found.
for( i = 0; i < (faces ? faces->total : 0); i++ )
{
// Create a new rectangle for drawing the face
CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
// Find the dimensions of the face,and scale it if necessary
pt1.x = r->x*scale;
pt2.x = (r->x+r->width)*scale;
pt1.y = r->y*scale;
pt2.y = (r->y+r->height)*scale;
// Draw the rectangle in the input image
cvRectangle( img, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 );
}
}
// Show the image in the window named "result"
cvShowImage( "result", img );
// Release the temp image created.
cvReleaseImage( &temp );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -