How to seperate the contours?

Viewed 57

This is the input image from which mask is being generated.

Input Image

I am getting an image mask as output from deep learning model and I am trying to get contours like in this image. The contours I am expecting should be of same height preferably most common found height among the contours and should be parallel.

Expected Mask Image

But the contours I am getting are overlapping like in this image.

Output Mask Image

Sometimes the contours are of different length and at different heights. How to get the contours as in expected image? I tried a different kernel size but am finding it difficult to get.

Below is the code I used to convert output obtained from deep learning model.

se1 = cv2.getStructuringElement(cv2.MORPH_RECT, (8,8))
mask1 = cv2.morphologyEx(pred_col_mask.copy(), cv2.MORPH_OPEN, se1)

se2 = np.ones((8,8),np.uint8)
mask2  = cv2.dilate(mask1, se2)

se3 = cv2.getStructuringElement(cv2.MORPH_RECT, (8,8))
cleaned_out = cv2.morphologyEx(mask2, cv2.MORPH_OPEN, se3)

cnts = cv2.findContours(cleaned_out.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
0 Answers
Related