# Python+OpenCV教程番外篇9：凸包及更多轮廓特征

## 多边形逼近

``````# 1.先找到轮廓
_, thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
image, contours, hierarchy = cv2.findContours(thresh, 3, 2)
cnt = contours[0]

# 2.进行多边形逼近，得到多边形的角点
approx = cv2.approxPolyDP(cnt, 3, True)

# 3.画出多边形
image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
cv2.polylines(image, [approx], True, (0, 255, 0), 2)
``````

## 凸包

``````# 1.先找到轮廓
_, thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
image, contours, hierarchy = cv2.findContours(thresh, 3, 2)
cnt = contours[0]

# 2.寻找凸包，得到凸包的角点
hull = cv2.convexHull(cnt)

# 3.绘制凸包
image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
cv2.polylines(image, [hull], True, (0, 255, 0), 2)
``````

``````print(hull[0])  # [[362 184]]（坐标）
hull2 = cv2.convexHull(cnt, returnPoints=False)
print(hull2[0])  # [510]（cnt中的索引）
print(cnt[510])  # [[362 184]]
``````

``````print(cv2.isContourConvex(hull))  # True
``````

## 点到轮廓距离

`cv2.pointPolygonTest()`函数计算点到轮廓的最短距离（也就是垂线），又称多边形测试：

``````dist = cv2.pointPolygonTest(cnt, (100, 100), True)  # -3.53
``````