#空间距离权重模板
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