Watershed algorithm
OpenCV comes with default implementations of watershed algorithms, at https://docs.opencv.org/trunk/d3/db4/tutorial_py_watershed.html, which theory says that any grayscale image can be viewed as a topographic surface where high intensity denotes peaks and hills, while low intensity denotes valleys. This algorithm is pretty famous and there are a lot of implementations available out there.
Consider the following image:
![](https://static.packt-cdn.com/products/9781788396905/graphics/0eabf14f-ae2e-4ee2-85bd-6cf955d1d451.jpg)
Let's select the regions depending on their topographic surface:
![](https://static.packt-cdn.com/products/9781788396905/graphics/3686e24f-de1a-45d0-b181-057c6d725017.png)
If you run the watershed algorithm on this, the output will look something like this:
![](https://static.packt-cdn.com/products/9781788396905/graphics/84c2ef6a-1870-41d3-b4a1-e0056e3e4a2d.png)
Sample code can be found on the link given previously, along with many other applications of the watershed algorithm.Â