opencv使用 --- fastGlobalSmootherFilter
0. 背景
做人脸数据集处理的时候,需要做光照合成,在 Face Illumination Transfer through Edge-preserving Filters [1] 里介绍了一种光照迁移方法,需要用到 WSL [2] edge-preserving Filters。
1. opencv 接口
opencv有WSL原理的改进版FGS[3], 具体接口[4]形式:
1 void cv::ximgproc::fastGlobalSmootherFilter ( InputArray guide, 2 InputArray src, 3 OutputArray dst, 4 double lambda, 5 double sigma_color, 6 double lambda_attenuation = 0.25, 7 int num_iter = 3 8 )
# Python: dst = cv.ximgproc.fastGlobalSmootherFilter( guide, src, lambda, sigma_color[, dst[, lambda_attenuation[, num_iter]]] )
2. 使用方法
需要先配置 opencv-contrib包,C++的配置没有具体尝试,python需要 pip install opencv-contrib-python 才能 import cv2.ximgproc
C++使用参考源码的 fgs_test
1 TEST(FastGlobalSmootherTest, SplatSurfaceAccuracy) 2 { 3 RNG rnd(0); 4 5 for (int i = 0; i < 5; i++) 6 { 7 Size sz(rnd.uniform(512, 1024), rnd.uniform(512, 1024)); 8 9 int guideCn = rnd.uniform(1, 2); 10 if(guideCn==2) guideCn++; //1 or 3 channels 11 Mat guide(sz, CV_MAKE_TYPE(CV_8U, guideCn)); 12 randu(guide, 0, 255); 13 14 Scalar surfaceValue; 15 int srcCn = rnd.uniform(1, 4); 16 rnd.fill(surfaceValue, RNG::UNIFORM, 0, 255); 17 Mat src(sz, CV_MAKE_TYPE(CV_16S, srcCn), surfaceValue); 18 19 double lambda = rnd.uniform(100, 10000); 20 double sigma = rnd.uniform(1.0, 100.0); 21 22 Mat res; 23 fastGlobalSmootherFilter(guide, src, res, lambda, sigma); 24 25 // When filtering a constant image we should get the same image: 26 double normL1 = cvtest::norm(src, res, NORM_L1)/src.total()/src.channels(); 27 EXPECT_LE(normL1, 1.0/64); 28 } 29 }
python 方法
import cv2 from cv2.ximgproc import * img = cv2.imread('test.jpg') imgSmooth = fastGlobalSmootherFilter(img, img, 625.0, 20)
注: guide 可以用原图或者灰度图,其他类型没有尝试
在python中 lambda是关键字,所以不能用 lambda=625.0这样的传参方式
sigma_color 参数取值 1.0~100, 低于1.0 看不出效果(跟论文里的参数设置情况不太一样)
参考:
[1] X. Chen, M. Chen, X. Jin, and Q. Zhao, “Face illumination transfer through edge-preserving filters,” in Proc. CVPR, 2011, pp. 281–287.
[2] Z. Farbman, R. Fattal, D. Lischinski, and R. Szeliski, “Edge-preserving decompositions for multi-scale tone and detail manipulation,” ACM Transactions on Graphics (Proc. SIGGRAPH), vol. 27, no. 3, Aug. 2008.
[3] Dongbo Min, Sunghwan Choi, Jiangbo Lu, Bumsub Ham, Kwanghoon Sohn, and Minh N Do. Fast global image smoothing based on weighted least squares. Image Processing, IEEE Transactions on, 23(12):5638–5653, 2014.
[4] OpenCV-3.4.4