OpenCV的PCA人脸识别

2019-04-15 15:08发布

在工作目录下建立/pic文件夹放入测试图片,建立/positive文件夹,放入自己的训练数据(我使用的是自己的相片中截获好的头像)建立list.txt,在里面写入pic/文件名以回车隔开,即可。 #define CV_NO_BACKWARD_COMPATIBILITY #include "cv.h" #include "highgui.h" #include #include #include #ifdef _EiC #define WIN32 #endif using namespace std; using namespace cv; void detectAndDraw( Mat& img, CascadeClassifier& cascade, CascadeClassifier& nestedCascade, double scale,int picid); int compressPCA(const Mat& pcaset, int maxComponents, const Mat& testset, Mat& compressed); String cascadeName ="haarcascade_frontalface_alt.xml"; String nestedCascadeName ="haarcascade_eye_tree_eyeglasses.xml"; int WINDOWID=1; int main( int argc, const char** argv ) { Mat frame, frameCopy, image; const String scaleOpt = "--scale="; size_t scaleOptLen = scaleOpt.length(); const String cascadeOpt = "--cascade="; size_t cascadeOptLen = cascadeOpt.length(); const String nestedCascadeOpt = "--nested-cascade"; size_t nestedCascadeOptLen = nestedCascadeOpt.length(); String inputName; CascadeClassifier cascade, nestedCascade; double scale = 1; for( int i = 1; i < argc; i++ ) { if( cascadeOpt.compare( 0, cascadeOptLen, argv[i], cascadeOptLen ) == 0 ) cascadeName.assign( argv[i] + cascadeOptLen ); else if( nestedCascadeOpt.compare( 0, nestedCascadeOptLen, argv[i], nestedCascadeOptLen ) == 0 ) { if( argv[i][nestedCascadeOpt.length()] == '=' ) nestedCascadeName.assign( argv[i] + nestedCascadeOpt.length() + 1 ); if( !nestedCascade.load( nestedCascadeName ) ) cerr << "WARNING: Could not load classifier cascade for nested objects" << endl; } else if( scaleOpt.compare( 0, scaleOptLen, argv[i], scaleOptLen ) == 0 ) { if( !sscanf( argv[i] + scaleOpt.length(), "%lf", &scale ) || scale < 1 ) scale = 1; } else if( argv[i][0] == '-' ) { cerr << "WARNING: Unknown option %s" << argv[i] << endl; } else inputName.assign( argv[i] ); } if( !cascade.load( cascadeName ) ) { cerr << "ERROR: Could not load classifier cascade" << endl; cerr << "Usage: facedetect [--cascade=""] " " [--nested-cascade[="nested_cascade_path"]] " " [--scale[= " " [filename|camera_index] " ; return -1; } image = imread( inputName, 1 ); if( !image.empty() ) { detectAndDraw( image, cascade, nestedCascade, scale,0 ); waitKey(0); } else if( !inputName.empty() ) { FILE* f = fopen( inputName.c_str(), "rt" ); if( f ) { char buf[1000+1]; int picid=1; printf("################################################################ "); printf(" "); while( fgets( buf, 1000, f ) ) { int len = (int)strlen(buf), c; while( len > 0 && isspace(buf[len-1]) ) len--; buf[len] = '