Display the segmentation of watershed algorithm

I am a newbie to Opencv, I have recently been using the watershed algorithm, Currently i have tried to segment the images using different color markers,but i wanted to display the segments as well.

Is there any method by which this can be done, as we can do it in Simple Linear Iterative Clustering super-pixel segmentation using Simple Linear Iterative Clustering function and mark boundaries function

Using Opencv and python.


1 answer

  • answered 2018-05-16 15:30 Jeru Luke

    First follow THIS DOCUMENTATION to obtain the markers in the image. I used the same image present in this documentation.

    After that follow this code line-by-line and visualize what each variable contains:

    Each unique connected component is assigned a unique value in the variable markers. I am normalizing those values present in markers to an integer. Each connected component can be obtained at a particular threshold value mentioned in the list uniq_thresh.

    label_hue = np.uint8(179*markers/1)
    uniq_thresh = np.unique(label_hue).tolist()
    cv2.imshow('label_hue', label_hue)

    prev is an image having the same shape as the original image but with pixels of value 0 throughout.

    prev = np.zeros_like(label_hue)

    Now for each of the unique threshold value in the list uniq_thresh I am displaying the connected component. But since thresh returns an image having componets above a certain threshold I am subtracting the previous threshold image from it in order to obtain a single component.

    for th in uniq_thresh:
        ret, thresh = cv2.threshold(label_hue, th, 255, cv2.THRESH_BINARY)
        cv2.imshow('ind', thresh)
        cv2.imshow('ind2', thresh - prev)
        prev = thresh