Canny邊緣檢測一般流程
- 高斯濾波,平滑圖像,減弱噪聲
- 計算圖像梯度大小和方向
- 非極大值抑制,稀疏邊緣
- 滯後閾值(雙閾值檢測)
函數cv2.Canny(image, threshold1, threshold2[, apertureSize[, L2gradient]])
參數:
- image:深度爲8位的圖像
- threshold1:滯後閾值中的低閾值
- threshold2:滯後閾值中的高閾值
- apertureSize:Sobel邊緣檢測中的卷積框大小
- L2gradient:精度設置,默認爲False
返回值
單通道且圖像深度爲8位的圖像,即灰度圖像
e.g.
設置兩個滑動條,分別控制threshold1,threshold2
import cv2
#載入圖片
img_original=cv2.imread('E:\ImagesFavour\\5.2.3.jpg')
#設置窗口
cv2.namedWindow('Canny')
#定義回調函數
def nothing(x):
pass
#創建兩個滑動條,分別控制threshold1,threshold2
cv2.createTrackbar('threshold1','Canny',50,400,nothing)
cv2.createTrackbar('threshold2','Canny',100,400,nothing)
while(1):
#返回滑動條所在位置的值
threshold1=cv2.getTrackbarPos('threshold1','Canny')
threshold2=cv2.getTrackbarPos('threshold2','Canny')
#Canny邊緣檢測
img_edges=cv2.Canny(img_original,threshold1,threshold2)
#顯示圖片
cv2.imshow('original',img_original)
cv2.imshow('Canny',img_edges)
if cv2.waitKey(1)==ord('q'):
break
cv2.destroyAllWindows()