Now, let's move on to our eleventh lesson, which is countless and take take a deeper look at what
contours are the different modes and the drawing of it on images.
So firstly, let's just import all libraries and download images to get us out of the way and now explain
to you what contours are, so.
Sure, we have this done.
So let's just here.
Get this over with.
This is what happens when you run this twice.
Doctors asks you if you want to replace your images and you just put it all just because it's OK if
we replace it, but not before.
And it's fine.
It's the same thing.
So contours are basically continuous lines of curves that cover the boundary of an object in an image.
So let's take a look at this input image here.
So let's run this and I want you to load this image.
You can see it's a license plate that says PC x fifty five 08.
So if you were to draw lines or curves around boundaries of an object, let's assume the boundary we
want to show is around p.
A contour would be something that draws like a little Green Line around the perimeter.
And that's exactly what it is, and it's quite useful, which I should emphasize extremely useful in
computer vision applications to use control.
It's not so much nowadays, though, because nowadays deep learning can do so many things.
But previously contours was like go to function if you want to do some sort of primitive object detection
or blood detection of something.
So let's go on to this.
So let's run some examples of code here.
And this is the counter function explained just in case you wanted to have the details of what the takes.
Basically, text input takes a retrieval mode that you want to use and the approximation method, which
will demo all of these options afterward in this chapter.
But for now, I'm just going to run the basic default actions a default, but a basic contour example
so you can get your feet wet and understand what's going on.
So firstly, this lotus image converted to agree a scale and then threshold.
This image, and the reason we treasure this image is because contours of contouring works best when
the images threshold and binaries like this.
Otherwise, it's it's almost futile to even try again contour and actually might not even work.
If it's a non-binary estimate, you have to try.
It's been a while since I've been messing with contours, but nevertheless, let's take a look at this.
So we run this scaled which threshold and then we passed that threshold, that image to our controls
function.
Using the to this, which is retrieves a list of all the contours doesn't care what the parent child
relationships in the contours.
And then we use the tuner proximal, which basically stores all the points for line.
However, it creates quite a big country and then it stores it here, so it gives us the top of the
contours and a hierarchy.
However, we're going to ignore hierarchy right now because how we can get quite confusing sometimes,
and it's not always.
It's not that important to do anymore.
It exists.
You can check it out on the open sea view documentation to if you wanted to see it in detail.
However, it's not that important right now for this lesson that we use to TV to draw contours, to
take the output, which is the Contos upper tier and then draw them.
And we just specify that we want to draw all which is the properties them to say control of what colors
and thickness and Hollywood contours we want to draw.
So you can draw individual contours or you can draw all of these negative one and just showing up it.
So let's run this.
And we can see this is the operative.
So it's still in a bit.
So you can see we are just said we have it in contours, which shows us that at the length of the saree,
we have 18 elements in disarray and will inspect to control elements just now so you can take a look.
So we can see, basically, as I said, we wanted the lines around the objects of an image here, so
you can see if we get a green line around the piece in the little black dots into P..
So this is quite cool.
You can see contours being immediately useful for something like object recognition.
If you wanted to extract each of the letters out.
I mean, yeah, we'll see our optical character recognition, I should say.
And you can basically isolate each character individually by using contours.
So that's quite good.
So now let's take a look at what the contour actually consists of.
You can see if we take a look at the first element traumatized 18 elements misery, so we index in the
first one.
You can see it contains this list of points, so lots and lots of points.
And what is that?
Basically, it's it's a point that coordinates for every pixel in the perimeter.
So it's just like a list of pixels that basically are the perimeter of the object here.
So kind contours give us that out.
But it's pretty cool, isn't it?
So now what happens if we don't trust or I guess the contours may not even work if you don't?
For sure.
And that's true.
It doesn't work.
So all of us remember the threshold to some Gaussian blur.
Gray scaling will actually you need to grayscale before your threshold and even kind of edges works
quite well with contact, but to find the contours afterward.
So remember this for fine contours work background has to be black, and foreground has to be basically
white to anything else.
Otherwise, you're not going to get the contours that you want if you wanted to do.
You can use midwives not to interpret the image so that you can still use fine contours and may be reinvented
afterward if you wanted to.
So it's not a dealbreaker.
So now let's take a look at using uncanny edges for contours.
So all we do is instead of instead of thresholding, we're just going to use candy edges on the Grayscale
imager, set some thresholds here and then take the edge output, which is a binaries image, by the
way, kind of edges outputs of binary estimates.
And you can just use these default parameters again, where we disregard parent-child relationships
and rest.
Look at all of the points we draw it again, like we did in the previous one right here and we get this
nice out.
But below so you can see, you know, we found 77 contours, and that's because Kanae edges creates
a lot more noise.
So you could have a lot more contours.
Like if you look zoom in, you'll see that a lot these little green patches that don't look that small.
That's because there's a multitude of contours here down here as well.
So that's how you get 77 contours.
So let's remember these steps when contouring.
You do want to grayscale and you do want to trash over kind of edge to binaries to image.
So it's important to do that.
So now let's take a look at irretrievable.
So you can actually, if you wanted to get the official up and to the docs and check this out from you,
and you can see all of the sudden what hierarchy here, which isn't.
It's a bit confusing sometimes, but you can take a look on your butt.
Let's go back to the notebook and we can see it here.
So what we're going to do in the hierarchy, basically hierarchy stores for values for each control
system is an index of the next contour second term to the index of the previous contour to it in this
apparent next parents control.
So again, like I said, this is a bit confusing.
Are you doing necessarily need to know all of this?
If you wanted to just take a look into this hierarchy document here?
So let's take a look at the red list.
So this fretless remember, I said in the beginning that this which gives all the contours and doesn't
create any parent child relationship.
So what this means is that parents and kids are equal.
Basically, there's no distinction between them.
Everything is just contours.
So they're all in the same hierarchy, flat level, and it's just run this and we get this up at here.
So it's pretty cool.
Now let's take a look at this one here because actually probably the change of plotting function, that's
why the images end up smaller.
If you wanted to increase the size of the image, you can assume size equal weight.
10 something.
Yeah, and it should keep the size bigger.
The default size was a bit smaller initially.
So anyway, back to this now red external isn't one of the extreme.
Out to flags all tail concerto left behind.
Meaning that you remember there was a concerto very strong and so to speak outside of the hollow point
in the piece.
You can see it previously here.
This woman can see this green circle roughly look and think, Yeah, if you go back down to this, you
can see it's no longer there.
So that's interesting.
So contours are encapsulated inside of the contours are left out when you send indirect external, which
mean which makes sense.
We want to get only the external contours only.
So that's pretty cool.
So we also print the hierarchy.
Here you can take a look at the hockey and see for yourself what it looks like.
And we also get the number of contours printed for you as well.
So now moving on to read our C comp, this one retrieves all the contours and reaches them into two
level hockey.
So you have to have accused forces, the external hierarchy and the internal or inside hockey of just
hockey too.
And the first one is the external external one is hockey one.
So let's just run this Goodacre here, which have run already, and you can see that the output is this
here.
So remember this this matrix is outputting here we can.
We explain what the values were to screw up.
Sorry for screwing the first team is the index of the next.
Contour is a continuous index of the previous contour to a term as the index of two parent control on
the 40mm as index of the child Kondo.
So that's how that's what this is here.
That's what it's telling us to Tim.
That's a good attitude for too many.
That's the index of the parent and index of the child control.
So you can see when we did it at all, there was no relationship at all.
With minus one, Redick still only gave us external contours.
So which is why we only looked at the contours and there's nothing else.
Why Aki after that and red?
Come see, now we have some differences here, so you can see this is basically telling us that this
belongs to probably a maximum, a big external contour.
This returns to belongs to another parent to a child, so you can see the relationships of the contours
there.
Red Tree, which achieves all contours and creates a full of family hockey.
This there's enough hockey one or two.
It just has this feel connected to hockey list right here, and you could take a look at the output
again.
Now, let's look at something that's a little bit more important, which is contouring modes.
So we have to contour inwards.
We have chin approx.
And chin approx.
Basically, it gives us all the points in the image here.
So this gives us like the whole list of points.
If you run this, could we get this here now?
Alternatively, there's something called actually, let me explain what the output of this is doing.
So you can see four see in contours, which means that what this is doing is that we're going through
every contour, each contour individually in our contours are list or three and printing the length
of them.
So you can see how much points it takes for one of these little parameter lines.
One of its contours, we call it.
So this one eighty seven points, this one was seventy points.
You can see some of the one point, which means that they were at a contour around one pixel.
The Pixel itself is just a contour.
You can see some of 400 points, which is a quite big contour.
So you can see filtering on contours.
That long is a where we can extract the contours that are probably for this piece, the action.
But these are six digits and you can actually see one two three four five six two seven seven.
Values are quite big, so most likely these characters belong to a seven.
Some values seven digits.
So what is chin approx?
Simple chin approx.
Simple this is different.
I'll explain to you what it is.
Chain approx.
Simple just stores the end points of light, so it doesn't store all of the coordinates of the stores,
the endpoint, so it's a lot less space, but you can see some of the points, but a lot of space it
occupies.
That's the difference with the controls here.
So again, in most cases, it can use chain approx.
None, which gives us full length contours.
Gina Brock Simple just stores the end points of each line, which is which is cool, which is important
if you wanted to save space.
If we have an application looking for an embedded system, so that concludes this chapter and contours
a hope you find it useful.
Next, we're going to move on to moments sorting and approximating and matching contours, which is
also very important.
Thank you.