The watershed algorithm is a classic algorithm used for segmentation and is especially useful when extracting touching or overlapping objects in images, such as the coins in the figure above.
Using traditional image processing methods such as thresholding and contour detection, we would be unable to extract each individual coin from the image — but by leveraging the watershed algorithm, we are able to detect and extract each coin without a problem.
When utilizing the watershed algorithm we must start with user-defined markers. These markers can be either manually defined via point-and-click, or we can automatically or heuristically define them using methods such as thresholding and/or morphological operations.
Based on these markers, the watershed algorithm treats pixels in our input image as local elevation (called a topography) — the method “floods” valleys, starting from the markers and moving outwards, until the valleys of different markers meet each other. In order to obtain an accurate watershed segmentation, the markers must be correctly placed.
In the remainder of this post, I’ll show you how to use the watershed algorithm to segment and extract objects in images that are both touching and overlapping. To accomplish this, we’ll be using a variety of Python packages including SciPy, scikit-image, and OpenCV.
Looking for the source code to this post?
Jump Right To The Downloads SectionWatershed OpenCV
In the above image you can see examples of objects that would be impossible to extract using simple thresholding and contour detection, Since these objects are touching, overlapping, or both, the contour extraction process would treat each group of touching objects as a single object rather than multiple objects.
The problem with basic thresholding and contour extraction
Let’s go ahead and demonstrate a limitation of simple thresholding and contour detection. Open up a new file, name it contour_only.py
, and let’s get coding:
# import the necessary packages from __future__ import print_function from skimage.feature import peak_local_max from skimage.segmentation import watershed from scipy import ndimage import argparse import imutils import cv2 # construct the argument parse and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", required=True, help="path to input image") args = vars(ap.parse_args()) # load the image and perform pyramid mean shift filtering # to aid the thresholding step image = cv2.imread(args["image"]) shifted = cv2.pyrMeanShiftFiltering(image, 21, 51) cv2.imshow("Input", image)
We start off on Lines 2-8 by importing our necessary packages. Lines 11-14 then parse our command line arguments. We’ll only need a single switch here, --image
, which is the path to the image that we want to process.
From there, we’ll load our image from disk on Line 18, apply pyramid mean shift filtering (Line 19) to help the accuracy of our thresholding step, and finally display our image to our screen. An example of our output thus far can be seen below:
Now, let’s threshold the mean shifted image:
# convert the mean shift image to grayscale, then apply # Otsu's thresholding gray = cv2.cvtColor(shifted, cv2.COLOR_BGR2GRAY) thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] cv2.imshow("Thresh", thresh)
Given our input image
, we then convert it to grayscale and apply Otsu’s thresholding to segment the background from the foreground:
Finally, the last step is to detect contours in the thresholded image and draw each individual contour:
# find contours in the thresholded image cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) print("[INFO] {} unique contours found".format(len(cnts))) # loop over the contours for (i, c) in enumerate(cnts): # draw the contour ((x, y), _) = cv2.minEnclosingCircle(c) cv2.putText(image, "#{}".format(i + 1), (int(x) - 10, int(y)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2) cv2.drawContours(image, [c], -1, (0, 255, 0), 2) # show the output image cv2.imshow("Image", image) cv2.waitKey(0)
Below we can see the output of our image processing pipeline:
As you can see, our results are pretty terrible. Using simple thresholding and contour detection our Python script reports that there are only two coins in the images, even though there are clearly nine of them.
The reason for this problem arises from the fact that coin borders are touching each other in the image — thus, the cv2.findContours
function only sees the coin groups as a single object when in fact they are multiple, separate coins.
Note: A series of morphological operations (specifically, erosions) would help us for this particular image. However, for objects that are overlapping these erosions would not be sufficient. For the sake of this example, let’s pretend that morphological operations are not a viable option so that we may explore the watershed algorithm.
Using the watershed algorithm for segmentation
Now that we understand the limitations of simple thresholding and contour detection, let’s move on to the watershed algorithm. Open up a new file, name it watershed.py
, and insert the following code:
# import the necessary packages from skimage.feature import peak_local_max from skimage.morphology import watershed from scipy import ndimage import numpy as np import argparse import imutils import cv2 # construct the argument parse and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", required=True, help="path to input image") args = vars(ap.parse_args()) # load the image and perform pyramid mean shift filtering # to aid the thresholding step image = cv2.imread(args["image"]) shifted = cv2.pyrMeanShiftFiltering(image, 21, 51) cv2.imshow("Input", image) # convert the mean shift image to grayscale, then apply # Otsu's thresholding gray = cv2.cvtColor(shifted, cv2.COLOR_BGR2GRAY) thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] cv2.imshow("Thresh", thresh)
Again, we’ll start on Lines 2-8 by importing our required packages. We’ll be using functions from SciPy, scikit-image, imutils, and OpenCV. If you don’t already have SciPy and scikit-image installed on your system, you can use pip
to install them for you:
$ pip install --upgrade scipy $ pip install --upgrade scikit-image $ pip install --upgrade imutils
Lines 11-14 handle parsing our command line arguments. Just like in the previous example, we only need a single switch, the path to the image --image
we are going to apply the watershed algorithm to.
From there, Lines 18 and 19 load our image from disk and apply pyramid mean shift filtering. Lines 24-26 perform grayscale conversion and thresholding.
Given our thresholded image, we can now apply the watershed algorithm:
# compute the exact Euclidean distance from every binary # pixel to the nearest zero pixel, then find peaks in this # distance map D = ndimage.distance_transform_edt(thresh) localMax = peak_local_max(D, indices=False, min_distance=20, labels=thresh) # perform a connected component analysis on the local peaks, # using 8-connectivity, then appy the Watershed algorithm markers = ndimage.label(localMax, structure=np.ones((3, 3)))[0] labels = watershed(-D, markers, mask=thresh) print("[INFO] {} unique segments found".format(len(np.unique(labels)) - 1))
The first step in applying the watershed algorithm for segmentation is to compute the Euclidean Distance Transform (EDT) via the distance_transform_edt
function (Line 32). As the name suggests, this function computes the Euclidean distance to the closest zero (i.e., background pixel) for each of the foreground pixels. We can visualize the EDT in the figure below:
On Line 33 we take D
, our distance map, and find peaks (i.e., local maxima) in the map. We’ll ensure that is at least a 20 pixel distance between each peak.
Line 38 takes the output of the peak_local_max
function and applies a connected-component analysis using 8-connectivity. The output of this function gives us our markers
which we then feed into the watershed
function on Line 39. Since the watershed algorithm assumes our markers represent local minima (i.e., valleys) in our distance map, we take the negative value of D
.
The watershed
function returns a matrix of labels
, a NumPy array with the same width and height as our input image. Each pixel value as a unique label value. Pixels that have the same label value belong to the same object.
The last step is to simply loop over the unique label values and extract each of the unique objects:
# loop over the unique labels returned by the Watershed # algorithm for label in np.unique(labels): # if the label is zero, we are examining the 'background' # so simply ignore it if label == 0: continue # otherwise, allocate memory for the label region and draw # it on the mask mask = np.zeros(gray.shape, dtype="uint8") mask[labels == label] = 255 # detect contours in the mask and grab the largest one cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) c = max(cnts, key=cv2.contourArea) # draw a circle enclosing the object ((x, y), r) = cv2.minEnclosingCircle(c) cv2.circle(image, (int(x), int(y)), int(r), (0, 255, 0), 2) cv2.putText(image, "#{}".format(label), (int(x) - 10, int(y)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2) # show the output image cv2.imshow("Output", image) cv2.waitKey(0)
On Line 44 we start looping over each of the unique labels
. If the label
is zero, then we are examining the “background component”, so we simply ignore it.
Otherwise, Lines 52 and 53 allocate memory for our mask
and set the pixels belonging to the current label to 255 (white). We can see an example of such a mask below on the right:
On Lines 56-59 we detect contours in the mask
and extract the largest one — this contour will represent the outline/boundary of a given object in the image.
Finally, given the contour of the object, all we need to do is draw the enclosing circle boundary surrounding the object on Lines 62-65. We could also compute the bounding box of the object, apply a bitwise operation, and extract each individual object as well.
Finally, Lines 68 and 69 display the output image to our screen:
As you can see, we have successfully detected all nine coins in the image. Furthermore, we have been able to cleanly draw the boundaries surrounding each coin as well. This is in stark contrast to the previous example using simple thresholding and contour detection where only two objects were (incorrectly) detected.
Applying the watershed algorithm to images
Now that our watershed.py
script is finished up, let’s apply it to a few more images and investigate the results:
$ python watershed.py --image images/coins_02.png
Let’s try another image, this time with overlapping coins:
$ python watershed.py --image images/coins_03.png
In the following image, I decided to apply the watershed algorithm to the task of pill counting:
$ python watershed.py --image images/pills_01.png
The same is true for this image as well:
$ python watershed.py --image images/pills_02.png
What's next? We recommend PyImageSearch University.
84 total classes • 114+ hours of on-demand code walkthrough videos • Last updated: February 2024
★★★★★ 4.84 (128 Ratings) • 16,000+ Students Enrolled
I strongly believe that if you had the right teacher you could master computer vision and deep learning.
Do you think learning computer vision and deep learning has to be time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a degree in computer science?
That’s not the case.
All you need to master computer vision and deep learning is for someone to explain things to you in simple, intuitive terms. And that’s exactly what I do. My mission is to change education and how complex Artificial Intelligence topics are taught.
If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here you’ll learn how to successfully and confidently apply computer vision to your work, research, and projects. Join me in computer vision mastery.
Inside PyImageSearch University you'll find:
- ✓ 86 courses on essential computer vision, deep learning, and OpenCV topics
- ✓ 86 Certificates of Completion
- ✓ 115+ hours of on-demand video
- ✓ Brand new courses released regularly, ensuring you can keep up with state-of-the-art techniques
- ✓ Pre-configured Jupyter Notebooks in Google Colab
- ✓ Run all code examples in your web browser — works on Windows, macOS, and Linux (no dev environment configuration required!)
- ✓ Access to centralized code repos for all 540+ tutorials on PyImageSearch
- ✓ Easy one-click downloads for code, datasets, pre-trained models, etc.
- ✓ Access on mobile, laptop, desktop, etc.
Summary
In this blog post we learned how to apply the watershed algorithm, a classic segmentation algorithm used to detect and extract objects in images that are touching and/or overlapping.
To apply the watershed algorithm we need to define markers which correspond to the objects in our image. These markers can be either user-defined or we can apply image processing techniques (such as thresholding) to find the markers for us. When applying the watershed algorithm, it’s absolutely critical that we obtain accurate markers.
Given our markers, we can compute the Euclidean Distance Transform and pass the distance map to the watershed function itself, which “floods” valleys in the distance map, starting from the initial markers and moving outwards. Where the “pools” of water meet can be considered boundary lines in the segmentation process.
The output of the watershed algorithm is a set of labels, where each label corresponds to a unique object in the image. From there, all we need to do is loop over each of the labels individually and extract each object.
Anyway, I hope you enjoyed this post! Be sure download the code and give it a try. Try playing with various parameters, specifically the min_distance
argument to the peak_local_max
function. Note how varying the value of this parameter can change the output image.
Download the Source Code and FREE 17-page Resource Guide
Enter your email address below to get a .zip of the code and a FREE 17-page Resource Guide on Computer Vision, OpenCV, and Deep Learning. Inside you'll find my hand-picked tutorials, books, courses, and libraries to help you master CV and DL!
Pranav
Hi Adrian,
(Particularly for detecting circles say for example red blood cells) How does watershed algorithm compare to hough_circles?
-Pranav
Adrian Rosebrock
For detecting red blood cells, this method will likely perform better than Hough circles. The parameters to Hough circles can be tricky to tune and even if you get them right, overlapping red blood cells can still be missed.
JetC
Does this only work with round objects, or will it also work with squarish/oblong shapes? Thanks
Adrian Rosebrock
It will work with square/oblong objects as well.
C.W. Predovic
Does this accurately work for 3-D images?
Adrian Rosebrock
Yes, the watershed algorithm is intended to work with both 2D and 3D images. However, I’ve never tried using watershed with 3D images within OpenCV, only the ImageJ implementation.
Alexandre de Siqueira
Awesome post, Adrian! Simple and killing! Learned a couple things on this one!
I’d like to ask you two questions.
1) Do you know if there is a relation between “pyramid mean shift filtering” (PMSF) and “discrete wavelet transforms” (Mallat cascade algorithm)?
2) Could you tell what paper originated PMSF?
Thank you very much!
Adrian Rosebrock
Hey Alexandre — I’m glad you enjoyed the blog post, that’s great! To answer your questions:
1. Pyramid mean-shift filtering is not related to wavelet transforms. Perhaps you are thinking about Haar cascades for object detection?
2. As for the original paper, you’ll want to look up Comanicu and Meer’s 2002 paper, Mean shift: A robust approach toward feature space analysis
Alexandre de Siqueira
Hey Adrian,
thank you for that references! I downloaded them and will check when time is available 🙂
Thanks again! Have a nice one!
Taufiq
Hi, can try this source code in android sir ?
Adrian Rosebrock
If you need to use this code for your Android device, you’ll need to convert it from Python to Java (or another suitable language for Android). This is mainly a Python blog and I don’t do much Java development.
ghanendra
Hi Adrian
while installing scipy its showing this
It gets stuck at Running setup.py install for scipy What to do??
Adrian Rosebrock
What platform are you installing SciPy on? If it’s a Raspberry Pi, it can take up to 45 minutes to 1 hour to compile and install SciPy. be patient with the install.
Jaime Lopez
Hi Adrian,
How could I used Watershed algorithm on remote sensing image to detect objects, because I have too many different objects so I can not apply simple thresholding?
Thanks, Jaime
Adrian Rosebrock
It really depends on what your image contents are. Normally, you apply watershed on an image that you have already thresholded. If you cannot apply thresholding, you might want to consider applying a more advanced segmentation algorithm such as GrabCut. Otherwise, you could look into training a custom object detector.
Jon
Hi Adrian,
Great tutorial! I’m using watershed to segment touching objects so that I can track them frame by frame using nearest neighbor distances. Everything works pretty good except that sometimes there are too many new contours formed after watershed and I know that I can decrease this by increasing the min_distance parameter in peak_local_max but I need to have a low value because the objects are really small and I start losing contours if I increase the parameter.
The problem is that the labels (for tracking) for the objects get switched up because I’m comparing the current object’s centroid to contour centroid’s that aren’t part of the same object. Do you have any advice for combining contours on a single object and getting an average centroid to compare to? Any help is much appreciated!
Adrian Rosebrock
That is quite the problem to have! Merging contours together is normally done by heuristics. You can compare adjacent watershed regions and compare them based on their appearance, such as texture or color. Regions with similar appearances can be merged together. In this case, you would generate a new mask for the merged objects and compute their corresponding centroid. Alternatively, if you have both contour variables handy, you should be able to compute the weighted (x, y) spatial coordinates to form the new centroid.
Josimar Amilcar Fernandes Andrade
I Rosebrock great work I bean follow you for a long time.
I have a problem with creating color gradient-weighted distance, can you help me.
Josimar Amilcar Fernandes Andrade
my objective is to get the separation lines
Adrian Rosebrock
Can you elaborate more on what you mean by “color gradient-weighted distance”?
Wanderson
Hi Adrian,
Can I use the watershed algorithm to segment a group of people walking together? The images must be captured by a video camera installed on the ceiling. I performed tests with GMM and KNN, but I got no success.
Thanks, Wanderson
Adrian Rosebrock
If you have a mask that represents the foreground (the people) versus the background, then yes, I would give the watershed algorithm a try. However, you might need a more powerful approach depending on your scene. I could foresee utilizing a custom object detector to detect each of the people individually instead of background subtraction/motion detection.
Wanderson
I appreciate your reply. I will direct my research from here.
Thank you!
Tim Brooks
Great article, Adrian
I am getting sometimes wrong results and would like to debug. What was used to visualize the Euclidean Distance Transform (fig. 5).
Thanks
Tim
Adrian Rosebrock
I actually used matplotlib for that visualization.
David
Been reading your tutorials and will be purchasing the opencv book, really good stuff.I have one question:
The watershed works by specifying a starting point to the algorithm. In your case this is done by an Euclidean distance from the background color (which is very dark) compared to the objects of interest (coins, pills).
I would like to use the watershed, but have a somewhat uneven specular background (clear plastic) which goes from almost white to very dark (even in the best diffuse lighting).
Any suggestions as to segmenting pills, coins or candy in such a scenario?
Thanks!
Adrian Rosebrock
Hey David — it’s great to hear you are enjoying the PyImageSearch blog! Regarding your question, do you have any example images of what you’re working with? That might be easier to provide a solution on techniques to try.
Philip Hahn
David – How did you generate the distance map in “Figure 5: Visualizing the Euclidean Distance Transform.”? An imshow of D looks identical to thresh. Thanks!
Adrian Rosebrock
Are you asking me or David? Figure 5 was generated using matplotlib and a plot of the distance map.
Miguel
Hi Adrian,
Great article. I’m trying to segment touching bean seed using the code that you posted,
in some cases seeds are well segmented, but in others the beans are splited.
I was decreasing and increasing the min_distance parameter, but i could not segmented the beans. Please, can you suggest me what can i do in that cases
these are my images:
https://s14.postimg.org/7371ox9sx/beans.png
https://s27.postimg.org/vk0x2zo37/Img0878.png
Thanks
Adrian Rosebrock
Hey Miguel — I can clearly see the beans touching in the second image. But what is the first image supposed to represent? The beans after segmentation?
Miguel
Hi Adrian
Yes, the first image represents the beans after segmentation. After obtaining the contours, i draw the segmented beans one by one. As I told you before, in some cases the beans are segmented correctly.
Thanks
Adrian Rosebrock
You’ll likely have to continue to fiddle with the thresholding parameters along with the Watershed parameters. There isn’t a one-size-fits-all solution when using these parameters. More advanced solutions would include using machine learning to do a pixel-wise segmentation of the image, but that’s a bit of a pain and I would try to avoid that.
Nada
Hi Adrian,
Hi i’m a beginner in opencv with python, I’m trying to use the code that you posted but i get this error :
error: argument -i/–image is required
Please, can you tell me what can i do
Thanks
Your comment is awaiting moderation.
Adrian Rosebrock
You need to read up on command line arguments before proceeding.
Ian V.
Hi Adrien,
I have to do a documentation about a programm that i have written in python. For a clean documentation, i would like to know how you displayed codefragments in a box with line numbering?
Thanks
Adrian Rosebrock
Hi Ian — the code fragments displayed in this blog post are handled by a WordPress plugin I use.
shyam
hi adrian,
is there any solution for objects( irregular shape) other than coins
Adrian Rosebrock
Absolutely. I cover shape detection here. Otherwise you might want to train a custom object detector.
Akash Kumar
Hi Adrian,
It’s a great and perfect tutorial. I would like to know what does that [1] mean and even in the contours [-2]? I am new to opencv.
thresh = cv2.threshold(gray, 0, 255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[-2]
What is the difference between cv2.THRESH_BINARY|cv2.THRESH_OTSU and cv2.THRESH_BINARY+cv2.THRESH_OTSU?
Thanks for the help.
Adrian Rosebrock
The
cv2.threshold
function returns a 2-tuple of the threshold value T used (computed via Otsu’s method) and the actualthresh
image. Since we are only interested in thethresh
image, we grab the value via [1]. This is called Python array indexing. I would suggest reading up on it.Also, my recommended way to extract contours via OpenCV 3 and OpenCV 2.4 is now:
This will make it compatible with both OpenCV 2.4 and OpenCV 3.
As for your last question the vertical pipe “| is a bitwise OR.
max
This site is an invaluable resource. Thanks for the thorough and lucid explanation of the watershed algorithm. I’m wondering if you can help me filter the set of contours returned by cv2.findContours(). Essentially, what I want is the set of contours that _do not_ share a boundary with other contours. I know this sounds contrary to the problem watershed is meant to solve, but my requirement is similar to the following problem: Given a picture with a number of coins (as in your example) some touching and some completely isolated, return the set of contours for the isolated coins only and exclude the return of any contours that are touching each other. Thanks for your help!
Adrian Rosebrock
I’m happy to hear you are enjoying the PyImageSearch blog, Max!
My suggestion here is to take the output contours, draw them, and then apply a connected component analysis. This will help you determine which contours touch.
I don’t like OpenCV’s connected component analysis function as much as the scikit-image one, so I would suggest starting there.
Carl.C
Hi, Adrian.
Great tutorial. I would like to ask why two identical pictures get different result?
I downloaded the coins picture straight from this website (Figure1, jpg format), and ran on it. It turned out to be 10 coins instead of 9, and #3 is missing, also said “[INFO] 10 unique segments found”.
Then I downloaded your source code and ran on the original picture(png format), it’s 9 coins.
I can’t figure it out because they really look identical.
It is normal for random mistakes? Or the result somehow relys on the picture format/ picutre quality?
Adrian Rosebrock
Which version of OpenCV are you using? There are minor differences between the versions that can cause slight differences in results. Furthermore, keep in mind that OpenCV is heavily dependent on a number of pre-req libraries, such as optimization packages, libraries used to load various image file formats, etc. Unless explicitly configured, no two computer vision development environments are 100% exact, so these differences can compound and sometimes lead to different results.
John Goodman
Hi Adrian,
I’m running Python 3.6.1 and OpenCV 3.2.0 and I’m seeing the same results. What’s happening is that the top left nickel is being counted twice as (#2 and #3).
Is there a way to tune for this by tweaking the filtering, thresholding or something else?
example: https://i.imgur.com/vHlh4mU.png
Anyway, Thanks for the great blog and book!
Adrian Rosebrock
Thanks for sharing the screenshot, John. I appreciate it. You would need to do some tweaking to the parameters here, I would have to play with the code to determine what actually needs to be changed. I’ll try to check this out and get back to you.
Luana de Oliveira
Hello Adrian,
Thank you very much for the tutorial. It’s great and it has helped me a lot up here.
I added at the end of the code a simple function to get the coordinates (x, y, and r) of the centroid of the circles
My current problem is that I’m trying to use this code in georeferenced images (tiff), to get the UTM coordinates (x, y and radius in meters) of each centroid at the end. I tried the gdal, but I could not. Would you have any tips?
Thank you!
Adrian Rosebrock
Hi Luana — unfortunately my experience with georeferenced images is pretty minimal, so I’m not sure what the best solution to the problem is. Sorry I couldn’t be of more help here!
shubham
hello Adrian! i have also tried this code but after running the first segment of code its giving output but no image is showing at all.only a window with gray background.
Please help me…
Adrian Rosebrock
That is indeed strange behavior! What version of OpenCV are you using? And how did you install OpenCV on your system?
vinayak
Hello Adrian,
i want to automatically segment some specific object if it is present in an image, for example dress, shoes,etc. Will this algorithm work for such a use case. I have implemented a pipeline in deep learning using fcn-image segmentation. It works well. Just wanted to check if watershed algorithm can be used in such a use case also? here images are unknown.
Adrian Rosebrock
If your deep learning-based segmentation pipeline can output masks for the objects in the image then I would give watershed a try. But again, it really depends on how heavy the overlap is. Really good deep learning segmentation algorithms can actually perform the overlap segmentation.
Garnies Hafitma
how can fix it??
usage: contour_only.py [-h] -i IMAGE
contour_only.py: error: argument -i/–image is required
just try i from senior high school, i didn’t have any information about this hehe
Adrian Rosebrock
It’s great to hear you are getting involved with programming and OpenCV in high school. The issue you are running into is due to command line arguments. You need to supply them when executing the script via the command line. Please take some time to educate yourself on command line arguments before continuing.
Meg
Hi Adrian,
In your code, you use “labels = watershed(-D, markers, mask=thresh)”
When I look at the OpenCV documentation, I only see two parameters, the input and the markers. Can you tell me where you get the third parameter from?
https://docs.opencv.org/3.0-rc1/d7
/d1b/group__imgproc__misc.html#ga3267243e4d3f95165d55a618c65ac6e1
Thanks much!
Adrian Rosebrock
We’re using the scikit-image implementation of Watershed, not the OpenCV implementation.
Meg
Thank you! One other question I had – what if my background changes? E.g. I want to use this code if the background is white and my coins are darker. If I manually change the threshold to Binary Inverse it works, but do you have any suggestions on how to automatically detect this?
Adrian Rosebrock
I would suggest using Otsu’s method for thresholding. Additionally you could manually do this yourself and compute a histogram of pixel intensities. Assuming there are more background pixels than foreground you can check the count of darker vs. lighter pixels and determine the correct threshold flag.
Jared Turpin
It is unclear to me why you there are two separate implementations of the watershed algorithm. What rationale did you use to select the scikit version over the OpenCV version?
Adrian Rosebrock
When this blog post was published OpenCV did not have an easily accessible watershed function with Python bindings. With the OpenCV 3 release; however, the watershed function became more accessible. Because of this, I used the scikit-image version when writing this post.
Tony Holdroyd
Hello Adrian, my project involves recognising , and segmenting,tumours in brain scans where there is quite a bit of noise in the image, including a skull outline. We want to detect a whitish patch against a darkerish background. We have tried a DL approach, but with limited success, and I was wondering if you could advise us, please, if we should put our efforts into the watershed function, or some other OpenCV, or indeed sci-kit, technique. Thanks, Best, Tony
Adrian Rosebrock
Hey Tony — do you have any example images that I could take a look at? Additionally, what deep learning approach did you use?
Ram
hey, i am a beginner. can you suggest how to find the performance of different image segmentation algorithms. I have the output of watershed, kmeans, thresholding. how to find which algorithm is best ?
Adrian Rosebrock
Typically you would need the ground-truth of what the correct segmentation looks like. From there you would compute the Intersection over Union for the resulting masks.
trya sovi
Hello adrian, can you explain why ” usage: contour_only.py [-h] -i IMAGE
contour_only.py: error: the following arguments are required: -i/–image ” error messages? thankyou.
Adrian Rosebrock
If you are new to command line arguments that’s okay but you will need to read this blog post first.
Ata
Dear Adrian,
Thank you for your great projects that you are sharing,
I’m new with python.
I followed the procedure as you had mentioned here.
there is a problem which relates to skimage .
I can not use it in Python while I have installed in several way like ;
sudo apt-get install python-skimage
but still receive same error as;
ImportError: No Module named skimage
But When I run the python in LXTerminal then I can import these without any error.
Can you help me with this?!
Adrian Rosebrock
Are you using a Python virtual environment to install scikit-image? If so, make sure you access your Python virtual environment before you install it:
The “apt-get” command will install your Python packages into the system Python which you likely do not want.
Roshan
Hi Adrian,
I am trying to count the number of seeds from the image but the background is gray instead of black as in your example and thus I am not able to detect unique segments. I would like to know the best way to deal with this.
Thanks.
Adrian Rosebrock
You may need to change and manually tune the threshold values you are using for your input image.
Bobby
Are you using a light box for this or what material is being used for the table-top black background color? Have a product name or link?
Adrian Rosebrock
It was actually just my coffee table (my coffee table is a dark expresso color).
Kundan
Hi Adrian,
Thanks for the wonderful tutorial.
I need to calculate particle size distribution by calculating the sizes of fragments/ pieces in a given image, I hope you would point me in the right direction.
Regards
Raftaar
Hello Adrian,
What is the benefit of performing bitwise OR vs just adding Otsu thresholding (+) to THRESH_BINARY i.e
cv.THRESH_BINARY+cv.THRESH_OTSU vs cv.THRESH_BINARY | cv.THRESH_OTSU
Adrian Rosebrock
A bitwise OR is different than an add. There has been confusion regarding this in the OpenCV documentation so I believe the developers just made both values and both will perform Otsu thresholding.
Raftaar
Hello Adrian,
If I remove the square brackets around c here on Line 39:
cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
The contours have lot of gaps in them.
Can you please let me know why is that the case
Adrian Rosebrock
I’m not sure what you mean by “gaps” here. Why are you trying removing that line?
shahbaz sharif
hey bro is there any way to get this work with topographic results of eye { topographer }
will be very happy for the ans 🙂
harry
how to plot the distance map?
yaya
hai adrian,
Thank you for your great projects that you are sharing,
I’m new with python.
I followed the procedure as you had mentioned here.
my project involves to study the technique of image processing to increase the potential of identifying tree species, focusing on the commercial species using drone imagery. we want to detect the species for tropical forest. any suggestion..
tq so much for your answer
Adrian Rosebrock
It’s hard to say without seeing example images/video of what you’re working with. If you can share some example images I can try to take a look.
Hassan
Hi Adrian thanks for the great tutorial, I have images with black overlapping circles on a white background and would like to detect them as you did here, but I don’t know what changes I should make to let the code work again
Any help would be appreciated
Adrian Rosebrock
Just invert the image to make the foreground white pixels:
image = cv2.bitwise_not(image)
Omar
Hi there Adrian,
We have an image dataset for brain tumors and we also have some of the data segmented to be able to start using a deep learning architecture for our model. My question is, is this algorithm capable of extracting contours and mapping it on the original images? If so, do you might have a resource or tutorial for that matter or will refactoring the code snippets here would be enough?
Many Thanks
Adrian Rosebrock
Hey Omar — if your goal is to apply deep learning + segmentation then you should utilize instance segmentation. Deep Learning for Computer Vision with Python covers instance segmentation via Mask R-CNNs. The book takes a medical focus as well, showing you how to train a Mask R-CNN for skin lesion/cancer segmentation as well as prescription pill segmentation. Give it a look, I believe it would really help you with your project.
Henrique
Hi Adrian, is there a way to count the area of each coin in order to be able to classify each coin with its respective value?
Adrian Rosebrock
Yes, but the first step would be to recognize the count itself. Most valued coins have different sizes, therefore a good method would simply to be measure the coin size.
Simone
Hi Adrian,
Thank you very much for your great tutorials! This example is particularly interesting as it works much better than the one in the openCV tutorial, at least for my dataset.
Just a tip for anyone interested in improved performance, above all when you are dealing with thousands of objects: setting the parameter watershed_line to True in the watershed function will mark the basins’ borders with the label 0 (background). Setting thresh[labels == 0] = 0, you can directly call findContours on thresh without the expensive for loop.
Hope that helps, and thank you again.
Adrian Rosebrock
Thanks for sharing, Simone!
sohini goswami
Hi Adrian,
I have some grains [wheat], can this algorithm work for segmenting the grains which are touching each other? Or do I have to use the Mask R CNN approach?
Adrian Rosebrock
Mask R-CNN may be overkill but it’s hard to say without seeing your images first. Give both a try and then let your empirical results guide you further.