#include "stdafx.h"
#include
#include
#include
using namespace cv;
using namespace std;
//第一部分
//int main()
//{
// VideoCapture cap(0);
// Mat frame;
// int i = 1;
// while (1)
// {
// char key = waitKey(100);
// cap >> frame;
// Point w1 = Point(0 , frame.rows/4*3 );
// Point w2 = Point(frame.cols, frame.rows / 4*3);
// Point w3 = Point(frame.cols/2, 0);
// Point w4 = Point(frame.cols / 2, frame.rows);
// Point w5 = Point(frame.cols/2, frame.rows/2);
//
// // rectangle(frame, , boundRect[i].br(), Scalar(0, 255, 255), 1, 8, 0);
// line(frame,w1,w2, Scalar(0, 0, 0), 1, 8, 0);
// line(frame, w3, w4, Scalar(0, 0, 255), 1, 8, 0);
// circle(frame, w5, 3, Scalar(255, 0, 0), -1, 8);
// imshow("frame", frame);
// string filename = format("D:\pic\3\pic%d.jpg", i);
//
// switch (key)
// {
// case'p':
// i++;
// imwrite(filename, frame);
// imshow("photo", frame);
// waitKey(500);
// destroyWindow("photo");
// break;
// default:
// break;
// }
// }
//}
//第二部分
CascadeClassifier face_cascade;
std::vector faces;
string face_cascade_name = "haarcascade_frontalface_alt2.xml";
int main(){
Mat img_gray,img;
img = imread("C:\Users\lbn\Desktop\1.bmp");
//img = imread("D:\pic\pic7.jpg");
cvtColor(img, img_gray, COLOR_BGR2GRAY);
equalizeHist(img_gray, img_gray);
int i =7;
//-- Detect faces
//cv::createFaceDetectionMaskGenerator
if (!face_cascade.load(face_cascade_name)) {
printf("级联分类器错误,可能未找到文件,拷贝该文件到工程目录下!
");
return -1;
}
//face_cascade.detectMultiScale(img_gray, faces, 1.1, 3, CV_HAAR_DO_ROUGH_SEARCH, Size(50, 50));
face_cascade.detectMultiScale(img_gray, faces, 1.1, 3, CV_HAAR_DO_ROUGH_SEARCH);
for (size_t j = 0; j < faces.size(); j++)
{
Mat faceROI = img(faces[j]);
Mat MyFace;
if (faceROI.cols > 100)
{
resize(faceROI, MyFace, Size(92, 112));
/* string filename = format("D:\pic\pic%d.jpg", i);*/
string str = format("C:UserslbnDesktop\%d.bmp",j);
// string str = format("D:\pic\oic%d.jpg", i);
imwrite(str, MyFace);
imshow("ii", MyFace);
}
Mat face;
Point center(int(faces[j].x + faces[j].width*0.5), int(faces[j].y + faces[j].height*0.5));
ellipse(img, center, Size(int(faces[j].width*0.5), int(faces[j].height*0.5)), 0, 0, 360, Scalar(255, 0, 0), 4, 8, 0);
imshow("人脸识别", img); waitKey();
}
}