# 影像處理期末 --- ## Canny detection ### 為一個複合性的 edge detection 演算法,包含 4 種演算法: - Gaussian Filter (高斯濾波) - 梯度偵測 - 非最大值抑制 - Hysteresis thresholding (邊界界定) ### 且具有以下優點: - 低錯誤率 — 偵測出的亮點大多屬於邊緣 - 定位準確 — 標識出的邊緣與實際邊緣接近 - 解析度高 — 邊緣線條細緻 ### 步驟 1. 用 Gaussian Filter 濾雜訊 (kernel=5) $$ K = \frac{1}{159} \begin{bmatrix} 2 & 4 & 5 & 4 & 2 \\ 4 & 9 & 12 & 9 & 4 \\ 5 & 12 & 15 & 12 & 5 \\ 4 & 9 & 12 & 9 & 4 \\ 2 & 4 & 5 & 4 & 2 \end{bmatrix} $$ 2. 用 Sobel 計算邊緣梯度方向 - Sobel_x 算 **垂直** 梯度 $$ G_x = \begin{bmatrix} -1 & 0 & +1 \\ -2 & 0 & +2 \\ -1 & 0 & +1 \end{bmatrix} $$ - Sobel_y 算 **水平** 梯度 $$ G_y = \begin{bmatrix} -1 & -2 & -1 \\ 0 & 0 & 0 \\ +1 & +2 & +1 \end{bmatrix} $$ - 強度 - $G = \sqrt{G^2_x + G^2_y}$ - 方向 - $\theta = \arctan{\frac{G_y}{G_x}}$ - 將方向簡易分為 4 類 (與 +x 軸夾角) - $0^{\circ}$ - $45^{\circ}$ - $90^{\circ}$ - $135^{\circ}$ 3. Non-maxima suppression (非最大值抑制) - $M_{NMS}$ - 作用 edge thinning - 假設已確定此梯度方向為 45 度,就逐步尋找矩陣內,45 度方向強度最大的點,其餘 45度方向的點皆歸 0 ![](https://i.imgur.com/EIkivuy.png) 4. Hysteresis thresholding - Double thresholding - 原因: non-maxima suppression 輸出仍會帶有一些非 edge 的 local maxima,同時 connectivity 性質不明顯的 edge 區域 - 方法: - 設定兩個 thresholds $T_{high}$ and $T_{low}$ - 像素$(x,y)$ 如果 $M_{NMS}(x,y)$ $\gt$ $T_{high}$,該像素就稱為 strong - 像素$(x,y)$ 如果 $M_{NMS}(x,y)$ $\le$ $T_{low}$,該像素就稱為 weak - 所有其他的像素稱為 candidate - 如果像素是 weak,則捨去;如果是 strong,則輸出為 edge - 如果像素是 candidate,而且 $M_{NMS}(x,y)$ $\gt$ $T_{low}$,則判斷是否沿著 local maxima 相連的edge方向有穿過 (x,y),若是,則輸出為 edge