偵測邊緣(2)
邊緣偵測可以沿著多個軸向進行,然後合併為單一的邊緣值。對於 2D 影像,水平與垂直的「邊緣圖」可以用畢氏定理合併:
$$z = \sqrt{x^2 + y^2}$$
常見的一種邊緣偵測器是 Sobel 濾波器。Sobel 濾波器會對偵測器中心像素給予較高的權重:
weights = [[ 1, 2, 1],
[ 0, 0, 0],
[-1, -2, -1]]
在這個練習中,請改良前一次的偵測作法,將兩個經 Sobel 濾波的影像結果合併成一張綜合邊緣圖。
本練習屬於課程
Python 的生醫影像分析
練習說明
- 對
im在第 1 與第 2 軸上套用ndi.sobel()。 - 使用畢氏定理計算整體邊緣幅度。用
np.sqrt()與np.square()。 - 顯示幅度影像。使用灰階色彩對應,並將
vmax設為75。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Apply Sobel filter along both axes
sobel_ax0 = ndi.sobel(____, axis=____)
sobel_ax1 = ____
# Calculate edge magnitude
edges = ____
# Plot edge magnitude
plt.imshow(____, ____, ____)
format_and_render_plot()