title: Get Edges/boundary for overlapping image
body: <p>I have a mask for a dental x-ray <a href="https://i.stack.imgur.com/bEv7q.png" rel="nofollow noreferrer">here</a> where all teeth are overlapping with each other. I want to count the number of teeth present in the image for that I want to separate overlapping tooths so I can use contour-based approach to count the number of tooths, I tried following approach but it is giving result like <a href="https://i.stack.imgur.com/6DnKk.png" rel="nofollow noreferrer">this</a>
  . how can I extract boundary for each tooth?</p>
  <pre><code>from skimage.feature import peak_local_max
  from skimage.morphology import watershed
  import matplotlib.pyplot as plt
  from scipy import ndimage
  import numpy as np
  import cv2
  
  def getImageEdge(input_image):
      img_gray = input_image
      image_black = np.zeros(shape=input_image.shape, dtype=&quot;uint8&quot;)
      thresh = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
      thresh_copy = thresh.copy()
      D = ndimage.distance_transform_edt(thresh_copy)
      localMax = peak_local_max(D, indices = False, min_distance = 12, labels = thresh)
      markers = ndimage.label(localMax, structure = np.ones((3, 3)))[0]
      labels = watershed(-D, markers, mask = thresh_copy)
      for label in np.unique(labels):
          if label == 0:
              continue
          mask = np.zeros(img_gray.shape, dtype = &quot;uint8&quot;)
          mask[labels == label] = 255
          contours, hierarchy = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
          cv2.drawContours(image_black, contours, -1, (255, 255, 255), 1)
      return image_black
  
  inputImage = cv2.imread(&quot;/content/dentalMask.bmp&quot;)
  inputImage = cv2.cvtColor(inputImage, cv2.COLOR_BGR2GRAY)
  outputImage = getImageEdge(inputImage)
  plt.imshow(inputImage)
  plt.show()
  plt.imshow(outputImage)
  plt.show()
  </code></pre>
  <p>EDITED:</p>
  <p>Based on the answer from fmw42 I have added one more image where it is showing more overlapping and failed in simple thresholding and contour-based approach.</p>
  <p><a href="https://i.stack.imgur.com/W51s7.png" rel="nofollow noreferrer">input</a>
  <a href="https://i.stack.imgur.com/prmaD.png" rel="nofollow noreferrer">output</a></p>