And welcome to Section E.T.A., where we take a look at perspective transforms.
So go ahead and open that notebook early.
Have it opened here?
So let's do the usual routine where we just look at all the files and images and libraries.
And now let's move on to this lesson.
So what's the point of this lesson?
Perspective transforms.
Well, the perspective transform is a way we can translate an image images cue to get it into a different
SKU or different aspect ratio, so we can use this handy OpenCV function called Get Perspective Transform
to implement this, so it's best taught with an example.
So let's take a look at this.
Let's take a look at this image.
Firstly, OK?
This image is a piece of paper that's scanned, and you can see it scanned at an angle.
But actually, it's a full picture, phone camera picture, and it's taken at an angle and you can see
it's not top down.
What if we wanted it to get it into a nice top down view?
So what we do, this is a good use, good exercise to show you how to use contours to grab this image,
just the box, the perimeter of our own image.
So we're going to go through this good well, firstly, we converted to grayscale image convert to grayscale.
Then we trust you with that.
We do some binary thresholding here using jutsu to do the adaptive threshold, and we take that to.
That's the output of this threshold function.
And we put it into fine contours and we use the red external, which means we have no hierarchy and
we just get the simple points because we don't want the whole line.
We just want the points.
So next to what we do, we draw the contours that we extracted from the image on one onto the image
itself onto the original image.
Using draw contours minus one means we're doing all we specify the color one.
That screen here is b g r, and the thickness is going to be two so we can see the line and then we
display the image here using this function, and then we just print the number of contours fund.
So that's basically it for this function.
Let's go ahead and run this.
And you can just do it yourself and get the output.
And you can see it worked very well.
It got the exact parameter outline of this piece of paper, but that's because it's on a black tier,
which which was great, which is fairly easy for this item to get.
However, you can see it did get some contours done with the bottom hip.
This Greenhills specs here, which are probably dust or maybe highlights from some light source that
was nearby, which is why we got 54 contours.
So what?
That's a problem.
We just want to get this first console out.
So what we do, we firstly just sort of the contours into area by area.
No, it's not actually necessary to do that.
However, it does speed up programming because what we're going to do, we're going to look through
all the contours here and we're going to get the parameter using Siri to linked to each contour.
And then using C to approximate what these parameters here, which is five percent of the perimeter
we're going to get to get the approximate points.
We're approximating this, this these contours.
And if it's equal to four, if the approximate point it goes before, then we break.
Now the reason we're sorting it by area first is that we kind of already know that this is a quanto
we want.
That's it's by far the largest control because these controls are going to be tiny.
They're like pixels are just groups of pixels.
So in order to do that and remember, these can also be fought to if if this is big enough, you can
have full points when you approximate it.
So we need to actually we need to actually just run it.
Firstly, first run this loop that it gets to first largest contour first.
So we run this and you can see upwards of four quarter points here.
It stores it indecipherable, called approx.
Approx.
Is this array of four corner points here.
So how do we use that?
How do we use that to get this?
This is the output we want.
It's a nice, flattened image that looks like it was scanned and it's going to make it.
It looks like a top down perspective exactly compared to the stick of the original.
Like this?
So let's see how we do that.
So what's done here?
Well, firstly, we just converted data type here because we need it as as an MP, two MP Flow 2.2.
So we convert that data type because that's what this perspective transform function takes into takes
in as inputs.
And then we declare the output point.
So the output points are basically where we want this to be.
So we're taking this point.
This is a top left.
I was specifying it.
If we wanted on zero zero, this is the top left.
This one here is the top.
Right.
You can see here zero 800, 500 800 at the bottom right there, St. this corner all the way down here.
And the last one is the bottom left, which is 500 zero.
OK, so that's how we get all of those points that we want to specify.
Do so know.
What do we do?
These are the points we want to outline to.
And with this, this if you put us on the 900, it changes the ratio and I'll do an experiment where
we can see the outputs of that.
So we just use the open view function here called Get Perspective, Transform, put into inputs and
outputs.
And this gives us M m, which is a stat which is short for metrics, which is why we call it M is our
transform matrix.
So now we're using the war perspective function c v2.1 perspective.
We can take the input image and then use to transform Matrix here.
And then this tells us the final size if we want the image, which which we already know up here anyway.
But this can change if you wanted to resize it, and it does the warping of the image here to give us
the output DSD.
OK.
So that's it for this.
Let's run this code and you can see how it looks.
No, let's change this to let's see 1000.
And let's see how this looks.
You can see it did change the orientation.
It's basically this was a four point seven by 11 inch people.
So you can adjust the pixels if you wanted to get that exact ratio back.
I forgot what the actual ratio is.
I didn't forget.
It just didn't calculate the actual ratio in these parameters here.
But that's the output.
So here's an exercise for you to do.
You remember when we got the full coordinate points here?
These just happened to luckily be in the order.
We wanted it here.
Well, actually, to be fair, we actually much just order to this these points.
However, if you wanted to order the points going top left clockwise, that's a good exercise for you.
So what these four points in going top left from top to first going clockwise.
So I want to see if you can do that.
So that would just be sorting it from zero to this point to this point to that point.
And then another not a good exercise for you to do is to get the initial contour aspect ratio and then
justify or warp to it.
But that aspect should be ratio correction.
But yeah, so this one was the one that actually discussed.
Previous Just now so that when you are actually doing the output, you just this is a very easy one
to specify the ratio to be the size of people.
And also, if you want to to add one, just remove this Green Line, which you should know how, because
the green light only appeared because we actually applied, we draw contours on the image.
So to get rid of the Green Line.
The hint, I'll tell you, was just to keep a copy of the original image that isn't passed to draw contours.
And then you can use that as the input here pretty much give you the answer there, but it's a good
exercise for you to try as well.
So that's it for this lesson.
The next lesson is going to we're going to go through histograms and K means clustering to find the
dominant colors of an image.
So it's a pretty cool lesson as well.
So I'll see you in the next lesson.
Thank you for watching.