Increasing segmentation depth using Watershed

asked 2018-09-19 00:39:42 -0600

RUCasul gravatar image

updated 2018-09-19 00:40:58 -0600

Hi all,

I'm trying to segment images containing a lot of individual objects. I'm applying the watershed algorithm to get the effect I want.

image description.

As you can see, the watershed algorithm is very good at picking out single objects. However, you can also see clumps of the same object in the picture. I want to extract each image as well as possible.

Can I get some guidance as to the best tactic? My code for performing the segmentation is below. I apply a few morphologies to open and close the fg/bg and apply a threshold to the image. I proceed by picking out the white parts of the threshold and using them and obtaining lables from them:

   kernel = np.ones((3,3),np.uint8)
  gray = cv2.GaussianBlur(image, (15, 15), 0)
gray = cv2.cvtColor(gray, cv2.COLOR_BGR2GRAY)
if invert:
    thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 115, 1)
else:
    thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 115, 1)
closing = cv2.morphologyEx(thresh,cv2.MORPH_CLOSE,kernel, iterations = 2)
opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2)


sure_fg = cv2.erode(closing,kernel,iterations=2)
sure_bg = cv2.dilate(opening,kernel,iterations=2)

dt = cv2.distanceTransform(sure_bg,cv2.DIST_L2,5)
dt = ((dt - dt.min()) / (dt.max() - dt.min()) * 255).astype(np.uint8)
dt = cv2.normalize(dt, None, 255,0, cv2.NORM_MINMAX, cv2.CV_8UC1)
ret, sure_fg = cv2.threshold(dt,int(float(md_lim)/(100.0)*dt.max()),255,0)

sure_fg = np.uint8(sure_fg)
unknown = cv2.subtract(sure_bg,sure_fg)

# Marker labelling
ret, labels = cv2.connectedComponents(sure_fg)

# Add one to all labels so that sure background is not 0, but 1
labels = labels+1

labels[unknown==255] = 0

labels = cv2.watershed(image,labels)
image[labels == -1] = [255,0,0]
edit retag flag offensive close merge delete