I have to connect the unfinished edges of the contours in the red color bounding box.enter image description here

I used distance transform, watershed, and morphology, but still, edges are not connecting.

import cv2
import glob
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt

og_path_test = ''
predicted_path = ''

test = glob.glob(og_path_test + '*')
for image in test:
    name = image.split('/')[-1]
    name_new = name[:-4] + '.jpg'

    im = Image.open(image)
    im = im.resize((IMG_WIDTH, IMG_HEIGHT))
    im = np.array(im, dtype=np.float32)

    x_batch = np.array([im], np.float32)
    prediction = model.predict(x_batch)

    mask = np.zeros_like(im[:, :, 0])
    for i in range(len(prediction)):
        mask += np.reshape(prediction[i], (IMG_WIDTH, IMG_HEIGHT))
    ret, mask = cv2.threshold(mask, np.mean(mask) + 1.2 * np.std(mask), 255, cv2.THRESH_BINARY)

    # Distance transform and watershed algorithm
    dist_transform = cv2.distanceTransform(mask.astype(np.uint8), cv2.DIST_L2, 5)
    _, local_maxima = cv2.threshold(dist_transform, 0.5 * dist_transform.max(), 255, cv2.THRESH_BINARY)
    _, markers = cv2.connectedComponents(local_maxima.astype(np.uint8))
    markers = markers + 1
    watershed_img = cv2.cvtColor(im.astype(np.uint8), cv2.COLOR_RGB2BGR)
    cv2.watershed(watershed_img, markers)
    mask[markers == -1] = 255

    path = predicted_path
    plt.imsave(os.path.join(path, name_new), np.squeeze(mask), vmin=0, vmax=255)
  • 1
    $\begingroup$ Could you share the image without your red boxes? $\endgroup$
    – Royi
    Apr 22 at 8:13
  • 1
    $\begingroup$ Can you show what have you tried so far, where you failed, where and why you are stuck, what you are trying to achieve and the general context of your problem? Any additional information may help people provide (possible) solution(s). Please keep in mind that we don't know anything about your problem and things that you may take for granted are missing puzzle pieces for us. $\endgroup$
    – ZaellixA
    Apr 22 at 9:56
  • $\begingroup$ @OverLordGoldDragon I am a new user here and this is my first time asking questions. I kindly request that you remain calm and patient as I navigate this platform. $\endgroup$
    – user67438
    Apr 22 at 13:03
  • $\begingroup$ You are responsible for reading Q&A rules, and regular users are responsible for following them. They aren't, and it's causing problems. If a negative reaction troubles you, as it does many on StackExchange, you should take it up on Meta.SE to set up higher barriers for entry. Nobody complains to a professor for refusing to do their homework for them. Now, you have shown work, and I've retracted both my comment and my downvote. That's more than you'll get from others. $\endgroup$ Apr 22 at 13:12
  • $\begingroup$ It's also being discussed on our meta. $\endgroup$ Apr 22 at 14:22

1 Answer 1


I will try to sketch a solution.

It is easy using a dilation (imdilate() in MATLAB) to close the curves. The issue is will make other elements connected as well.

So the idea is to create a set of operations which dilates non connected curves while keep other lines thin.

Assuming we can label curves which are closed, we can iterate:

  1. Use morphological to dilate the image (imdilate() in MATLAB).
  2. Keep the closed curves thin.

The trick to locate the closed curve is applying any graph based algorithm like connected components. Once you have this, just iterate.

  • $\begingroup$ It's something, but I wonder just how much. It quite downplays the problem of false positives. No doubt this would be closed and identified as "success". I question whether manual feature engineering can handle this problem well. $\endgroup$ Apr 28 at 13:04
  • $\begingroup$ I was under the assumption that what is closed should not change. Basically you'd want to elongate the lines along their direction. So my first idea was using Radon like transformation and only dilate along existing lines. $\endgroup$
    – Royi
    Apr 28 at 13:25
  • $\begingroup$ @OverLordGoldDragon It does not matter, even though it closes the unnecessary polygon. I have a way to identify and eliminate those polygons. $\endgroup$
    – user67438
    May 6 at 14:59
  • $\begingroup$ @user67438, If you don't care about closing other polygons, just keep using imdilate() repeatedly and you'll have them closed. $\endgroup$
    – Royi
    May 6 at 15:07
  • $\begingroup$ @Royi - Thanks for your reply! $\endgroup$
    – user67438
    May 6 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.