Python OpenCV 影像邊緣偵測 Canny Edge Detection

本篇將介紹如何使用 OpenCV 與 Python 來作影像邊緣偵測 Canny Edge Detection,在寫 Python 影像處理程式時常會用到 OpenCV 邊緣偵測 Canny 的功能,接下來介紹怎麼使用 Python 搭配 OpenCV 模組來進行影像的邊緣偵測 Canny Edge Detection。

Python OpenCV Canny 影像邊緣偵測使用範例

在作邊緣偵測時,通常會先做平滑化(cv2.GaussianBlur)來降低雜訊,,再做 cv2.Canny 跟 cv2.GaussianBlur 前需要先將影像轉為灰階,最後依據結果調整平滑/模糊參數(cv2.GaussianBlur)或邊緣檢測參數(cv2.Canny)來達到想要的結果,整體步驟大約分成這幾步:

  1. 影像轉灰階: cv2.cvtColor
    將影像轉成灰階,也可以在 imread 時就指定讀取為灰階影像

  2. 影像去雜訊/平滑影像: cv2.GaussianBlur
    cv2.GaussianBlur 第二個參數是指定 Gaussian kernel size,本範例使用 5×5 大小

  3. 邊緣偵測: cv2.Canny
    採用雙門檻值
    第二個參數是指定最小門檻值 threshold1 – first threshold for the hysteresis procedure.
    第三個參數是指定最大門檻值 threshold2 – second threshold for the hysteresis procedure.

cv2.Canny 詳細程式碼如下:

opencv-canny.py
1
2
3
4
5
6
7
8
9
10
11
12
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2

image = cv2.imread('lena.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
canny = cv2.Canny(blurred, 30, 150)

cv2.imshow('Input', image)
cv2.imshow('Result', canny)
cv2.waitKey(0)

結果如下圖所示,

這次我們把圖片換成 building.jpg,並且把沒有經過平滑的 Canny 結果與有經過平滑的 Canny 結果都顯示出來,

opencv-canny2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2
import numpy as np
from matplotlib import pyplot as plt

image = cv2.imread('building.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
canny = cv2.Canny(gray, 30, 150)

blurred = cv2.GaussianBlur(gray, (5, 5), 0)
canny_blurred = cv2.Canny(blurred, 30, 150)

plt.subplot(221), plt.imshow(image), plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(222), plt.imshow(canny, cmap='gray'), plt.title('Canny')
plt.xticks([]), plt.yticks([])
plt.subplot(223), plt.imshow(blurred, cmap='gray'), plt.title('Blurred')
plt.xticks([]), plt.yticks([])
plt.subplot(224), plt.imshow(canny_blurred, cmap='gray'), plt.title('Canny Blurred')
plt.xticks([]), plt.yticks([])
plt.tight_layout()
plt.show()

換成 building.jpg 結果如下圖所示,

cv2.Canny 參數的詳細細節請參考這裡
cv2.GaussianBlur 參數的詳細細節請參考這裡

以上就是 Python OpenCV 影像邊緣偵測 Canny Edge Detection 介紹,
如果你覺得我的文章寫得不錯、對你有幫助的話記得 Facebook 按讚支持一下!

參考
[Python+OpenCV] 灰階、模糊、邊緣檢測 | 程式好好玩 - 點部落
https://dotblogs.com.tw/coding4fun/2017/11/09/125723
Canny Edge Detection — OpenCV-Python Tutorials 1 documentation
https://opencv24-python-tutorials.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_canny/py_canny.html
邊緣偵測懶人包-Canny演算法 - 天道酬勤 - Medium
https://medium.com/@bob800530/opencv-%E5%AF%A6%E4%BD%9C%E9%82%8A%E7%B7%A3%E5%81%B5%E6%B8%AC-canny%E6%BC%94%E7%AE%97%E6%B3%95-d6e0b92c0aa3
邊緣檢測(Canny - edge detection) @ HBY coding academic :: 痞客邦 ::
http://idiot3838.pixnet.net/blog/post/194161931
OPENCV – Edge detection邊緣偵測 – CH.Tseng
https://chtseng.wordpress.com/2016/12/05/opencv-edge-detection%E9%82%8A%E7%B7%A3%E5%81%B5%E6%B8%AC/
[Python + OpenCV] Canny邊緣偵測 @ K_程式人 :: 痞客邦 ::
https://jennaweng0621.pixnet.net/blog/post/404318621

其它相關文章推薦
Python OpenCV 影像二值化 Image Thresholding
Python OpenCV 影像平滑模糊化 blur
Python OpenCV 彩色轉灰階(RGB/BGR to GRAY)
Python OpenCV 彩色轉HSV(RGB/BGR to HSV)
Python OpenCV 彩色轉YCbCr(RGB/BGR to YCbCr)
Python OpenCV 灰階轉彩色(Gray to RGB/BGR)
Python OpenCV resize 圖片縮放
小專案 Python OpenCV 圖片轉字元圖畫