Watershed Algorithm and its Application For Image Segmentation



A Watershed algorithm is used for segmentation in some complex images 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 an image.

For markers, it can be user defined by manually clicking and getting 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 algorithms 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)


image segmentation
Are you facing problems with segmentation of images?

Our expert team will assist you by using watershed algorithm and tries to segment the complex images.

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 algorithms like watershed come 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 Nor for 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

A guide to diverse applications of Multidimensional Image Processing

With profuse data visualization, imaging processes are undergoing disruption. Deep learning technology has entered the area with a new angle of multidimensional image processing. The applications in various verticals are undeniable too. Transforming images digitally with specific functionality expands its applications. From simple 2-D to 3.D and now multidimensional properties have augmented the entire process.

This blog revisits the main types of image processing and examples related to the creation/segmentation of binary images, quantum dots, and steel grains.

Five types of imaging processes for accuracy and location

  • Visualization-to detect objects not clear in the image
  • Recognition- To distinguish objects in the image
  • Sharpening and restoration- Enhance the original image by sharpening it
  • Pattern recognition- Measure various patterns around the objects
  • Retrieval- search digital images similar to the original image

When the image is acquired, it undergoes processing steps and they relate to:

  • Acquisition
  • Enhancement
  • Restoration
  • Coloring
  • Multiresolution
  • Compression
  • Morphing

Applications Multidimensional Image Processing

We visualize several objects digitally at any given moment. Due to image segmentation, our eyes can distinguish the uniqueness of the shape. It helps in annotating and uses technology it makes it more appealing to the eyes. Medical image is one such example that has moved from 2D to 3D and now multidimensional angles.

Segmenting a Binary Image

A binary image has a pixel of two intense values, i.e. black, and white. They are ideal for image processing as they can separate an object from its background. With segmentation, it is possible to put a label on each pixel- ‘background’ and the ‘object.’ A binary image is sourced from a grayscale by thresholding. A histogram of intensities is created; the threshold indicates pixels that fall into ‘white’ or ‘black.’ Further steps include extraction of background from the object or number of objects. It is done with the connected component technique.

Segmenting the Quantum Dots

Various facets of science have used quantum dots the tiniest specks that are concentrated in a single part. They are also considered 0-dimensional. Currently, nanotechnology is using this process to deliver quantum dots, which are also called ‘artificial atoms.’ In addition, where are they applied? Right from our home lighting systems (as they are made of semiconductor material) to detecting biological warfare detectors and solar cells. Pretty much, all is possible, as they make light through color with wavelength and frequency. They are best used for optical images and are good alternatives to pigments and dyes in reflective paints. In the case of solar cells, this is a breakthrough technology.

Segmenting Steel Grains

Multidimensional image processing can well be used effectively in segmenting steel grains. In a good image, the light and dark areas are quite distinctive. In the process, over-segmentation brings bad results. If such a problem occurs, the minima can be removed to modify the image. This is called watershed transformation. With the introduction of Austenitic steel and other micro-alloys, the grain of steel assumes importance for some industry verticals. To have the correct structural components segmenting steel grains is critical. It affects the hardness, durability, and strength. Image enhancement ensures that the right grade of steel is made after grain analysis.

For further information on multidimensional image, processing and its diverse applications do let us know. We shall be glad to share our knowledge and experience.


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

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

Related article

Docker is one of best tools for containerization tool for creating images. Docker provide the layered architecture, using which it will not take much space

An interdisciplinary field, data science uses scientific systems, algorithms, processes, and other methods to gain insight and knowledge from data in different forms,

Creating an algorithm that can learn from data to make a prediction is what Machine Learning is all about.

DMCA Logo do not copy