Python中的陷波拒绝过滤

2024-09-29 06:22:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在python中为一个赋值实现notch-reject过滤。我尝试使用Rafael Gonzales书中的陷波抑制滤波器公式,得到的只是一张边缘检测图像。然后我尝试了理想切口,结果如下:

Input image--Output of my program--Expected output

这是我的密码:

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()

Tags: theimageimportfftimgnppltfilter
1条回答
网友
1楼 · 发布于 2024-09-29 06:22:36
    我得到的是一个边缘检测图像,因为您的实现是强>高通<强>过滤器,中间是一个黑圆圈,作为边缘检测器。<李>
  • 然后我尝试了如果你应用正确,这是正确的

主要概念是在频域中过滤不需要的噪声,噪声可以被视为白点,您的角色是通过在频域中将白点乘以黑圈来抑制这些白点(称为过滤)

to improve this result add more notch filters (H5, H6, ...) to suppress the noise.

results

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()

相关问题 更多 >