SideWindowBoxFilter滑动窗口滤波

随便乱翻看到的一个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()

 

看起来还可以,哈哈。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值