·代码如下:
// face_recog_from_picture.cpp: 定义控制台应用程序的入口点。 #include<opencv2/opencv.hpp> #include<iostream> using namespace std; using namespace cv; int main(int argc,char *argv[]) { Mat img = imread("she.jpg"); namedWindow("display"); imshow("display", img); /*********************************** 1.加载人脸检测器 ******************************/ // 建立级联分类器 CascadeClassifier cascade; // 加载训练好的 人脸检测器(.xml) const string path = "./xml/haarcascade_frontalface_alt.xml"; if ( ! cascade.load(path)) { cout << "cascade load failed!\n"; } //计时 double t = 0; t = (double)getTickCount(); /*********************************** 2.人脸检测 ******************************/ vector<Rect> faces(0); cascade.detectMultiScale(img, faces, 1.1, 5, 0 ,Size(30,30)); cout << "detect face number is :" << faces.size() << endl; /******************************** 3.显示人脸矩形框 ******************************/ if (faces.size() > 0) { for (size_t i = 0;i < faces.size();i++) { rectangle(img, faces[i], Scalar(150, 0, 0), 3, 8, 0); } } else cout << "未检测到人脸" << endl; t = (double)getTickCount() - t; //getTickCount(): Returns the number of ticks per second. cout << "检测人脸用时:" << t * 1000 / getTickFrequency() << "ms (不计算加载模型的时间)" << endl; namedWindow("face_detect"); double scale = 0.8; Size dsize = Size(img.cols*scale, img.rows*scale); Mat img2 = Mat(dsize, CV_32S); resize(img, img2, dsize); imshow("face_detect", img2); while(waitKey(0)!='k') ; destroyWindow("display"); destroyWindow("face_detect"); return 0; }
·执行如下:
网络异常,图片无法展示
|