Python中的缺口拒绝过滤

我正在尝试在 python 中为分配实现缺口拒绝过滤。我曾尝试使用 Rafael Gonzales 书中的陷波拒绝滤波器公式,但我得到的只是边缘检测图像。然后我尝试了理想的缺口拒绝,结果如下:

输入图像--我的程序的输出--预期输出

这是我的代码:

import cv2
import numpy as np
import matplotlib.pyplot as plt


def notch_reject_filter(shape, d0=9, u_k=0, v_k=0):
    P, Q = shape
    # Initialize filter with zeros
    H = np.zeros((P, Q))

    # Traverse through filter
    for u in range(0, P):
        for v in range(0, Q):
            # Get euclidean distance from point D(u,v) to the center
            D_uv = np.sqrt((u - P / 2 + u_k) ** 2 + (v - Q / 2 + v_k) ** 2)
            D_muv = np.sqrt((u - P / 2 - u_k) ** 2 + (v - Q / 2 - v_k) ** 2)

            if D_uv <= d0 or D_muv <= d0:
                H[u, v] = 0.0
            else:
                H[u, v] = 1.0

    return H


img = cv2.imread('input.png', 0)

img_shape = img.shape

original = np.fft.fft2(img) 
center = np.fft.fftshift(original)  

NotchRejectCenter = center * notch_reject_filter(img_shape, 32, 50, 50)  
NotchReject = np.fft.ifftshift(NotchRejectCenter)
inverse_NotchReject = np.fft.ifft2(NotchReject)  # Compute the inverse DFT of the result


plot_image = np.concatenate((img, np.abs(inverse_NotchReject)),axis=1)

plt.imshow(plot_image, "gray"), plt.title("Notch Reject Filter")
plt.show()

回答

  • 我得到的只是边缘检测图像,因为您的实现是高通滤波器,它是中间的黑色圆圈,用作边缘检测器。
  • 然后我尝试了理想的缺口拒绝如果您正确应用,这是正确的。

主要概念是过滤频域中不需要的噪声,噪声可以被视为白点,您的作用是通过将它们乘以频域中的黑色圆圈来抑制该白点(称为过滤)。

为了改善这个结果,添加更多陷波滤波器(H5,H6,...)来抑制噪声。

import cv2
import numpy as np
import matplotlib.pyplot as plt

#------------------------------------------------------
def notch_reject_filter(shape, d0=9, u_k=0, v_k=0):
    P, Q = shape
    # Initialize filter with zeros
    H = np.zeros((P, Q))

    # Traverse through filter
    for u in range(0, P):
        for v in range(0, Q):
            # Get euclidean distance from point D(u,v) to the center
            D_uv = np.sqrt((u - P / 2 + u_k) ** 2 + (v - Q / 2 + v_k) ** 2)
            D_muv = np.sqrt((u - P / 2 - u_k) ** 2 + (v - Q / 2 - v_k) ** 2)

            if D_uv <= d0 or D_muv <= d0:
                H[u, v] = 0.0
            else:
                H[u, v] = 1.0

    return H
#-----------------------------------------------------

img = cv2.imread('input.png', 0)

f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
phase_spectrumR = np.angle(fshift)
magnitude_spectrum = 20*np.log(np.abs(fshift))

img_shape = img.shape

H1 = notch_reject_filter(img_shape, 4, 38, 30)
H2 = notch_reject_filter(img_shape, 4, -42, 27)
H3 = notch_reject_filter(img_shape, 2, 80, 30)
H4 = notch_reject_filter(img_shape, 2, -82, 28)

NotchFilter = H1*H2*H3*H4
NotchRejectCenter = fshift * NotchFilter 
NotchReject = np.fft.ifftshift(NotchRejectCenter)
inverse_NotchReject = np.fft.ifft2(NotchReject)  # Compute the inverse DFT of the result


Result = np.abs(inverse_NotchReject)

plt.subplot(222)
plt.imshow(img, cmap='gray')
plt.title('Original')

plt.subplot(221)
plt.imshow(magnitude_spectrum, cmap='gray')
plt.title('magnitude spectrum')

plt.subplot(223)
plt.imshow(magnitude_spectrum*NotchFilter, "gray") 
plt.title("Notch Reject Filter")

plt.subplot(224)
plt.imshow(Result, "gray") 
plt.title("Result")


plt.show()


以上是Python中的缺口拒绝过滤的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>