And digital image processing implemented in Python - Image Denoising - high-pass filtering trapezoid

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/wujuxKkoolerter/article/details/102759152

Trapezoidal high pass filter

A high-pass ladder filter (TLPF) transfer function as follows:

$$
H(u,v) = 1 - \begin{cases}
1 & D(u,v) \lt D_0 \\
\frac{D(u,v) - D_1}{D_0-D_1} & D_0 \lt D(u,v) \leq D_1 \\
0 &D(u,v) > D_1
\end{cases}

\tag{18-1}

$$

Python language codes are as follows:

def trapezoidal_low_pass_kernel(img,D0=5,D1=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
    idx2 = (low_pass >= D0) & (low_pass <= D1)
    idx3 = low_pass > D1

    low_pass[idx] = 1
    low_pass[idx2] = (low_pass[idx2] - D1) / (D1 - D0)
    low_pass[idx3] = 0

    return low_pass

def trapezoidal_high_pass_filter(img,D0=5,D1=15):
    assert img.ndim == 2
    gray = np.float64(img)
    kernel = 1 - trapezoidal_low_pass_kernel(img,D0,D1)
    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)

Result of the program:

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/wujuxKkoolerter/article/details/102759152
Recommended