如何从单个斑点将轮廓合并为一个

问题描述

我想问一下如何将多个轮廓合并为一个大轮廓。如下图所示。一粒药丸有多个轮廓。我想将它们合并成一个药丸状。

    output = cv2.bitwise_and(image,image,mask=img2)
    img_gray = cv2.cvtColor(output,cv2.COLOR_BGR2GRAY)
    D = ndimage.distance_transform_edt(img2)
    localMax = peak_local_max(D,indices=False,min_distance=20,labels=img2)
    markers = ndimage.label(localMax,structure=np.ones((3,3)))[0]
    labels = watershed(-D,markers,mask=img2)
    print("[INFO] {} unique segments found".format(len(np.unique(labels)) - 1))
    contours = []

    for label in np.unique(labels):
        if label == 0:
            continue

        mask = np.zeros(img_gray.shape,dtype="uint8")
        mask[labels == label] = 255

        # detect contours in the mask and grab the largest one
        cnts = cv2.findContours(mask.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
        cnts = imutils.grab_contours(cnts)
        c = max(cnts,key=cv2.contourArea)
        # hull = cv2.convexHull(c)

        cv2.drawContours(image_raw,cnts,-1,(0,255,0),2)

    cv2.@R_404_6424@how("Image detection",image_raw)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)