使用OPENCV简单实现具有肤质保留功能的磨皮增白算法

在一个美颜高手那里发现一个美颜算法,他写出了数学表达式,没有给出代码,正好在研究OPENCV,顺手实现之。具体过程就是一系列矩阵运算,据说是从一个PS高手那里研究 出来的,一并表示感谢。

这是数学表达式:

Dest =(Src * (100 - Opacity) + (Src + 2 * GuassBlur(EPFFilter(Src) - Src + 128) - 256) * Opacity) /100 ;

	public static Mat face2(Mat image) {
		Mat dst = new Mat();

		// int value1 = 3, value2 = 1; 磨皮程度与细节程度的确定
		int value1 = 3, value2 = 1; 
		int dx = value1 * 5; // 双边滤波参数之一
		double fc = value1 * 12.5; // 双边滤波参数之一
		double p = 0.1f; // 透明度
		Mat temp1 = new Mat(), temp2 = new Mat(), temp3 = new Mat(), temp4 = new Mat();

		// 双边滤波
		Imgproc.bilateralFilter(image, temp1, dx, fc, fc);

		// temp2 = (temp1 - image + 128);
		Mat temp22 = new Mat();
		Core.subtract(temp1, image, temp22);
		// Core.subtract(temp22, new Scalar(128), temp2);
		Core.add(temp22, new Scalar(128, 128, 128, 128), temp2);
		// 高斯模糊
		Imgproc.GaussianBlur(temp2, temp3, new Size(2 * value2 - 1, 2 * value2 - 1), 0, 0);

		// temp4 = image + 2 * temp3 - 255;
		Mat temp44 = new Mat();
		temp3.convertTo(temp44, temp3.type(), 2, -255);
		Core.add(image, temp44, temp4);
		// dst = (image*(100 - p) + temp4*p) / 100;
		Core.addWeighted(image, p, temp4, 1 - p, 0.0, dst);
		
		Core.add(dst, new Scalar(10, 10, 10), dst);
		return dst;

	}
	      

测试代码:

 Mat src2 = Imgcodecs.imread("E:/work/qqq/e.jpg");
         Mat src3 = face2(src2);
       
         Mat dest = new Mat(new Size(src2.cols()+src3.cols(), src2.rows()), src2.type());
         Mat temp1 = dest.colRange(0, src2.cols());
         Mat temp2 = dest.colRange(src2.cols(), dest.cols());
         src2.copyTo(temp1);
         src3.copyTo(temp2);
	Imgcodecs.imwrite("E:/work/qqq/z3.jpg",dest);	


参考:

http://www.cnblogs.com/Imageshop/p/4709710.html

http://www.cnblogs.com/Imageshop/p/3871237.html

猜你喜欢

转载自blog.csdn.net/blogercn/article/details/77949949