双边滤波

在这里插入图片描述

#空间距离权重模板
import numpy as np
from scipy import signal
import cv2,math
def getClosenessWeight(sigma_g,H,W):
    r,c = np.mgrid[0:H:1,0:W:1]
    r=r.astype(np.float64)
    c=c.astype(np.float64)
    print(r)
    print(c)
    r-=(H-1)/2
    
    print(r)
    c-=(W-1)/2
    print(c)
    closeWeight = np.exp(-0.5*(np.power(r,2)+np.power(c,2))/math.pow(sigma_g,2))
    return closeWeight
if __name__ =='__main__':
    a = getClosenessWeight(2,5,5)
    print(a)

结果
[[0. 0. 0. 0. 0.]
[1. 1. 1. 1. 1.]
[2. 2. 2. 2. 2.]
[3. 3. 3. 3. 3.]
[4. 4. 4. 4. 4.]]
[[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]]
[[-2. -2. -2. -2. -2.]
[-1. -1. -1. -1. -1.]
[ 0. 0. 0. 0. 0.]
[ 1. 1. 1. 1. 1.]
[ 2. 2. 2. 2. 2.]]
[[-2. -1. 0. 1. 2.]
[-2. -1. 0. 1. 2.]
[-2. -1. 0. 1. 2.]
[-2. -1. 0. 1. 2.]
[-2. -1. 0. 1. 2.]]
[[0.36787944 0.53526143 0.60653066 0.53526143 0.36787944]
[0.53526143 0.77880078 0.8824969 0.77880078 0.53526143]
[0.60653066 0.8824969 1. 0.8824969 0.60653066]
[0.53526143 0.77880078 0.8824969 0.77880078 0.53526143]
[0.36787944 0.53526143 0.60653066 0.53526143 0.36787944]]

数组练习

import numpy as np
from scipy import signal
import cv2,math
def getClosenessWeight(sigma_g,H,W):
    r,c = np.mgrid[0:H:1,0:W:1]
    r=r.astype(np.float64)
    c=c.astype(np.float64)
    r-=(H-1)/2
    c-=(W-1)/2
    closeWeight = np.exp(-0.5*(np.power(r,2)+np.power(c,2))/math.pow(sigma_g,2))
    return closeWeight
def bfltGray(I,H,W,sigma_g,sigma_d):
    #I图像矩阵【0,1】
    #H,W模板
    #sigma_g 距离权重 >1
    #sigma_d 近似权重 <1
    #距离权重
    closenessWeight = getClosenessWeight(sigma_g,H,W)
    #模板中心位置
    cH = (H-1)/2
    cW = (W-1)/2
    rows,cols = I.shape
    bfltGrayImage = np.zeros(I.shape,np.float32)
    for r in range(rows):
        for c in range(cols):
            pixel = I[r][c]
            #判断边界
            rTop = 0 if r-cH<0 else r-cH
            rBottom = rows-1 if r+cH>rows-1 else r+cH
            cLeft = 0 if c-cW<0 else c-cW
            cRight = cols-1 if c+cW>cols-1 else c+cW
            print(rTop,rBottom,cLeft,cRight)
            #区域
            region = I[int(rTop):int(rBottom)+1,int(cLeft):int(cRight)+1]
            print(region)
            #相似性权重
            similarityWeightTemp = np.exp(-0.5*np.power(region-pixel,2.0)/math.pow
                                          (sigma_d,2))
            closenessWeightTemp = closenessWeight[int(rTop-r+cH):int(rBottom-r+cH+1),
                            int(cLeft-c+cW):int(cRight-c+cW+1)]
            print(rTop-r+cH,rBottom-r+cH+1,cLeft-c+cW,cRight-c+cW+1)
            print(closenessWeightTemp)
            #两个模板相乘
            weightTemp = similarityWeightTemp*closenessWeightTemp
            #归一化权重模板
            weightTemp = weightTemp/np.sum(weightTemp)
            bfltGrayImage[r][c] = np.sum(region*weightTemp)

