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:

Let's select the regions depending on their topographic surface:

If you run the watershed algorithm on this, the output will look something like this:

Sample code can be found on the link given previously, along with many other applications of the watershed algorithm.Â