Hi and welcome back to Chapter 22, a Section 22, where we take a look at the watershed algorithm,
which is basically a segmentation algorithm similar to find contours, which we'll explore now.
So let's do this stuff.
So now that's open.
Let's load a little files and libraries and the image and show a function.
And while loads.
I'll explain the watershed algorithm in principle.
So imagine we have an image like this where it's a bunch of coins here what what digital your algorithm
does.
The reason it's called watershed is because it looks for low points or dark areas in an image.
And those pictures dark areas to be valleys so the valleys will fill with water if you were to actually
apply water to that, that geographical scene.
So similarly, with these types of images, the darker areas have become filled with water.
And that's how you actually have a segmentation between this.
So what this allows us to do is, firstly, before I even go into that, imagine we're trying to do
use to get the contours around each coin here.
Now we could use something like half circles to find this.
But what if we want to do something else faster and quicker and more resilient?
So we wanted to use fine contours, but fine contours wouldn't work because look what happens?
You can actually get.
You actually have these images, these moving centers here.
So that's going to be a problem.
So how do we remove those touching mass?
Well, one way we can do it is by dilation as well.
So we can use them dilation and then we can using something called finding to show foreground here you.
Which basically means that what we're trying to find, the area that we're showing is to foreground.
So we actually what we do here.
Firstly, we perform some morphology x here, which which applies simple blur or noise removal to the
image here.
So the next step is we need to find a sure background area.
And to do that, we just actually have to sort of dilate this image here with a kernel of tree by tree.
And we do have tree times, which is why this is a show foreground, which I'll explain shortly.
But this is why it looks like this and this short background, which you don't have to.
I don't have to show a background split.
But we can take a look at it.
No, actually, it would be good for us to do.
So let's plot as well.
So let's not run.
This could or didn't do the stuff beforehand.
Actually, make sure you run these cellblocks before running to code the images out there because it
saves your lasso, but so often make that mistake.
All right.
So you can see this is a threshold image here, which actually should be sure.
Background Let's run this again with the appropriate titles.
OK, good.
So now we have these images here, so we have to show the image here, which is our short foreground,
which actually lets us called Show F G for future reference.
And we have a show background on knowing.
So I'll explain the rest of the code, but just to show you illustrating what we're getting so far.
We have to show foreground here.
We have the sure background here and then we have the unknown areas.
So there is a basically to mask here as well.
So let's go back up to just good.
So to deter it, to get to show off foreground, we used it silly to distance transform as well.
Here we use the parameters that open if we specifies in the documentation for this application.
This is a watershed application.
And then we multiply it by the MAX by 0.7 and we apply the threshold here.
So that's how we get our threshold value and that's what we get to show foreground.
So we can put the region here and then we subtract the show background from the show foreground to get
the unknown area here.
So that's basically what we've done so far, and that's really annoying area looks like this.
So the whole point of that was actually to obtain this image.
This new image here.
So what we do, we actually run the connected components, which is basically it's an algorithm that
shows the connectivity of blood like regions in a binary image.
So now that we have that, we have the regional markers that unknown eliminated here.
So then we can now apply the watershed algorithm.
So it seems like a lot of work to get to this point, and it's actually not that intuitive.
However, just follow these steps in these guidelines to implement this if you need to.
And what we do here markers that is minus one.
We actually just make them this value as well.
So that's how we can.
Actually, this is blue, so we actually make the markers blue.
I should say.
Sorry for that.
So no, we can draw circles around the coins.
Exactly.
And we don't have that.
We do have some of them joining like this bit that it's actually not fully joined.
It actually has the boundary here.
So they're all not joined, which is what we wanted and wanted to extract.
These are objects here separately.
So this achieves the purpose here.
So whenever you need to do a segmentation of of things that do overlap quite a bit, you can use this
algorithm to implement that segmentation.
So hopefully from this lesson in useful will then move on now to background and foreground subtraction.
Thank you.