Looking for an Expert Development Team? Take two weeks Trial! Try Now

Watershed Algorithm and its application

banner

Introduction

Watershed algorithm is used for segmentation in some complex images as if we apply simple thresholding and contour detection then will not be able to give proper results.

Watershed algorithm is based on extracting sure background and foreground and then using markers will make watershed run and detect the exact boundaries. This algorithm generally helps in detecting touching and overlapping objects in image.

For markers, it can be user defined like manually clicking and get the coordinates for markers and also using some defined algorithms such as thresholding or any morphological operations.

Due to the presence of noise, we can’t apply watershed algorithm directly.

Let’s dive into the limitation of contour detection.

Limitation of contour detection for image segmentation

1. import cv2 2. import numpy as np 3. img = cv2.imread('images/water_coins.jpg') 4. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 5. cv2.imshow('image', gray) 6. ## Applying dilation for sure_bg detection 7. ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) 8. cnts, heir = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 9. img2 = img.copy() 10. cv2.drawContours(img2,cnts,-1,(0,255,0),3) 11. cv2.imshow('contour', img2) 12. cv2.waitKey(0)

Output -:

image segmentation

As we can see from the above output, contour detection is not able to do the proper segmentation because of the joint coins. This is where segmentation algorithm like watershed comes into picture.

Watershed algorithm for image segmentation

These are the following steps for image segmentation using watershed algorithm -:

Step 1: Finding the sure background using morphological operation like opening and dilation.

Step 2: Finding the sure foreground using distance transform.

Step 3: Unknown area is the area neither lies in foreground and background and used it as a marker for watershed algorithm.

Background extraction

1. import cv2 2. import numpy as np 3. img = cv2.imread('images/water_coins.jpg') 4. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 5. cv2.imshow('image', gray) 6. ## Applying dilation for sure_bg detection 7. ret, thresh = v2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) 8. ## Defining kernel for opening operation 9. kernel = np.ones((3,3), np.uint8) 10. opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=2) 11. cv2.imshow('Open', opening) 12. ## After opening, will perform dilation 13. sure_bg = cv2.dilate(opening, kernel, iterations=3) 14. ## Sure background image 15. cv2.imshow('dilated', sure_bg)
Background extraction
Background extraction dilated
Background extraction bg

Foreground extraction

1. dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5) 2. ret, sure_fg = cv2.threshold(dist_transform,0.7*dist_transform.max(),255,0) 3. sure_fg = np.uint8(sure_fg)

Finding the unknown area(Neither sure foreground and sure background)

1. unknown = np.subtract(sure_bg, sure_fg) 2. cv2.imshow('unknown', unknown)
Background extraction unknown area

Applying watershed algorithm

1. ret, markers = cv2.connectedComponents(sure_fg) 2. print(markers) 3. ## Add one so that sure background is not 1 4. markers = markers +1 5. ## Making the unknown area as 0 6. markers[unknown == 255] = 0 7. #cv2.imshow('markers2', markers) 8. cv2.waitKey(0) 9. markers = cv2.watershed(img, markers) 10. ## boundary region is marked with -1 11. img[markers == -1] = (255, 0, 0) 12. cv2.imshow('watershed',img) 13. cv2.waitKey(0)
Background extraction watershed algorithm

Conclusion

In this blog, we have discussed the image segmentation algorithm with the limitation of contour detection.

We have seen how to use watershed algorithm with the processing of image using machine learning development solutions.

Read More:

DMCA Logo do not copy