0

Recently, I have been studying image segmentation. I am using the "cv2.findcontour" function to contain the annotation information of the mask image. Pixel values of the mask have 0, 1, and 2. However, there is a problem that the contour is separated from the overlapping part as shown in the picture below. Is there a way to combine images of the same label or something else?

enter image description here

I used following code:

for i in tqdm(range(16,17)):

    mask = train_y[i]
    height = image_data[i]['height']
    width = image_data[i]['width']
    mask_resize = cv2.resize(mask, dsize=(width, height), interpolation=cv2.INTER_AREA) 
    labels = np.unique(mask_resize[mask_resize > 0])
    image_id = image_data[i]['id']
    plt.imshow(mask_resize)
    plt.show()
    for label in labels:

        y = mask_resize == label
        y = y.astype(np.uint8)
        contours, _ = cv2.findContours(y, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
        for i, contour in enumerate(contours):
            mask_zero = np.zeros(shape=(height, width), dtype="uint8")
            ctr = np.array(contour).reshape((-1,1,2)).astype(np.int32)
            M = cv2.moments(ctr)     
            if int(M['m00']) > 1000 :
                centroid_x = int(M['m10']/M['m00'])
                centroid_y = int(M['m01']/M['m00'])
                img = cv2.drawContours(mask_zero, [ctr], -1, (255,0,0), 5)
                segmentation = np.abs(contour).flatten().tolist()
                plt.imshow(img)
                plt.show()
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
  • I would: 1) separate by mask index as binary mask (`mask==1` and `mask==2`) 2)apply a closening operation or something like that, or some dilations and erotions – Ulises Bussi Jan 26 '22 at 03:15
  • are those two half-animal contours given the **same label**? they must be. if they aren't, you can't possibly associate nearby blobs just because they're nearby. – Christoph Rackwitz Jan 26 '22 at 09:18

0 Answers0