赞
踩
opencv是开源的计算机视觉库,实现人脸检测我们使用的是opencv库中帮我们训练好的检测器,一般在D:\opencv\sources\data\haarcascades目录下有各种检测器。
可以根据各种要求选择不同的训练器,我是结合了官网中的demo检测了人脸和人眼。代码有详细的注释,当然我还添加了一部分对图像的处理函数,有兴趣的可以了解一下。
各种函数参数都可以取opencv官网查看。
我使用的是opencv4.11和vs2017.
- #include<opencv2/opencv.hpp>
- #include<iostream>
-
- using namespace cv;
- using namespace std;
-
- //识别人眼
- void eyeDetect(Mat& image,Mat& Frame,vector<Rect> Eyes,CascadeClassifier& EyeDetector,double s)
- {
- EyeDetector.detectMultiScale(image, Eyes, 1.1, 3, 0, Size(0, 0));
-
- for (int i = 0; i < Eyes.size(); i++)
- {
-
-
- Mat roi = Frame(Rect(Eyes[i].x*s,Eyes[i].y*s,Eyes[i].width*s,Eyes[i].height*s));
- resize(roi, roi, Size(Eyes[i].width*s * 1.2, Eyes[i].height *s* 1.2));
- //Mat ROI = Frame(Rect((Eyes[i].x*s - (roi.cols-Eyes[i].width*s)/2), (Eyes[i].y*s - (roi.rows - Eyes[i].height*s) / 2),roi.cols,roi.rows));
- //Mat p = imread("2.jpg",0);
- //roi.copyTo(ROI);
- //生成矩形框,颜色为绿色
- rectangle(Frame, Point(Eyes[i].x, Eyes[i].y)*s, Point(Eyes[i].x + Eyes[i].width, Eyes[i].y + Eyes[i].height)*s, Scalar(0, 255, 0), 1, 8);
-
- }
-
- }
- //对暂时保存的图像进行亮度的调节
- void beautifyFace(Mat& matSelfPhoto, float alpha, float beta)
- {
- for (int y = 0; y < matSelfPhoto.rows; y++)
- {
- for (int x = 0; x < matSelfPhoto.cols; x++)
- {
- for (int c = 0; c < 3; c++)
- {
- matSelfPhoto.at<Vec3b>(y, x)[c] = saturate_cast<uchar>(alpha*(matSelfPhoto.at<Vec3b>(y, x)[c]) + beta);
- }
- }
- }
- }
- //人脸识别函数
- void facedetected(Mat & IMG, Mat& Frame,vector<Rect>& faces1, CascadeClassifier& facedetect, double &Scale) {
-
-
- //人脸检测
- facedetect.detectMultiScale(IMG, faces1, 1.1, 3, 0, Size(0, 0));
-
-
- for (int i = 0; i < faces1.size(); i++)
- {
- Rect rect = faces1[i];
-
- //生成矩形框,颜色为绿色
-
- rectangle(Frame, Point(faces1[i].x, faces1[i].y)*Scale, Point(faces1[i].x + faces1[i].width, faces1[i].y + faces1[i].height)*Scale, Scalar(0, 255, 0), 1, 8);
-
- //设置矩形区域并将人脸设立成为感兴趣区域,并传入函数对所选取区域进行美颜
-
- Mat imageROI = Frame(Rect(faces1[i].x*Scale, faces1[i].y*Scale, faces1[i].width*Scale, faces1[i].height*Scale));
-
-
- //磨皮处理
- //bilateralFilter(imageROI, a, 30, 30 * 2, 30 / 2);
- }
- }
-
-
- int main(int argc, const char** argv) {
- CascadeClassifier faceDector,eyesDector;
- eyesDector.load("D:/opencv/sources/data/haarcascades/haarcascade_eye.xml");
- faceDector.load("D:/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml");
- double scale = 4;
- //打开摄像头
- /*VideoCapture capture(0);
- //cout << capture.get(CAP_PROP_FPS) << endl;
- if (!capture.isOpened()) {
- cout << "摄像头打开失败" << endl;
- return -1;
- }*/
-
-
- Mat img, a;
- vector<Rect> faces;
- vector<Rect> eyes;
- Mat frame0 = imread("1.jpg");
- imshow("1", frame0);
- Mat frame = frame0;
- if (frame.empty()) {
- cout << "打开失败";
- return -1;
- }
-
- //转成灰度图
- cvtColor(frame, img, COLOR_RGB2GRAY);
- //imshow("22222", img);
- //缩放图像提高效率
- resize(img, img, Size(), 1 / scale, 1 / scale, INTER_LINEAR_EXACT);
-
- //直方图均衡化
- equalizeHist(img, img);
-
-
- eyeDetect(img, frame, eyes, eyesDector, scale);
- facedetected(img,frame, faces, faceDector, scale);
- //调用亮度调节函数
- //beautifyFace(frame, 1.1, 68);
-
- //高斯模糊
- //GaussianBlur(frame, frame, Size(9, 9), 0, 0);
-
- cv::imshow("2", frame);
- waitKey(0);
- return 0;
-
- }
代码效果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。