數字圖像處理-頻域濾波-高通/低通濾波


頻域濾波

頻域濾波是在頻率域對圖像做處理的一種方法。步驟如下:

濾波器大小和頻譜大小相同,相乘即可得到新的頻譜。

濾波后結果顯示,低通濾波去掉了高頻信息,即細節信息,留下的低頻信息代表了概貌。常用的例子,比如美圖秀秀的磨皮,去掉了臉部細節信息(痘坑,痘印,暗斑等)。高通濾波則相反。

高通/低通濾波

1.理想的高/低通濾波

顧名思義,高通濾波器為:讓高頻信息通過,過濾低頻信息;低通濾波相反。

理想的低通濾波器模板為:

 

其中,D0表示通帶半徑,D(u,v)是到頻譜中心的距離(歐式距離),計算公式如下:

 

M和N表示頻譜圖像的大小,(M/2,N/2)即為頻譜中心

理想的高通濾波器與此相反,1減去低通濾波模板即可。

部分代碼:

# 定義函數,顯示濾波器模板
def showTemplate(template):
    temp = np.uint8(template*255)
    cv2.imshow('Template', temp)
    return


# 定義函數,顯示濾波函數
def showFunction(template):
    row, col = template.shape
    row = np.uint16(row/2)
    col = np.uint16(col/2)
    y = template[row, col:]
    x = np.arange(len(y))
    plt.plot(x, y, 'b-', linewidth=2)
    plt.axis([0, len(x), -0.2, 1.2])
    plt.show()
    return


# 定義函數,理想的低通/高通濾波模板
def Ideal(src, d0, ftype):
    template = np.zeros(src.shape, dtype=np.float32)  # 構建濾波器
    r, c = src.shape
    for i in range(r):
        for j in range(c):
            distance = np.sqrt((i - r/2)**2 + (j - c/2)**2)
            if distance < d0:
                template[i, j] = 1
            else:
                template[i, j] = 0

    if ftype == 'high':
        template = 1 - template
    return template
Ideal

 2. Butterworth高/低通濾波

Butterworth低通濾波器函數為:

從函數圖上看,更圓滑,用冪系數n可以改變濾波器的形狀。n越大,則該濾波器越接近於理想濾波器

 1減去低通濾波模板即可得到高通濾波模板

 

部分代碼:

# 定義函數,巴特沃斯高/低通濾波模板
def Butterworth(src, d0, n, ftype):
    template = np.zeros(src.shape, dtype=np.float32)  # 構建濾波器
    r, c = src.shape
    for i in np.arange(r):
        for j in np.arange(c):
            distance = np.sqrt((i - r/2)**2 + (j - c/2)**2)
            template[i, j] = 1/(1 + (distance/d0)**(2*n))  # Butterworth 濾波函數
            template[i, j] = np.e ** (-1 * (distance**2 / (2 * d0**2)))  # Gaussian濾波函數
    if ftype == 'high':
        template = 1 - template
    return template
Butterworth

 3. Gaussian高/低通濾波

Guassian低通濾波器函數為:

 

 1減去低通濾波模板即可得到高通濾波模板

 

 部分代碼:

# 定義函數,高斯高/低通濾波模板
def Gaussian(src, d0, ftype):
    template = np.zeros(src.shape, dtype=np.float32)  # 構建濾波器
    r, c = src.shape
    for i in np.arange(r):
        for j in np.arange(c):
            distance = np.sqrt((i - r / 2) ** 2 + (j - c / 2) ** 2)
            template[i, j] = np.e ** (-1 * (distance ** 2 / (2 * d0 ** 2)))  # Gaussian濾波函數
    if ftype == 'high':
        template = 1 - template
    return template
Gaussian

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM