python - Python 中的缺口拒绝过滤

标签 python numpy opencv image-processing filter

我正在尝试在 python 中为分配实现缺口拒绝过滤。我曾尝试使用 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()

最佳答案

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

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

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

    关于python - Python 中的缺口拒绝过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65483030/

    相关文章:

    python - python 检查对象是否是列表列表的代码

    python - 如何用python表达以下分段函数?

    python - 如何从 Python 中的补丁中恢复 3D 图像?

    python - 如何在 numpy 数组中随机洗牌 "tiles"

    python - 如何使用python检测复选框

    android - 为什么opencv的降噪功能不起作用

    python - 抓取 protected 电子邮件

    python - 实例化 boolean 二维数组的理解?

    python - 使用 MD5 校验和在多个硬盘驱动器上搜索重复文件是否安全?

    python - 具有 Python 特定过滤器的 OpenCV 不一样