opencv人脸检测+美颜

人脸检测依靠opencv中的cascadeClassfier级联分类器,opencv有训练好的模型使用。

美颜原理:先进行高斯模糊滤波,

双边滤波  bilateralBlur

再次高斯模糊

最后addweight

见代码注释

#include<opencv2/opencv.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/objdetect/objdetect.hpp>
using namespace std;
using namespace cv;

int main()
{
	//VideoCapture capture(0);
	string face_cascade_name = "D:\\opencv3.4.1_source\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt2.xml";

	CascadeClassifier face_cascade;
	Mat frame;
	if (!face_cascade.load(face_cascade_name))
	{
		cout << "load failed" << endl;
		return 0;
	}
	
	frame = imread("1.jpg");
	//capture >> frame;
	//waitKey(3);
	vector<Rect> faces;
	Mat frame_gray;
	vector<Mat> rois;
	cvtColor(frame, frame_gray,COLOR_BGR2GRAY);
	equalizeHist(frame_gray, frame_gray);
	face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));
	for (auto iter = faces.begin(); iter != faces.end(); iter++)
	{
		rectangle(frame, *iter, Scalar(0, 0, 255), 2);
		//rois.push_back(frame(*iter).clone());
		rois.push_back(frame(*iter));
	}
	for (auto iter = rois.begin(); iter != rois.end(); iter++)
	{
		int rows = iter->rows;
		int cols = iter->cols*iter->channels();
		for (int i = 0; i < rows; i++)
		{
			uchar* data = iter->ptr<uchar>(i);
			for (int j = 0; j < cols; j++)
			{
				data[j] = saturate_cast<uchar>(data[j] * 1.1 + 68);
			}
		}
	}
	for (int i = 0; i < rois.size(); i++)
	{
		Mat roi;
		GaussianBlur(rois[i], roi, Size(5, 5), 0,0);
		Mat roi1;
		bilateralFilter(roi, roi1, 30, 30 * 2, 30 / 2);  //双边滤波
		Mat matFinal;
		GaussianBlur(roi1, matFinal, Size(0, 0), 9);
		addWeighted(roi1, 1.5, matFinal, -0.5, 0, rois[i]);   //这一步相当于非掩膜锐化
		string win_name = "faces_" + to_string(i + 1);
		imshow(win_name, rois[i]);
	}
	imshow("source", frame);
	waitKey(0);
	return 0;
;
	waitKey(0);
	return 0;
}

效果如下:

阅读更多
换一批

没有更多推荐了,返回首页