標籤:min opencv3 canny 角度 inline compute 閾值 data .com
git:https://github.com/linyi0604/Computer-Vision
1 # coding:utf8 2 3 import cv2 4 import numpy as np 5 6 7 # 讀入映像 8 img = cv2.imread("../data/line1.png") 9 # 轉為灰階映像10 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)11 # Canny邊緣檢測12 edges = cv2.Canny(gray, 50, 100)13 """14 canny邊緣檢測:15 有五個步驟:16 1 高斯濾波器降噪17 2 計算梯度18 3 邊緣上使用非最大抑制 nms19 4 邊緣上使用雙閾值去除假陽性20 5 分析所有邊緣串連 消除不明顯的邊緣21 """22 23 minLineLength = 2024 maxLineGap = 525 lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100, minLineLength, maxLineGap)26 """27 cv2.HoughLinesP28 作用:標準霍夫線變換, 找到映像中的所有直線29 參數:30 1 二值圖31 2 半徑精度32 3 角度精度33 4 最短檢測長度34 5 允許的最大缺口35 返回:36 一個列表,每一項是一個四元組,分別是直線兩個端點的座標37 """38 for line in lines:39 for x1, y1, x2, y2 in line:40 # 在圖片上畫直線41 cv2.line(img, (x1, y1), (x2, y2), (0, 255, 0), 2)42 43 cv2.imshow("edges", edges)44 cv2.imshow("lines", img)45 cv2.waitKey()46 cv2.destroyAllWindows()
python opencv3 直線檢測