string xmlPath="D:\opencv\data\haarcascades\haarcascade_frontalface_default.xml"; CascadeClassifier ccf; //创建分类器对象 Mat img=imread("d://timg.jpg"); if(!ccf.load(xmlPath)) //加载训练文件 { printf("不能加载指定的xml文件"); return 0; } vector<Rect> faces; //创建一个容器保存检测出来的脸 Mat gray; cvtColor(img,gray,CV_BGR2GRAY); //转换成灰度图,因为harr特征从灰度图中提取 equalizeHist(gray,gray); //直方图均衡行 ccf.detectMultiScale(gray,faces,1.1,3,0,Size(10,10),Size(100,100)); //检测人脸 for(vector<Rect>::const_iterator iter=faces.begin();iter!=faces.end();iter++) { rectangle(img,*iter,Scalar(0,0,255),2,8); //画出脸部矩形 } imshow("faces",img); waitKey(0); return 0;