In the last video we talked about sobel edge detection.
Okay. Sobel operator is a kernal convolution process we can use
and it returns a high
response where there's a sharp change in gradient image
and low response where there isn't.
it's generally well on grayscale images
that we first blurred just to make sure things are
nice and smooth
it also handily produces an orientation at every point
ya know with which tells us
from north in image, upwards to  downwards, left and right
what direction is our edge facing
and that can be really useful for
post-processing this image so that's
what we gonna talk about now the canny edge
detector is essentially takes a Sobel
operator and makes it just a step better ,a step more
useful perhaps. For edge analysis which is to get
rid of the edges that we're not really
interested and keep only the really good stuff
the Canny edge detector was published in 1986 and the input of a Canny operator is simply the output of Sobel.
Okay, so we've taken over image we've converted to grayscale
We've ran our Gaussian blur.
And then we've run our Sobel operator in both the X&Y directions.
We've calculated the gradient and the
orientation of those and that's when were
ready for canny's process. okay it's
fairly simple but of course it's also
effective and so it still sees a lot of use.
There are other newer edge detectors ,and there are certainly a
lot of newer filters , that are using Sobel.
Sobel still sees a lot of  use.
Canny works by taking the image from the Sobel output. Thinning all the edges so they are 1 pixel wide.
Because thick edges doesn't really help us.
What we really care about is:
Where are the edges?
 Not: 
How thick are they?
imagine we're trying to find the center line of a root because we want to find where the root is.
if we do a Sobel edge detection its
it's gonna find gradients at both left
and the right side of the root, it's going to be
bit messy.
But also its not resolution
independent if we've got a really high
resolution image the gradient is going to be spread
over many pixels in a low resolution
image you can have a Sharpish gradient
because that's the best that we can do
with those pixels.
What a Canny edge detector does is get rid of all that and
just say this is where your edge is because
I've got rid of all that stuff on the outside of it.
Canny works by first finding the edges and then
use a process called hysteresis thresholding
which sees use in other
areas which is essentially a
two-level threshold which we'll talk
about ok so the first thing to do for
every pixel is try find out if its a
local maximum ok that means that is
bigger than its neighbors ok sounds easy
but we also bear in mind the orientation
that was produced by Sobel for every part
of the image if this our image and
we're examining let's say this pixel
here
okay we've run our Sobel X and Y operators on it, and we've got a value of gX and gY
and we've got a value of magnitude of
this edge.
Now the magnitude of this edge
might be smaller or larger and what we want to do is try to find out if X is
bigger than its neighbors.
we really only care
about whether X is bigger than its neighbors
across the edge, right?
If along the edge, yes it might be
getting larger or smaller but that's not really
what we're concerned with.
So basically if the edges traveling down this direction
then we really care if
 X is bigger than this one(diagonal edge neighbor ) 
and this one(other diagonal edge neighbor ).
 Okay
if the edge is traveling in this direction(parrallel to edge)
then we really care
if X is bigger than this one and this one
(parrallel neighbors)
So what Canny does is say
what's the orientation of our edge
based on the output of Sobel operator.
We use inverse Tan to find that.
And then given that:  Is it bigger than it's neighbors and
doing that over the whole image will
produce lots of very nice thin edges
right at the peaks of the center of our response.
[Moderator off camera] So if you've got a gradient on your roots or on your whatever the edge of the
flower, the petal or whatever it's basically just gonna find the strongest [..unintelligble]
So, if we imagine 
looking at the gradient from the side then you might find a Sobel
response looks a bit like that, okay.
Because it's a kind of shallow edge, you know.
It's brightest where it is mostly
but because a photograph or something maybe
it's not completely steep, okay.
And what Canny does is scrap all this and just get an edge right in the center, okay.
And that's really what we want, okay.
So that's the first process.
The second stage is to remove the edges that even though we weren't over maximum,
There still not very useful to use because maybe they're really weak response,
So noise basically.
We want to create an image of the dominate edges.
And preserve only the dominate edges.
For that we use a process 
called hysteresis thresholding.
So let's imagine that we've run our maximum suppression
over the whole image ok so we have lots
of nice individual lines of pixels, okay.
That's great, but how do we threshold them to decide what edge is important and which edge isn't?
So if we pick just a threshold let's
say all the edges go from naught to 255
So naught is no edge, 255 is about the stronges t edge you can imagine, okay.
What value do we pick that's a good edge ,alright?
If we pick a value of twenty
most of the edges are going to be in, right?
Which means your gonna get a lot of noise, a lot of nonsense, and if you're looking
for the football in your picture, there's gonna be a lot of
other stuff you have to weed out before you can work out where the ball is
But then if we pick a high threshold like 200, we're gonna get the
edge of the ball maybe, 
but we might start to lose some of it because not all of the edges are
gonna be as strong, just that's how it is! alright.
So they lets say you hysterisis thresholding in one dimension first ,
and then you'll see how it applies
to an edge so if this is are one
dimensional image ok and we have an edge response over here that is pretty good and we
have an edge response here which is
really good and they no edge response
over here now if we were thresholding
just by a single level then we can put this level here
there were gonna get this top area , but we're not gonna
get this area,  or this area. And that might be OK but we
might want this one okay because this is kind of part of the same object perhaps that we want
to try and preserve. 
So hysteresis th resholding will have a threshold here and a threshold here
anything above the
top threshold is automatically okay[valid] so we take
all this and this is already ok anything
below the bottom threshold is automatically
discounted it's not strong enough response
that's probably not an edge we're interested in
ok so we take off here and this is all got rid of. And then  anything  between the two thresholds
is only preserved if it's connected to
something above the top threshold, ok .
So we're trying to sort of continue along edges where we've already had a high
responses at some point, ok.
So maybe the side the football is really good and the
other side is not so good, but because
it's connected you think 'yeah that's probably okay.'
Part of the same edge really.
So this stuff gets included simply by
being connected to this high threshold.
In two dimensions it works the same way.
We look around the image and we search for edges.
And any edge that's above our top threshold we automatically include.
And any edge that's connected to it by pixel traversal we automatically include
Anything else we scrap.
And that really preserves only the sort of the core edges of the image the  stuff
that really shows you what shape
everything is and there you could maybe do
some other post-processing to find
objects or something like that.
So I should probably show you some images now.
I've got my [unintelligble] .
I haven't coded up Canny because there's
a lot of good implementations of Canny out there. Takes a little bit of time to traverse an image. okay.
If you looking for an implementation, the OpenCV one is very good
okay and you can use OpenCV in Java and C++ and Python as far as i know.
ok.
So what Canny does is it takes our Sobel operator and using
the orientation of each edge it thins it,
and then it does hysteresis thresholding to
find the sort of duller edges and you get a picture that
looks much like what it did before
but now we really just got the outline of the flower
almost the entire edge of
the flower and it's  leaf have been preserved
But we've lost a lot of stuff between the petals.
So we can obviously adjust both the lower and upper thresholds of the hysteresis thresholding,
and the sort of noiseness of the Sobel operater by blurring,
 and both of those will have an effect.
And we can really control what edges come out of Canny.
We can have just a few really dominant edges or [unintelligle] edges if that's what we want.
Here is an edge,
 fairly obviously we can see that but a computer can't.
 So if we put our Sobel operator here
then what we're essentially doing is
doing a hundred times one plus a hundred times two plus a hundred times.
