Laplacian濾波器實現並用於圖像銳化


一. 拉普拉斯濾波器簡介:

        我們知道:


拉普拉斯算子 ↑
 

x方向上二階偏導數的數值近似計算 ↑
 

y方向上二階偏導數的數值近似計算 ↑
 
 

拉普拉斯算子在平面內的數值近似 ↑
 
 

拉普拉斯濾波器卷積核表示 ↑
 

二. 3*3的laplacian濾波器實現

# laplacian filter

def laplacian_filter(img, K_size=3):

    H, W = img.shape

    # zero padding

    pad = K_size // 2

    out = np.zeros((H + pad * 2, W + pad * 2), dtype=np.float)

    out[pad: pad + H, pad: pad + W] = img.copy().astype(np.float)

    tmp = out.copy()

    # laplacian kernle

    K = [[0., 1., 0.],[1., -4., 1.], [0., 1., 0.]]

    # filtering

    for y in range(H):

        for x in range(W):

            out[pad + y, pad + x] = np.sum(K * (tmp[y: y + K_size, x: x + K_size]))

    out = np.clip(out, 0, 255)

    out = out[pad: pad + H, pad: pad + W].astype(np.uint8)

    return out

 


三. 利用laplacian濾波器實現圖像的銳化

        由於拉普拉斯是一種微分算子,它的應用可增強圖像中灰度突變的區域,減弱灰度的緩慢變化區域。

        因此,銳化處理可選擇拉普拉斯算子對原圖像進行處理,產生描述灰度突變的圖像,再將拉普拉斯圖像與原始圖像疊加而產生銳化圖像:


使用拉普拉斯濾波器實現的圖像銳化算法 ↑

        其中,f(x,y)為原始圖像,g(x,y)為銳化后圖像,c為-1(卷積核中間為負數時,若卷積核中間為正數,則c為1)。


四. 通過laplacian濾波器實現圖像銳化 python源碼

import cv2

import numpy as np

# Image sharpening by laplacian filter

def laplacian_sharpening(img, K_size=3):

    H, W = img.shape

    # zero padding

    pad = K_size // 2

    out = np.zeros((H + pad * 2, W + pad * 2), dtype=np.float)

    out[pad: pad + H, pad: pad + W] = img.copy().astype(np.float)

    tmp = out.copy()

    # laplacian kernle

    K = [[0., 1., 0.],[1., -4., 1.], [0., 1., 0.]]

    # filtering and adding image -> Sharpening image

    for y in range(H):

        for x in range(W):

            # core code

            out[pad + y, pad + x] = (-1) * np.sum(K * (tmp[y: y + K_size, x: x + K_size])) + tmp[pad + y, pad + x]

    out = np.clip(out, 0, 255)

    out = out[pad: pad + H, pad: pad + W].astype(np.uint8)

    return out

# Read Gray Scale image

img = cv2.imread("../paojie_g.jpg",0).astype(np.float)

# Image sharpening by laplacian filter

out = laplacian_sharpening(img, K_size=3)

# Save result

cv2.imwrite("out.jpg", out)

cv2.imshow("result", out)

cv2.waitKey(0)

cv2.destroyAllWindows()

 


五. 實驗結果:


銳化后圖像 ↑
 

原圖 ↑
 

六. 參考內容

  https://www.jianshu.com/p/abeb28548c44


免責聲明!

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



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