使用OPENCV簡單實現具有膚質保留功能的磨皮增白演算法
阿新 • • 發佈:2018-12-22
在一個美顏高手那裡發現一個美顏演算法,他寫出了數學表示式,沒有給出程式碼,正好在研究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