if __name__ =='__main__':
    #image = cv2.imread('E:\\sy2\\5\\img6.jpg',cv2.IMREAD_GRAYSCALE)
    I = np.array([[1,2,3,4,5,6,7],
                      [1,2,3,4,5,6,7],
                      [1,2,3,4,5,6,7],
                      [1,2,3,4,5,6,7],
                      [1,2,3,4,5,6,7],
                      [1,2,3,4,5,6,7],
                      [1,2,3,4,5,6,7]],np.uint8)
    medianBlurImage = bfltGray(I,3,3,2,2)
    
'''

结果

0 1.0 0 1.0
[[1 2]
 [1 2]]
1.0 3.0 1.0 3.0
[[1.         0.8824969 ]
 [0.8824969  0.77880078]]
0 1.0 0.0 2.0
[[1 2 3]
 [1 2 3]]
1.0 3.0 0.0 3.0
[[0.8824969  1.         0.8824969 ]
 [0.77880078 0.8824969  0.77880078]]
0 1.0 1.0 3.0
[[2 3 4]
 [2 3 4]]
1.0 3.0 0.0 3.0
[[0.8824969  1.         0.8824969 ]
 [0.77880078 0.8824969  0.77880078]]
0 1.0 2.0 4.0
[[3 4 5]
 [3 4 5]]
1.0 3.0 0.0 3.0
[[0.8824969  1.         0.8824969 ]
 [0.77880078 0.8824969  0.77880078]]
0 1.0 3.0 5.0
[[4 5 6]
 [4 5 6]]
1.0 3.0 0.0 3.0
[[0.8824969  1.         0.8824969 ]
 [0.77880078 0.8824969  0.77880078]]
0 1.0 4.0 6.0
[[5 6 7]
 [5 6 7]]
1.0 3.0 0.0 3.0
[[0.8824969  1.         0.8824969 ]
 [0.77880078 0.8824969  0.77880078]]
0 1.0 5.0 6
[[6 7]
 [6 7]]
1.0 3.0 0.0 2.0
[[0.8824969  1.        ]
 [0.77880078 0.8824969 ]]
0.0 2.0 0 1.0
[[1 2]
 [1 2]
 [1 2]]
0.0 3.0 1.0 3.0
[[0.8824969  0.77880078]
 [1.         0.8824969 ]
 [0.8824969  0.77880078]]
0.0 2.0 0.0 2.0
[[1 2 3]
 [1 2 3]
 [1 2 3]]
0.0 3.0 0.0 3.0
[[0.77880078 0.8824969  0.77880078]
 [0.8824969  1.         0.8824969 ]
 [0.77880078 0.8824969  0.77880078]]
0.0 2.0 1.0 3.0
[[2 3 4]
 [2 3 4]
 [2 3 4]]
0.0 3.0 0.0 3.0
[[0.77880078 0.8824969  0.77880078]
 [0.8824969  1.         0.8824969 ]
 [0.77880078 0.8824969  0.77880078]]
0.0 2.0 2.0 4.0
[[3 4 5]
 [3 4 5]
 [3 4 5]]
0.0 3.0 0.0 3.0
[[0.77880078 0.8824969  0.77880078]
 [0.8824969  1.         0.8824969 ]
 [0.77880078 0.8824969  0.77880078]]
0.0 2.0 3.0 5.0
[[4 5 6]
 [4 5 6]
 [4 5 6]]
0.0 3.0 0.0 3.0
[[0.77880078 0.8824969  0.77880078]
 [0.8824969  1.         0.8824969 ]
 [0.77880078 0.8824969  0.77880078]]
0.0 2.0 4.0 6.0
[[5 6 7]
 [5 6 7]
 [5 6 7]]
0.0 3.0 0.0 3.0
[[0.77880078 0.8824969  0.77880078]
 [0.8824969  1.         0.8824969 ]
 [0.77880078 0.8824969  0.77880078]]
0.0 2.0 5.0 6
[[6 7]
 [6 7]
 [6 7]]
0.0 3.0 0.0 2.0
[[0.77880078 0.8824969 ]
 [0.8824969  1.        ]
 [0.77880078 0.8824969 ]]
1.0 3.0 0 1.0
[[1 2]
 [1 2]
 [1 2]]
0.0 3.0 1.0 3.0
[[0.8824969  0.77880078]
 [1.         0.8824969 ]
 [0.8824969  0.77880078]]
1.0 3.0 0.0 2.0
[[1 2 3]
 [1 2 3]
 [1 2 3]]
0.0 3.0 0.0 3.0
[[0.77880078 0.8824969  0.77880078]
 [0.8824969  1.         0.8824969 ]
 [0.77880078 0.8824969  0.77880078]]
1.0 3.0 1.0 3.0
[[2 3 4]
 [2 3 4]
 [2 3 4]]
0.0 3.0 0.0 3.0
[[0.77880078 0.8824969  0.77880078]
 [0.8824969  1.         0.8824969 ]
 [0.77880078 0.8824969  0.77880078]]
1.0 3.0 2.0 4.0
[[3 4 5]
 [3 4 5]
 [3 4 5]]
0.0 3.0 0.0 3.0
[[0.77880078 0.8824969  0.77880078]
 [0.8824969  1.         0.8824969 ]
 [0.77880078 0.8824969  0.77880078]]
1.0 3.0 3.0 5.0
[[4 5 6]
 [4 5 6]
 [4 5 6]]
0.0 3.0 0.0 3.0
[[0.77880078 0.8824969  0.77880078]
 [0.8824969  1.         0.8824969 ]
 [0.77880078 0.8824969  0.77880078]]
1.0 3.0 4.0 6.0
[[5 6 7]
 [5 6 7]
 [5 6 7]]
0.0 3.0 0.0 3.0
[[0.77880078 0.8824969  0.77880078]
 [0.8824969  1.         0.8824969 ]
 [0.77880078 0.8824969  0.77880078]]
1.0 3.0 5.0 6
[[6 7]
 [6 7]
 [6 7]]
0.0 3.0 0.0 2.0
[[0.77880078 0.8824969 ]
 [0.8824969  1.        ]
 [0.77880078 0.8824969 ]]
2.0 4.0 0 1.0
[[1 2]
 [1 2]
 [1 2]]
0.0 3.0 1.0 3.0
[[0.8824969  0.77880078]
 [1.         0.8824969 ]
 [0.8824969  0.77880078]]
2.0 4.0 0.0 2.0
[[1 2 3]
 [1 2 3]
 [1 2 3]]
0.0 3.0 0.0 3.0
[[0.77880078 0.8824969  0.77880078]
 [0.8824969  1.         0.8824969 ]
 [0.77880078 0.8824969  0.77880078]]
2.0 4.0 1.0 3.0
[[2 3 4]
 [2 3 4]
 [2 3 4]]
0.0 3.0 0.0 3.0
[[0.77880078 0.8824969  0.77880078]
 [0.8824969  1.         0.8824969 ]
 [0.77880078 0.8824969  0.77880078]]
2.0 4.0 2.0 4.0
[[3 4 5]
 [3 4 5]
 [3 4 5]]
0.0 3.0 0.0 3.0
[[0.77880078 0.8824969  0.77880078]
 [0.8824969  1.         0.8824969 ]
 [0.77880078 0.8824969  0.77880078]]
2.0 4.0 3.0 5.0
[[4 5 6]
 [4 5 6]
 [4 5 6]]
0.0 3.0 0.0 3.0
[[0.77880078 0.8824969  0.77880078]
 [0.8824969  1.         0.8824969 ]
 [0.77880078 0.8824969  0.77880078]]
2.0 4.0 4.0 6.0
[[5 6 7]
 [5 6 7]
 [5 6 7]]
0.0 3.0 0.0 3.0
[[0.77880078 0.8824969  0.77880078]
 [0.8824969  1.         0.8824969 ]
 [0.77880078 0.8824969  0.77880078]]
2.0 4.0 5.0 6
[[6 7]
 [6 7]
 [6 7]]
0.0 3.0 0.0 2.0
[[0.77880078 0.8824969 ]
 [0.8824969  1.        ]
 [0.77880078 0.8824969 ]]
3.0 5.0 0 1.0
[[1 2]
 [1 2]
 [1 2]]
0.0 3.0 1.0 3.0
[[0.8824969  0.77880078]
 [1.         0.8824969 ]
 [0.8824969  0.77880078]]
3.0 5.0 0.0 2.0
[[1 2 3]
 [1 2 3]
 [1 2 3]]
0.0 3.0 0.0 3.0
[[0.77880078 0.8824969  0.77880078]
 [0.8824969  1.         0.8824969 ]
 [0.77880078 0.8824969  0.77880078]]
3.0 5.0 1.0 3.0
[[2 3 4]
 [2 3 4]
 [2 3 4]]
0.0 3.0 0.0 3.0
[[0.77880078 0.8824969  0.77880078]
 [0.8824969  1.         0.8824969 ]
 [0.77880078 0.8824969  0.77880078]]
3.0 5.0 2.0 4.0
[[3 4 5]
 [3 4 5]
 [3 4 5]]
0.0 3.0 0.0 3.0
[[0.77880078 0.8824969  0.77880078]
 [0.8824969  1.         0.8824969 ]
 [0.77880078 0.8824969  0.77880078]]
3.0 5.0 3.0 5.0
[[4 5 6]
 [4 5 6]
 [4 5 6]]
0.0 3.0 0.0 3.0
[[0.77880078 0.8824969  0.77880078]
 [0.8824969  1.         0.8824969 ]
 [0.77880078 0.8824969  0.77880078]]
3.0 5.0 4.0 6.0
[[5 6 7]
 [5 6 7]
 [5 6 7]]
0.0 3.0 0.0 3.0
[[0.77880078 0.8824969  0.77880078]
 [0.8824969  1.         0.8824969 ]
 [0.77880078 0.8824969  0.77880078]]
3.0 5.0 5.0 6
[[6 7]
 [6 7]
 [6 7]]
0.0 3.0 0.0 2.0
[[0.77880078 0.8824969 ]
 [0.8824969  1.        ]
 [0.77880078 0.8824969 ]]
4.0 6.0 0 1.0
[[1 2]
 [1 2]
 [1 2]]
0.0 3.0 1.0 3.0
[[0.8824969  0.77880078]
 [1.         0.8824969 ]
 [0.8824969  0.77880078]]
4.0 6.0 0.0 2.0
[[1 2 3]
 [1 2 3]
 [1 2 3]]
0.0 3.0 0.0 3.0
[[0.77880078 0.8824969  0.77880078]
 [0.8824969  1.         0.8824969 ]
 [0.77880078 0.8824969  0.77880078]]
4.0 6.0 1.0 3.0
[[2 3 4]
 [2 3 4]
 [2 3 4]]
0.0 3.0 0.0 3.0
[[0.77880078 0.8824969  0.77880078]
 [0.8824969  1.         0.8824969 ]
 [0.77880078 0.8824969  0.77880078]]
4.0 6.0 2.0 4.0
[[3 4 5]
 [3 4 5]
 [3 4 5]]
0.0 3.0 0.0 3.0
[[0.77880078 0.8824969  0.77880078]
 [0.8824969  1.         0.8824969 ]
 [0.77880078 0.8824969  0.77880078]]
4.0 6.0 3.0 5.0
[[4 5 6]
 [4 5 6]
 [4 5 6]]
0.0 3.0 0.0 3.0
[[0.77880078 0.8824969  0.77880078]
 [0.8824969  1.         0.8824969 ]
 [0.77880078 0.8824969  0.77880078]]
4.0 6.0 4.0 6.0
[[5 6 7]
 [5 6 7]
 [5 6 7]]
0.0 3.0 0.0 3.0
[[0.77880078 0.8824969  0.77880078]
 [0.8824969  1.         0.8824969 ]
 [0.77880078 0.8824969  0.77880078]]
4.0 6.0 5.0 6
[[6 7]
 [6 7]
 [6 7]]
0.0 3.0 0.0 2.0
[[0.77880078 0.8824969 ]
 [0.8824969  1.        ]
 [0.77880078 0.8824969 ]]
5.0 6 0 1.0
[[1 2]
 [1 2]]
0.0 2.0 1.0 3.0
[[0.8824969  0.77880078]
 [1.         0.8824969 ]]
5.0 6 0.0 2.0
[[1 2 3]
 [1 2 3]]
0.0 2.0 0.0 3.0
[[0.77880078 0.8824969  0.77880078]
 [0.8824969  1.         0.8824969 ]]
5.0 6 1.0 3.0
[[2 3 4]
 [2 3 4]]
0.0 2.0 0.0 3.0
[[0.77880078 0.8824969  0.77880078]
 [0.8824969  1.         0.8824969 ]]
5.0 6 2.0 4.0
[[3 4 5]
 [3 4 5]]
0.0 2.0 0.0 3.0
[[0.77880078 0.8824969  0.77880078]
 [0.8824969  1.         0.8824969 ]]
5.0 6 3.0 5.0
[[4 5 6]
 [4 5 6]]
0.0 2.0 0.0 3.0
[[0.77880078 0.8824969  0.77880078]
 [0.8824969  1.         0.8824969 ]]
5.0 6 4.0 6.0
[[5 6 7]
 [5 6 7]]
0.0 2.0 0.0 3.0
[[0.77880078 0.8824969  0.77880078]
 [0.8824969  1.         0.8824969 ]]
5.0 6 5.0 6
[[6 7]
 [6 7]]
0.0 2.0 0.0 2.0
[[0.77880078 0.8824969 ]
 [0.8824969  1.        ]]

图片代码

#保留边缘的平滑算法 :双边滤波和导向滤波
#双边滤波
#空间距离权重模板
import numpy as np
from scipy import signal
import cv2,math
def getClosenessWeight(sigma_g,H,W):
    r,c = np.mgrid[0:H:1,0:W:1]
    r=r.astype(np.float64)
    c=c.astype(np.float64)
    print(r)
    print(c)
    r-=(H-1)/2
    
    print(r)
    c-=(W-1)/2
    print(c)
    closeWeight = np.exp(-0.5*(np.power(r,2)+np.power(c,2))/math.pow(sigma_g,2))
    return closeWeight

def bfltGray(I,H,W,sigma_g,sigma_d):
    #I图像矩阵【0,1】
    #H,W模板
    #sigma_g 距离权重 >1
    #sigma_d 近似权重 <1
    #距离权重
    closenessWeight = getClosenessWeight(sigma_g,H,W)
    #模板中心位置
    cH = (H-1)/2
    cH = int(cH)
    cW = (W-1)/2
    cW = int(cW)
    rows,cols = I.shape
    bfltGrayImage = np.zeros(I.shape,np.float32)
    for r in range(rows):
        for c in range(cols):
            pixel = I[r][c]
            #判断边界
            rTop = 0 if r-cH<0 else r-cH
            rTop = int(rTop)
            rBottom = rows-1 if r+cH>rows-1 else r+cH
            rBottom = int(rBottom)
            cLeft = 0 if c-cW<0 else c-cW
            cLeft = int(cLeft)
            cRight = cols-1 if c+cW>cols-1 else c+cW
            cRight = int(cRight)
            #区域
            region = I[rTop:rBottom+1,cLeft:cRight+1]
            #相似性权重
            similarityWeightTemp = np.exp(-0.5*np.power(region-pixel,2.0)/math.pow
                                          (sigma_d,2))
            closenessWeightTemp = closenessWeight[rTop-r+cH:rBottom-r+cH+1,
                            cLeft-c+cW:cRight-c+cW+1]
            #两个模板相乘
            weightTemp = similarityWeightTemp*closenessWeightTemp
            #归一化权重模板
            weightTemp = weightTemp/np.sum(weightTemp)
            bfltGrayImage[r][c] = np.sum(region*weightTemp)
    return bfltGrayImage
if __name__ =='__main__':
    image = cv2.imread('E:/sy2/5/img1.png',cv2.IMREAD_GRAYSCALE)
    cv2.imshow('image',image)
    #将灰度值归一化
    image = image/255.0
    bfltImage = bfltGray(image,33,33,19,0.2)
    cv2.imshow('BilateralFiltering',bfltImage)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

在这里插入图片描述
在这里插入图片描述
opencv提供双边滤波函数
bilateralFilter
adaptiveBilateralFilter

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值