参考:引导图滤波(Guided Image Filtering)原理以及OpenCV实现
/**
* @description:引导滤波
* @param I 引导图像
* @param P 输入图像
* @param radius滤波器半径
* @param eps 惩罚因子
* @return 输出图像
*/
cv::Mat GuidedFilter(cv::Mat I, cv::Mat P, int radius, float eps)
{
cv::Size size(2 * radius + 1, 2 * radius + 1);
//求I*I, I*P
cv::Mat I2 = I.mul(I);
cv::Mat IP = I.mul(P);
//求均值
cv::Mat meanI, meanP, meanI2, meanIP;
cv::boxFilter(I, meanI, -1, size);
cv::boxFilter(P, meanP, -1, size);
cv::boxFilter(I2, meanI2, -1, size);
cv::boxFilter(IP, meanIP, -1, size);
//求方差、协方差
cv::Mat varI, covIP;
varI = meanI2 - meanI.mul(meanI);
covIP = meanIP - meanI.mul(meanP);
//求系数a, b
cv::Mat a, b;
cv::divide(covIP, varI + eps, a);
b = meanP - a.mul(meanI);
//a、b窗口内求平均
cv::Mat meanA, meanB;
cv::boxFilter(a, meanA, -1, size);
cv::boxFilter(b, meanB, -1, size);
//输出
cv::Mat output = meanA.mul(I) + meanB;
return output;
}