Hi and welcome to Chapter 10, where we take a look at dilation, erosion and edge detection.
So let's go ahead and open this notebook.
Already have it open here and you can see there's two more things added to the to this topic that we're
learning here.
It's going to be opening and closing, which are actually combinations of dilation and erosion, but
you don't know what the dilation and erosion is yet.
So let's take a look at this.
So firstly, let's download our images and see all the major functions and all libraries, and that's
done.
So let's take a look at the definitions here.
So let's zoom in a bit.
Dilation adds pixels to the boundaries of an object in the image.
Erosion removes pixels at the boundaries of an object, and opening is this erosion, followed by dilation
and closing this dilation followed by erosion.
So you can see this illustrated right here.
This is the original image here.
This is a number five.
You can see the downward erosion issue, which we look at this.
It removes the pixels from the boundaries.
So all of these things that have boundaries here are gone.
You can see it.
This first column has gone.
This top arrow here is gone as well.
This one is gone, so everything basically thins out.
So that's what erosion does.
It erodes all the pixel around the objects.
You only left with this in a line here, which luckily still looks like five just a very thin.
The five dilation does the opposite dilation padgett of additional pixels around the boundary here,
basically fattening it up to make it look like this.
So that's pretty simple.
But it's important to realize, though, that remember, I'm using zeros and ones here, but it's really
zero and 255 for 255 wouldn't have fit here.
So this relative binary scale, remember this is the white is what the algorithm sees as the image,
as the object.
I should say yes, because a lot, lot, lot of times people have people who frequently think if it's
a black line, like if this was inverted, inverted and it was a black five on a white background,
if you wrote it, you would get a thinner five when in fact you would get the opposite people, lots
of people for a long time to open to the open to these algorithms.
What's in reverse to what actually said it did, but it's not true.
It's because it needs to.
Actually white is what the object is, not the black counter-intuitive because we tend to think black
is the object that we wrote like with a pair of black pen.
But it's the opposite.
So let's just run this code and you can see we've implemented our own erosion and deletions here together
with opening and closing.
And I'll explain opening closing before at first before I explain the rest of the code.
So you can see this is the original here.
Handwritten open CVI eroding it turns it out, significantly diluting it makes it nice, and Taco Bell
basically molds both the font and opening you.
Now opening the second definition of opening again, opening is erosion, followed by deletion.
So before you go to the image again, hopefully you forgot what it looks like.
If you want to do erosion before deletion, erosion turns it out and dilation puts it back up.
So you can publish, you should get something that looks similar to the original, but it doesn't quite.
And the reason for that full is because when you wrote this here, you remember we were erasing pieces
of the image.
If you raise piece of this image, you're never going to get it back in.
No amount of deletion is going to add pixels to something that doesn't exist, which is why you end
up with this here, which looks kind of patchy.
No closing.
Closing is the opposite.
Closing, what do you do?
You delete this, so you kind of flattened image first and then you turn it out.
So closing, actually, it would look pretty decent, meaning mainly because you painted it first and
then you turned it out.
So that's how you get to closing here.
So let's take a look at the code and see how this is done.
So it's quite simple.
Firstly, you define a kernel, and the kernel basically specifies how much of of the erosion of that
relation we want to do.
Iterations basically tells you how much time you want to do it, so you can remember you can do it in
a region over and over and over again.
You just basically eroding it and then eroding it again or diluting dilating again, vice versa.
So all we need to do is you see v2.0 would use the input image, have reloaded, set the kernel using
a five by five kernel?
That's basically how it how it works.
So the kernel is a matrix that we use for the 2D convolution that is done in these functions here.
So the logic, you know, you'll actually notice it's going to be more dilation or more iteration being
applied.
Similarly, we can do dilate using almost the exact same function.
Actually, it pretty much is the same arguments, except.
Dilate, we replace the road with dilate and then to do opening and closing.
It's a bit different, but quite simple as well.
You use CV 2.0 Morphology X and then just put into the image you set when you were to open a close.
So it's more when the score open and more open to score close, depending on what you're doing.
And that's it.
Quite simple to run.
So let's take a look at edge detection.
So before I took a look at Kanae edge detection, I want to explain to you what edge detection is.
So imagine this is a line, you know, image.
So imagine this is the blue line we're looking at here.
This is the intensity across the blue line you can see in the graph here.
Density 255 is a brightness, so you can see it's bright and it's dark and it's bright again.
So I don't know if you remember your mats from high school university, but if you do the first or the
derivatives, which is the rates of change these points here you can see this is this.
This point here is it's consistent as well.
Firstly, here, but then there's a high rate of change here, which is why you reach this minimum point
here.
And then similarly, at this point, it goes up again quite quickly.
And that's where you get the maximum points.
So that's the rates of change being given here by first or the derivatives.
So that's one way I can show you illustrating what an edge is.
So you know what an edge.
And you know, algorithmic algorithmically how we can find the edge using the first or the derivatives.
So what are we going to do?
We're going to take a look at Cannae edge detection, which is an algorithm.
Very good algorithm, actually.
It's a bit complicated.
It's actually entire people on it that can give us the edges here.
So let's take a look at a county edge function.
So the cutting edge function should have been augments, the better.
The the first argument is the input image, the second and two documents that women and Maxwell, respectively.
The fourth argument, which is optional, is the aperture size, which is the size of the symbol colonel
used for the image gradients finding image gradients default history.
But we don't use them, but you use the default value here.
So what we can see is kind of edge.
We can suggest for shields.
Yeah, just a minute max values here, and let's run just changed thresholds to take a look so we can
widen the threshold.
We can narrow the threshold and use them.
Ed Schultz Yeah.
And let's see what the results are.
So look, this is the canny edge of the first image here with those parameters and put sort of parameters
we used and you can see it looks, it looks quite good actually, in my opinion.
This one would have wide thresholds look similar.
It's not much different.
This one with narrow thresholds definitely has less lines, and it's it's a bit harder to make out that
there's a person here.
You can probably see the car here, but it's a bit more visible, at least sort of in the sort of images
and forward sort of thresholds we use basically again gave it a lot more lines as well.
So there's something called up to a canny, not a canny was just a quick method that someone developed
online.
I think this year on StackOverflow should give you a lot of credit, but I can't remember and I can't
find a way to sources.
I remember pasting it into some of my code and using it.
So what it does, it basically calculate it does do some blurring first, then gets a median image value
and then uses its max value times.
0.66 then points the max value four and five 1.3 three.
So what we do after that is basically use these to get the thresholds or more thresholds range from
zero to 255.
So we're going it's going to get the values from that or to a function here.
And let's see how it works because setting these strategies for kind of isn't always easy.
So this looks quite nice, in my opinion, but you probably have a different opinion.
Feel free to explore, even tweak this auto kidney function function.
Tweak these thresholds.
It's always good to experiment with of these functions on your own and with different images together.
Get a few of them and what they do.
Thank you.
And let's move on to the next lesson, which is contours.
Very important lesson.
Stay tuned.