【OpenCV】??高手勿入! 半小時學會基本操作 14??
- 概述
- 影像輪廓
- 繪制輪廓
- 輪廓特征
- 輪廓近似
- 邊界矩形
- 外接圓
概述
OpenCV 是一個跨平臺的計算機視覺庫, 支持多語言, 功能強大. 今天小白就帶大家一起攜手走進 OpenCV 的世界. (第 14 課)

影像輪廓
cv2.findContours可以幫助我們查找輪廓.
格式:
cv2.findContours(image, mode, method, contours=None, hierarchy=None, offset=None)
引數:
- image: 需要查找輪廓的圖片
- mode: 模式
- RETR_EXTERNAL: 只檢測最外面的輪廓
- RETR_LIST: 檢測所有的輪廓, 并將其保存到一條鏈表中
- RETR_CCOMP: 檢索所有的輪廓, 將他們組織為兩層: 頂部是各分部法外部邊界, 第二層是空洞的邊界
- RRTR_TREE: 檢索所有的輪廓, 并重構嵌套輪廓的整個層次
- method: 輪廓逼近的方法
- CHAIN_APPROX_NONE: 以 Freeman 鏈碼的方式輸出輪廓, 所有其他方法輸出多邊形 (定點的序列)
- CHAIN_APPROX_SIMPLE: 壓縮水平的, 垂直的和斜的部分, 只保留他們的終點部分
回傳值:
- contours:輪廓本身
- hierarchy: 輪廓的對應編號
原圖:

繪制輪廓
cv2.drawContours可以實作輪廓繪制.
格式:
cv2.drawContours(image, contours, contourIdx, color, thickness=None, lineType=None, hierarchy=None, maxLevel=None, offset=None):
引數:
- image: 需要繪制輪廓的圖片
- contours: 輪廓
- color: 顏色
- thickness: 輪廓粗細

繪制所有輪廓:
# 讀取圖片
img = cv2.imread("contours.png")
# 轉換成灰度圖
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 二值化
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 獲取輪廓 (所有)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
# 繪制輪廓
draw_img = img.copy()
res = cv2.drawContours(draw_img, contours, -1, (0, 0, 255), 2)
# 圖片展示
cv2.imshow("res", res)
cv2.waitKey(0)
cv2.destroyAllWindows()
輸出結果:

繪制單個輪廓:
# 讀取圖片
img = cv2.imread("contours.png")
# 轉換成灰度圖
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 二值化
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 獲取輪廓
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
# 繪制輪廓 (單一)
draw_img = img.copy()
res = cv2.drawContours(draw_img, contours, 0, (0, 0, 255), 2)
# 圖片展示
cv2.imshow("res", res)
cv2.waitKey(0)
cv2.destroyAllWindows()
輸出結果:

輪廓特征
# 獲取輪廓
cnt = contours[0] # 取第一個輪廓
# 面積
area = cv2.contourArea(cnt)
print("輪廓面積:", area)
# 周長, True表示合并
perimeter = cv2.arcLength(cnt, True)
print("輪廓周長:", perimeter)
輸出結果:
輪廓面積: 8500.5
輪廓周長: 437.9482651948929
輪廓近似
原圖:

代碼:
# 讀取圖片
img = cv2.imread("contours2.png")
# 轉換成灰度圖
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 二值化
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 獲取輪廓
contours, hieratchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
# 繪制輪廓
draw_img = img.copy()
res = cv2.drawContours(draw_img, contours, 0, (0, 0, 255), 2)
# 圖片展示
cv2.imshow("res", res)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 取外圍輪廓
cnt = contours[0]
# 輪廓近似
epsilon = 0.1 * cv2.arcLength(cnt, True)
approx = cv2.approxPolyDP(cnt, epsilon, True)
# 繪制輪廓
draw_img = img.copy()
res = cv2.drawContours(draw_img, [approx], -1, (0, 0, 255), 2)
# 圖片展示
cv2.imshow("res", res)
cv2.waitKey(0)
cv2.destroyAllWindows()
輸出結果:
直接繪制輪廓:

輪廓近似:

邊界矩形
cv2.boundingRect可以幫助我們得到邊界矩形的位置和長寬.
例子:
# 讀取圖片
img = cv2.imread("contours.png")
# 轉換成灰度圖
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 二值化
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 獲取輪廓
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
# 獲取第一個輪廓
cnt = contours[0]
# 獲取正方形坐標長寬
x, y, w, h = cv2.boundingRect(cnt)
# 圖片展示
img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 輪廓面積
area = cv2.contourArea(cnt)
# 邊界矩形面積
rect_area = w * h
# 占比
extent = area / rect_area
print('輪廓面積與邊界矩形比:', extent)
輸出結果:
輪廓面積與邊界矩形比: 0.5154317244724715

外接圓
cv2.minEnclosingCircle可以幫助我們得到外接圓的位置和半徑.
例子:
# 讀取圖片
img = cv2.imread("contours.png")
# 轉換成灰度圖
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 二值化
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 獲取輪廓
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
# 獲取第一個輪廓
cnt = contours[0]
# 獲取外接圓
(x, y), radius = cv2.minEnclosingCircle(cnt)
# 獲取圖片
img = cv2.circle(img, (int(x), int(y)), int(radius), (255, 100, 0), 2)
# 圖片展示
cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
輸出結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/290570.html
標籤:AI
