引导滤波/导向滤波原理与实现

本文介绍了一种图像处理技术——引导滤波,并提供了一个使用Python实现的示例代码。该算法由何凯明等人于2010年提出,通过调整图像的局部特征来改善图像质量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引导滤波/导向滤波是何凯明等人在2010年ECCV上提出来的,文章为《Guided Image Filtering》。

import cv2
import numpy as np

input_fn=r"E:\study\python\python_code\code\CV\Guide_filter\pic\beach.jpg"

def my_guidedFilter_oneChannel(srcImg,guidedImg,rad=9,eps=0.01):
    
    srcImg=srcImg/255.0
    guidedImg=guidedImg/255.0
    img_shape=np.shape(srcImg)
    
    P_mean=cv2.boxFilter(srcImg, -1, (rad, rad), normalize=True) 
    I_mean=cv2.boxFilter(guidedImg,-1, (rad, rad), normalize=True) 
    
    I_square_mean=cv2.boxFilter(np.multiply(guidedImg,guidedImg), -1, (rad, rad), normalize=True) 
    I_mul_P_mean=cv2.boxFilter(np.multiply(srcImg,guidedImg), -1, (rad, rad), normalize=True)
    
    var_I=I_square_mean-np.multiply(I_mean,I_mean)
    cov_I_P=I_mul_P_mean-np.multiply(I_mean,P_mean)
    
    a=cov_I_P/(var_I+eps)
    b=P_mean-np.multiply(a,I_mean)
    
    a_mean=cv2.boxFilter(a, -1, (rad, rad), normalize=True) 
    b_mean=cv2.boxFilter(b, -1, (rad, rad), normalize=True) 
    
    dstImg=np.multiply(a_mean,guidedImg)+b_mean
    
    return dstImg*255.0
    

def my_guidedFilter_threeChannel(srcImg,guidedImg,rad=9,eps=0.01):
    
    img_shape=np.shape(srcImg)

    dstImg=np.zeros(img_shape,dtype=float)

    for ind in range(0,img_shape[2]):
        dstImg[:,:,ind]=my_guidedFilter_oneChannel(srcImg[:,:,ind],
              guidedImg[:,:,ind],rad,eps)
    
    dstImg=dstImg.astype(np.uint8)
    
    return dstImg


def main():
    img=cv2.imread(input_fn)
    print(np.shape(img))

    dstimg=my_guidedFilter_threeChannel(img,img,9,0.01)
    print(np.shape(dstimg))
    cv2.imwrite('output.jpg',dstimg)
    cv2.imshow('output',dstimg)
    cv2.waitKey(0)
    
if __name__ == '__main__':
    main()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值