SideWindowBoxFilter滑动窗口滤波
程序员文章站
2024-03-16 11:02:40
...
随便乱翻看到的一个2019CVPR的oral,并且还不是深度学习的,所以赶快学习一下,具体的论文解读和C++实现可以看这里https://cloud.tencent.com/developer/article/1492206,我尝试用python复现一下,不知道对不对,希望看到错误的话指点我一下。。。
import cv2
import numpy as np
from scipy import signal
from scipy import misc
import random
def gasuss_noise(image, mean=0, var=0.001):
'''
添加高斯噪声
mean : 均值
var : 方差
'''
image = np.array(image/255, dtype=float)
noise = np.random.normal(mean, var ** 0.5, image.shape)
out = image + noise
if out.min() < 0:
low_clip = -1.
else:
low_clip = 0.
out = np.clip(out, low_clip, 1.0)
out = np.uint8(out*255)
#cv.imshow("gasuss", out)
return out
def conv2(k_l,k_r,img,padding="SAME"):
temp1 = signal.convolve2d(img,k_l,boundary='symm',mode='same')
temp1 = signal.convolve2d(temp1,k_r.T,boundary='symm',mode='same')
return temp1
def SideWindowBoxFilter(im, radius, iteration):
r = radius
K = np.ones((2*r+1,1))/(2*r+1)
k_L=K
k_L[r+1:]=0
k_L = k_L/np.sum(k_L)
k_R = np.flipud(k_L)
m,n = im.shape[0:2]
m = m+2*r
n = n+2*r
total = m*n
row, col = np.mgrid[0:m,0:n]
offset = row + m*(col-1) - total
result = im
d = np.zeros((m,n,8))
for i in range(0,im.shape[2]):
U = np.pad(im[:,:,im.shape[2]-1-i],((r,r),(r,r)),'edge')
for j in range(0,iteration):
origin = conv2(k_L,k_L, U,'same')
d[:,:,0] = conv2(k_L,k_L, U,'same') - U
d[:,:,1] = conv2(k_L,k_R, U,'same') - U
d[:,:,2] = conv2(k_R,k_L, U,'same') - U
d[:,:,3] = conv2(k_R,k_R, U,'same') - U
d[:,:,4] = conv2(k_L,K, U,'same') - U
d[:,:,5] = conv2(k_R,K, U,'same') - U
d[:,:,6] = conv2(K,k_L, U,'same') - U
d[:,:,7] = conv2(K,k_R, U,'same') - U
tmp = np.abs(d)
ind = np.argmin(tmp,2)
#ind = np.min(tmp,2)
index = offset+total*ind
new_d = np.zeros(d.shape[0:2],d.dtype)
height,width = new_d.shape[0:2]
for ni in range(height):
for j in range(width):
index_ = ind[ni,j]
new_d[ni,j] = d[ni,j,index_]
#dm = d[index]
U = U + new_d
result[:,:,im.shape[2]-1-i] = U[r:-r,r:-r]
return result
img = cv2.imread('timg.jpg')
#img = cv2.resize(img,(img.shape[1]//2,img.shape[0]//2))
img = gasuss_noise(img)
cv2.imshow('img1',img)
result = SideWindowBoxFilter(img,3,2)
cv2.imshow('img',result)
cv2.waitKey()
看起来还可以,哈哈。