数字图像处理与Python实现-图像降噪-理想带阻滤波

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/wujuxKkoolerter/article/details/102733501

理想带阻滤波(BandStop Filter BSF)

带阻滤波器(bandstop filters,BSF)指能通过大多数频率分量,但将某些频率分量衰减到极低水平的滤波器。
理想带阻滤波器的表达式如下:

H ( u , v ) = { 1 , D ( u , v ) < D 0 W / 2 0 , D 0 W / 2 D ( u , v ) D 0 + W / 2 1 , D ( u , v ) > D 0 + W / 2 H(u,v) = \begin{cases} 1,& D(u,v) \lt D_0 - W/2 \\\\ 0,& D_0-W/2 \leq D(u,v) \leq D_0 + W/2 \\\\ 1,&D(u,v) \gt D_0 + W / 2 \end{cases}

理想带通滤波器表达式如下:
H ( u , v ) = { 0 , D ( u , v ) < D 0 W / 2 1 , D 0 W / 2 D ( u , v ) D 0 + W / 2 0 , D ( u , v ) > D 0 + W / 2 H(u,v) = \begin{cases} 0,& D(u,v) \lt D_0 - W/2 \\\\ 1,& D_0-W/2 \leq D(u,v) \leq D_0 + W/2 \\\\ 0,&D(u,v) \gt D_0 + W / 2 \end{cases}

在这里插入图片描述

Python语言实现代码如下:


def bandstop_kernel(img,D0=5,W=10):
    assert img.ndim == 2
    r,c = img.shape[1],img.shape[0]
    u = np.arange(r)
    v = np.arange(c)
    u, v = np.meshgrid(u, v)
    low_pass = np.sqrt( (u-r/2)**2 + (v-c/2)**2 )

    idx = (low_pass < (D0 - W / 2)) | (low_pass > (D0 + W / 2))
    low_pass[idx] = 1
    low_pass[~idx] = 0

    return low_pass

def bandstop_filter(img,D0=5,W=10):
    assert img.ndim == 2
    kernel = bandstop_kernel(img,D0,W=10)
    gray = np.float64(img)
    gray_fft = np.fft.fft2(gray)
    gray_fftshift = np.fft.fftshift(gray_fft)
    dst = np.zeros_like(gray_fftshift)
    dst_filtered = kernel * gray_fftshift
    dst_ifftshift = np.fft.ifftshift(dst_filtered)
    dst_ifft = np.fft.ifft2(dst_ifftshift)
    dst = np.abs(np.real(dst_ifft))
    dst = np.clip(dst,0,255)
    return np.uint8(dst)

def bandpass_filter(img,D0=5,W=10):
    kernel = 1.0 - bandstop_kernel(img,D0,W=10)
    gray = np.float64(img)
    gray_fft = np.fft.fft2(gray)
    gray_fftshift = np.fft.fftshift(gray_fft)
    dst = np.zeros_like(gray_fftshift)
    dst_filtered = kernel * gray_fftshift
    dst_ifftshift = np.fft.ifftshift(dst_filtered)
    dst_ifft = np.fft.ifft2(dst_ifftshift)
    dst = np.abs(np.real(dst_ifft))
    dst = np.clip(dst,0,255)
    return np.uint8(dst)

程序运行结果:

理想带阻滤波:

在这里插入图片描述
理想带通滤波器:

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/wujuxKkoolerter/article/details/102733501