So now let's move on to movements, which we will explain in the following slide and then how we can
use that and sort contours and approximate, as well as matching contours.
So this is a fairly big chapter, but I'll go through it in quite know, quite a bit of detail so that
you get a good understanding of this topic because it's quite important.
So as usual, we import our libraries and our major function and download images.
And let's do this.
Let's run this example.
Let's find contours as per usual, like we did in the previous chapter.
So what we're going to do is lower.
This image here, called bunch of shapes, showed your original image grayscale.
This image, then use can edges on it and then find the contours using that, which means we don't want
any hierarchy as well as giving you full control lines.
We're printing number of contours that we found, and then we're drawing the contours onto the image.
So let's do that.
And we get this here.
Actually, let's just do something here, let's change the size this default size to something larger,
let's try 16.
So I saw you run this again, probably should put it in a separate block next time.
Let's just do that.
And so this now prints in the bigger image here.
So it's a bit bigger.
So let's take a look at this here.
So you can see this in the images that we displayed in the original image.
These are the kind of edges for it.
Yes, it looks quite nice and defined, and look at the contours of the contours look great.
They're exactly around the object as we wanted, and that's because it's a nice, clean image here.
It's not an actual photograph, which is a lot has a lot more noise going on.
So let's just zoom out again.
So now let's take a look at something.
What if we wanted to sort by area of these contours?
How do we get the area of each of these objects?
So to do that, we actually going to use this function inside of OpenSea because contour area, you
can take a contour.
So what we're doing here that we created is a function that just takes all the contours of the output
from the fine contours, which is, oh yeah, well, actually, it's in the previous it's over here.
So we actually don't run the fine contours and discord block it just so, you know, just dismissed
that, actually.
So now I'm moving on.
So what we do, we just take a look or take all the contours we had before.
So on a group of contours here and then we'll look into it.
We're going through each contour in this line right here.
Well, this is a loop right here, and we're getting the area of each contour, installing that into
every where he is here.
And then we just keep spending to each area to to the jury and retune this afterward.
So it's a simple function that gives us all the areas for all our contours that we will find contours
found in that image.
So now that we have an array of areas, we can use python functions as sorted that takes an eye to eye
trouble right here and we can sort we saw thing on this here calls contour area, and that's one quick
way we can actually get this sorted here.
We don't even actually have to use this function anymore.
We can just quickly get it here, so we can actually this function to just basically to give us the
areas.
But this function actually sorts it here.
So we this is a very cool one line for one liner here that does sorting out contours of that area.
Now we can you can print our contours clear area here by using the function we created here to this
prince theory design prince theory of all the control areas.
So we have it for all use cases and now we're moving on to movements.
So moments.
Effectively, the center point of the contour.
So closed moments because it's a moment in physics, movements are basically a way to send a point of
a mass X.
Effectively, that's what this is doing here.
Finally, the center point of each of the objects here.
So what we're going to do, we're going to get the X and Y coordinates because moments are puts this
this variable called M, but we call it them, but standardize space until we call it.
And from that, we can actually use that to compute the X and Y of points with this formula here.
So you can take the first index here and one zero divided by zero zero and zero one, and then we get
the X and Y coordinates of the center point.
That's pretty cool.
So what we're doing here with it protects you.
Remember this from the previous chapter?
We're just putting the text one digit here.
Well, this putting getting it from this loop here.
So every time it isolates cycle increments, this I buy one.
So we're adding one two three four.
And if you notice that's largest area to smallest area, the square is the smallest area and a big square
here.
It's the largest area.
So let's just run this.
Make sure everything runs correctly.
Oh, let's see what happened.
Maybe we didn't run.
Oh, I see.
So what is needed is a mistake in my could we just call it in the original image, but we didn't actually
have a defined.
So no, we have a defined death, so everything is good to go.
So we're just putting text onto the image here.
So now let's take a look at something else.
We're going to use moments to calculate the center and then get the X coordinate to sort these controls
from left to right.
So if you wanted to do something like OCR and you wanted to get the letters and sequence in the order
of left to right, I'll show you how to do that now.
So let's create some little hope functions.
This function here, called X, could what this is doing is returns the X coordinate for the control
for a.
So you remember how we got the X coordinate here?
All it does was right.
All it does now is to X gets a moment of that.
And then and then basically returns excluding it right here.
So it's quite simple.
And what we do, we just show that for a minimum size, we once the contours of a certain size, we
would send the X coordinate.
This is basically just to reject smaller contours.
Also what we can do here.
So the Function Label Control Center just puts a red circle on the control center.
So we just take an image and take the contour and it just places a red circle on it.
So get get it should probably get used to making some little functions like this using open with calls.
It becomes quite handy to do some simple or even something relatively complex pre-processing type image
work.
So let's run this function here.
So now when we run this block of could, you can see we get the output that we said we would.
We now have the center points label that's a red dot in the center of each shape and zoom in a bit.
And we also now have it labeled left to right numerically one two three four.
So let's take a look at the function and see how we programmatically did that.
So taking a look, look at this here.
Remember, we have the contours we calculated before in a previous cell block that was done done above.
So we have the contours here.
We just loop through the contours and run our label contour center, which is this function here.
So this function here and what that is, I just basically loop through the contours and draws a red
dot in the center using the to circle function.
So that line is simple enough.
So we didn't show that image here because remember, we kept the original image, so we know it is operating.
We're putting the circles onto the image.
You know what we're doing here, we're sorting the contours again.
But what we're doing was sorting using our own function called X court.
What is this recently this this phone was this python function allows us to call a wooden sorting function
footy area so we could pass, and it takes an array list here and applies.
This function would function would source this returns a sorted vision of this tree.
And you can sort by any means you want.
We're sorting this contour by area, I should say.
Yeah.
So, so think about area and returning and leaves hitting the X coordinates of those contours.
So now you can see why after using this function where we just sort of on the X coordinate that we output
from above here, it just uses the X coordinates as a sorting metric and we reverse the order to get
it left to right.
Otherwise it would have been right to left, and that's how we sort our contours or contours.
Remember those full list of contours that we got, which is actually just for in this case.
Now that list is sorted left, right and now we can use the C v2.0 central function draw contours.
Sorry to draw the contours onto the original image, and we just set the moment again to put the labels
for the numerical value.
And we just because we're iterating enumerating this, which means we're implementing every iteration.
You can actually put this in the context in the string part of it.
The string argument so you can put the ones here, the new number, the C, I should say.
So that's a pretty cool function.
It's quite useful for a lot of applications.
So now we're moving on to something called approximate poly ADP.
So this is a function that can take a contour and approximate it around so you can take a look at the
code.
So we load an image, get to the copy of the original, which binaries, which threshold.
We do regular fine contours on the threshold of image, and then we iterate to each contour here just
to get the bounding rectangle.
I'm not sure why we're doing that.
Oh yeah, that's because we just want to get the X draw the contours to rectangles onto the image,
and then we just output the two images here first.
One is just a drawing of all contours, and the second one is about rectangles.
So you can take a look at this here.
These are drawing of all contours, it contours of God.
And these are two rectangles.
So the rectangles are basically the a box that is around the control.
So each contour, we draw a box around it, which is why you have boxes within boxes because of destroying
on top of the contours that we've created from the floor.
That's why we did this.
But what we're going to do, we're going to apply something called approximate poly ADP approx.
Probably DP for short.
And what does does it takes a contours an image here.
It takes an accuracy and accuracy as a percent of the contour parameter.
So you just take two percent here off the quantal parameter.
And you said this is true.
I'm not sure what variable this is, where we can always check it in the documentation and we just draw
the approximate outputs, which is due.
This all puts its own vision of an approximate contour here.
And we just put it inside a full list here, and let's see what this looks like.
So what what do you see here?
This is actually basically the contours we've got here, but there actually are approximately no interval
the polygons or stronger shapes.
So you can see instead of having jagged edges here or no more defined lines.
So it's a way of cleaning up your contours and approximately approximating it, which has proven quite
useful to me in a lot of applications.
Now let's take a look at convex hull.
Convex Hull will look similar to contour approximation, but it's not, and even in some cases, it
would provide the same results.
So a good way to illustrate this before in the code is to take a look at the original image here.
This is the image with the contours here.
And this is a convex hole.
Now what a convex folded is.
It actually drew a approximate line across this here.
It's a bounding line across a pond about the contours.
So that is maybe confusing because approximate the approximate one wouldn't have given you this new,
more jagged line here.
This one, no actually approximates the outer point here.
So you get sort of a convex hull, which is why it's named of it.
So it's a nice way to get a bounding area off a contour as well and to implement this.
What you do is you run the fine contours function normal, as you normally did before.
We can sort to come to us by area so we can move the largest contour of the image.
That's because in these images here, the largest contour was a big square across the image, so we
wanted to remove that from the image here.
So that's what we do with this here.
Just keep so little area and keep everything except for this room, basically.
And so the highest contour, I should say.
And then what we do, we just loop through the contours.
Convex wholeness takes contours of inputs and outputs.
The Hulk quite simple.
And then we plotted your contour.
Same thing with placing into internally and drew it here.
So that's it.
So now let's take a look at the matching contours, as much in control is actually quite important and
quite useful to do.
So what what do we mean by matching contours?
Let's take a look at this up until you can see this is the template we wanted to match here, and we
wanted to match it to one of these shapes here.
So how do we how do we do that?
Let's take a look.
So to do that, we use something called match shapes.
Match Shapes basically takes a contour template as a as the updating the control we want to match to
takes the input, contour, the control we were trying to see if it if it's matching with this or we're
just testing it with this animated contour type of content matching method of their true methods implemented
here and a method parameter, which is probably a parameter that's at configures.
Each method.
So let's leave it as default for now.
So what we do, we firstly need to get our template threshold.
So we know the attempted image, which is this four star image here.
4.8 star.
And then what we do, we just get to regular.
We do the regular thresholds, grayscale it and then we get find these fine contours of that here as
well.
So what do we do?
We then saw the contours in two by area again, and then we extract a second largest conto, which is
our template contours in that a way to do it as well.
I showed you previously in the previous slide.
And that's the reason is because you would have a bigger square block across it.
You could trust anyone to see, but that's a way just to get rid of it.
And what do we do?
We extract the contours.
Now we get the contours from the second target target image.
Those are the controls we want to see if they're matching.
So remember, we have this, we have target.
We have target group.
That's how we have threshold at twice here.
So we're trying to see which and which control, you know, output, you know, image here.
This image, which condo matches most closely with that condo.
So to do that, we actually just to get the contours of the second image as trash, too.
And then we just run much higher ups on it.
So we attempted contour, which means consistent in this loop.
We don't change template control.
It's the same template, but we're comparing it to each contour in our contours, outputs it from the
using fine contours on the second image.
And if the threshold is less than 0.1 five, which is an arbitrary threshold we've set here, you can
probably experiment with different thresholds for different images.
We say it's a match.
If not, it's not a match.
And then what we do once we get a match, we just make this closest going equal to see which is this
the kind of work that's currently being iterated on in this loop?
And then we can just use that.
Do you stroke onto us to draw the control?
And then this is how we get all the nice, cool image where we matched this contour to this contour.
So hope you enjoyed this lesson.
That's it for now.
And then we'll move on to line circle and bluff protection in the next section.
Thank you.