目錄
- 銳化(高通)空間濾波器
- 基礎 - 一階導數和二階導數的銳化濾波器
- 二階導數銳化圖像--拉普拉斯
銳化(高通)空間濾波器
- 平滑通過稱為低通濾波
- 類似于積分運算
- 銳化通常稱為高通濾波
- 微分運算
- 高過(負責細節的)高頻,衰減或抑制低頻
基礎 - 一階導數和二階導數的銳化濾波器
數字函數的導數是用差分來定義的。定義這些差分的方法有多種
一階導數的任何定義都要滿足如下要求:
- 恒定灰度區域的一階導數必須為0
- 灰度臺階或斜坡開始處的一階導數必須非零。
- 灰度斜坡上的一階導數必須非零。
二階導數的任何定義都要滿足如下要求:
- 恒定灰度區域的二階導數必須為零。
- 灰度臺階或斜坡的開始處和結束處的地階導數必須非零。
- 灰度斜坡上的二階導數必須為零。
一維函數f(x)f(x)f(x)的一階導數的一個基本定義是差分:
?f?x=f(x+1)?f(x)(3.48)\frac{\partial f}{\partial x} = f(x+1) -f(x) \tag{3.48}?x?f?=f(x+1)?f(x)(3.48)
二階導數定義為差分:
?2f?x2=f(x+1)+f(x?1)?2f(x)(3.49)\frac{\partial^2 f}{\partial x^2} = f(x+1) + f(x-1) - 2f(x) \tag{3.49}?x2?2f?=f(x+1)+f(x?1)?2f(x)(3.49)
def first_derivative(y):y_1 = np.zeros(len(y)-1)for i in range(1, len(y)):if i + 1 < len(y):y_1[i] = y[i+1] - y[i]return y_1def second_derivative(y):y_2 = np.zeros(len(y)-1)for i in range(1, len(y)):if i + 1 < len(y):y_2[i] = y[i+1] + y[i-1] - 2*y[i]return y_2
#一維數字一階與二階導數
y = np.arange(1, 7)
y = y[::-1]
y = np.pad(y, (3, 5), mode='constant', constant_values=(6, 1))
y = np.pad(y, (0, 5), mode='constant', constant_values=(0, 6))
x = np.arange(len(y))fig = plt.figure(figsize=(16, 4))
ax_1 = fig.add_subplot(1, 2, 1)
ax_1.plot(x, y, '-.s', label="data")
ax_1.legend(loc='best', fontsize=12)y_1 = first_derivative(y)
y_2 = second_derivative(y)ax_2 = fig.add_subplot(1, 2, 2)
ax_2.plot(y_1[1:], ':o', label='First derivative')
ax_2.plot(y_2[1:], '--s', alpha=0.5, label='Second derivative')
ax_2.plot(np.zeros(19), '--k', alpha=0.5)
ax_2.legend(loc='best', fontsize=12)
plt.tight_layout()
plt.show()
二階導數銳化圖像–拉普拉斯
我們感興趣的是各向同性核,這種核的響應與圖像中灰度不連續的方向無關。最簡單的各向同性導數算子(核)是拉普拉斯, 它定義為:
?2f=?2f?x2+?2f?y2(3.50)\nabla^2 f = \frac{\partial^2 f}{\partial x^2} + \frac{\partial^2 f}{\partial y^2} \tag{3.50}?2f=?x2?2f?+?y2?2f?(3.50)
我們把這個片子以離散形式表達:
?2f?x2=f(x+1,y)+f(x?1,y)?2f(x,y)(3.51)\frac{\partial^2 f}{\partial x^2} = f(x+1, y) + f(x-1, y) -2f(x, y) \tag{3.51}?x2?2f?=f(x+1,y)+f(x?1,y)?2f(x,y)(3.51)
?2f?y2=f(x,y+1)+f(x,y?1)?2f(x,y)(3.52)\frac{\partial^2 f}{\partial y^2} = f(x, y+1) + f(x, y-1) -2f(x, y) \tag{3.52}?y2?2f?=f(x,y+1)+f(x,y?1)?2f(x,y)(3.52)
兩個變量的離散拉普拉斯是
?2f(x,y)=f(x+1,y)+f(x?1,y)+f(x,y+1)+f(x,y?1)?4f(x,y)(3.53)\nabla^2 f(x, y)= f(x+1, y) + f(x-1, y) + f(x, y+1) + f(x, y-1)-4f(x, y) \tag{3.53}?2f(x,y)=f(x+1,y)+f(x?1,y)+f(x,y+1)+f(x,y?1)?4f(x,y)(3.53)
寫成矩陣的形式就得到拉普拉斯核:
[01(f(x,y?1)的權值)01(f(x?1,y)的權值)?4(f(x,y)的權值)1(f(x+1,y)的權值)01(f(x,y+1)的權值)0]\begin{bmatrix} 0 & 1 (f(x,y-1)的權值) & 0 \\ 1 (f(x-1,y)的權值) & -4 (f(x,y)的權值) & 1 (f(x+1,y)的權值) \\ 0 & 1 (f(x,y+1)的權值) & 0\end{bmatrix}???01(f(x?1,y)的權值)0?1(f(x,y?1)的權值)?4(f(x,y)的權值)1(f(x,y+1)的權值)?01(f(x+1,y)的權值)0????
圖像的銳化的濾波原理也類似于描述的低通濾波,只不過使用不同的系數
后面兩個是含對角項的擴展公式的核:
[0101?41010][1111?81111]\begin{bmatrix} 0 & 1 & 0 \\ 1 & -4 & 1 \\ 0 & 1 & 0\end{bmatrix} \begin{bmatrix} 1 & 1 & 1 \\ 1 & -8 & 1 \\ 1 & 1 & 1\end{bmatrix}???010?1?41?010???????111?1?81?111????
這兩個是二階導數定義得到的:
[0?10?14?10?10][?1?1?1?18?1?1?1?1]\begin{bmatrix} 0 & -1 & 0 \\ -1 & 4 & -1 \\ 0 & -1 & 0\end{bmatrix} \begin{bmatrix} -1 & -1 & -1 \\ -1 & 8 & -1 \\ -1 & -1 & -1\end{bmatrix}???0?10??14?1?0?10????????1?1?1??18?1??1?1?1????
拉普拉斯是導數算子,因此會突出圖像中的急劇灰度過渡,并且不會強調緩慢變化的灰度區域。這往往會產生具有灰色邊緣線和其他不連續性的圖像,它們都疊加在暗色無特征背景上。將拉普拉斯圖像與原圖相加,就可以“恢復”背景特征,同時保留拉普拉斯的銳化效果。 上面兩個核得到的圖像需要用原圖減去得到的圖,下面兩個核得到的圖像需要用原圖加下得到的圖,以實現銳化。
g(x,y)=f(x,y)+c[?2f(x,y)](3.54)g(x,y) = f(x,y) + c[\nabla^2 f(x,y)] \tag{3.54}g(x,y)=f(x,y)+c[?2f(x,y)](3.54)
使用上面的前兩個核,則c=?1c = -1c=?1,后面的兩個二階導數定義的核則c=1c = 1c=1
兩組核剛好相差一個負號
觀察平滑與銳化核可發現,平滑核的系數之和為1,銳化核的系數之和為0。用銳化核處理圖像時,有會出現負值,需要進行額外處理才能得到合適的視覺結果。
# Laplacian filter 不同的核,拉普拉斯銳化
img = cv2.imread('DIP_Figures/DIP3E_Original_Images_CH03/Fig0338(a)(blurry_moon).tif', 0)
img = normalize(img)kernel_laplacian_a = np.array([[0,1,0],[1,-4,1],[0,1,0]])
# 下面這個核雖然可以銳化,但是效果沒有上面這個好
# kernel_laplacian_d = np.array([[
# [-1, -1, -1],
# [-1, 8, -1],
# [-1, -1, -1]
# ]])
laplacian_img_a = filter_2d(img, kernel_laplacian_a, mode='constant')
laplacian_img_b = filter_2d(img, -kernel_laplacian_a, mode='constant')plt.figure(figsize=(14, 16))
plt.subplot(2, 2, 1), plt.imshow(img, cmap='gray', vmin=0, vmax=1), plt.title('Original')
plt.subplot(2, 2, 2), plt.imshow(laplacian_img_a, cmap='gray', vmax=1), plt.title('Laplacian')laplacian_img_ori = normalize(img - laplacian_img_a)
plt.subplot(2, 2, 3), plt.imshow(laplacian_img_ori, cmap='gray', vmax=1), plt.title('Laplacian With ORI')laplacian_imgori = normalize(img + laplacian_img_b)
plt.subplot(2, 2, 4), plt.imshow(laplacian_imgori, cmap='gray', vmax=1), plt.title('Laplacian With ORI')plt.tight_layout()
plt.show()