连接点的Numpy数组以形成轮廓?

时间:2019-03-09 03:20:12

标签: python opencv

def cnts_convex_hull(cnts,image):
    results=[]
    points=[]
    for cnt in cnts:
        for point in cnt:
            reg_point=point[0]
            print(reg_point)
            points.append(reg_point)
    results=np.array(points)
    # i want to add a fn to form a contour , then use convexhull() on it.

img=cv2.imread("3.jpg")
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
min = np.array([0, 100 , 100])
max = np.array([179, 255, 255])
mask = cv2.inRange(hsv, min, max)
res = cv2.bitwise_and(img, img, mask=mask)
##################################################
gray_res = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
kernal = np.ones((5, 5), np.uint8)
gray_res = cv2.morphologyEx(gray_res, cv2.MORPH_CLOSE, kernal)
##################################################
edges = cv2.Canny(gray_res, 100, 100)
cnts,h  = cv2.findContours(edges.copy(), cv2.RETR_TREE, 
cv2.CHAIN_APPROX_SIMPLE)
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:10]
cnts_convex_hull(cnts,img)
cv2.imshow('frame',img)
cv2.waitKey(0)

我手上的问题很简单,我有(经过过滤和边缘检测以及通常的东西之后)看起来像这样的东西: https://i.stack.imgur.com/GUrYZ.png,我希望最终使其接近于形状而不是2条线,所以我想在它们上形成凸包,并且我指的是2个轮廓的全部点列表。 所以在上面的代码中,我能够在np.array()中获取点 但是我无法将它们转换为轮廓形状,因此可以在它们上预成型凸壳

0 个答案:

没有答案