How it works...

The cv2.watershed function implements the algorithm and takes two arguments, the image to segment and the initial seeds. The segmented image should be color and 8-bit. Seeds should be stored in the image, with the same spatial size as the segmented image, but with only one channel and a different depth, int32. Different seeds should be represented in the second argument, with different numbers, and other pixels should be set to zero. The routine fills zero values in the seed image with relevant neighbor seeds.

After launching the code from this recipe, you will see an image similar to the following:

Get OpenCV 3 Computer Vision with Python Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.