python opencv 凸包 convexHull

 

import cv2

# 读取图片并转至灰度模式
# 1.先找到轮廓
img = cv2.imread('Test.png', 0)
_, thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
contours, hierarchy = cv2.findContours(thresh, 3, 2)
# cnt = contours[0]
# 寻找物体的凸包并绘制凸包的轮廓
# for cnt in contours:
#     hull = cv2.convexHull(cnt)
#     length = len(hull)
#     # 如果凸包点集中的点个数大于5
#     if length > 5:
#         # 绘制图像凸包的轮廓
#         for i in range(length):
#             cv2.line(img, tuple(hull[i][0]), tuple(hull[(i+1)%length][0]), (255,0,0), 2)

for cnt in contours:
    # 2.寻找凸包,得到凸包的角点
    hull = cv2.convexHull(cnt)
    print(hull[0])
    # 3.绘制凸包
    cv2.polylines(img, [hull], True, (255, 255, 0), 2)
cv2.imshow('img', img)
cv2.waitKey()




猜你喜欢

转载自blog.csdn.net/qq_39097425/article/details/127877057
今日推荐