1
00:00:00,420 --> 00:00:05,850
And welcome to Section E.T.A., where we take a look at perspective transforms.

2
00:00:05,880 --> 00:00:08,910
So go ahead and open that notebook early.

3
00:00:08,910 --> 00:00:09,810
Have it opened here?

4
00:00:10,440 --> 00:00:15,630
So let's do the usual routine where we just look at all the files and images and libraries.

5
00:00:16,320 --> 00:00:18,310
And now let's move on to this lesson.

6
00:00:18,330 --> 00:00:20,730
So what's the point of this lesson?

7
00:00:20,970 --> 00:00:22,500
Perspective transforms.

8
00:00:22,950 --> 00:00:30,900
Well, the perspective transform is a way we can translate an image images cue to get it into a different

9
00:00:30,900 --> 00:00:37,710
SKU or different aspect ratio, so we can use this handy OpenCV function called Get Perspective Transform

10
00:00:38,100 --> 00:00:42,130
to implement this, so it's best taught with an example.

11
00:00:42,180 --> 00:00:43,410
So let's take a look at this.

12
00:00:43,800 --> 00:00:44,730
Let's take a look at this image.

13
00:00:44,730 --> 00:00:45,540
Firstly, OK?

14
00:00:46,230 --> 00:00:50,880
This image is a piece of paper that's scanned, and you can see it scanned at an angle.

15
00:00:51,510 --> 00:00:55,830
But actually, it's a full picture, phone camera picture, and it's taken at an angle and you can see

16
00:00:56,010 --> 00:00:57,090
it's not top down.

17
00:00:57,210 --> 00:00:59,910
What if we wanted it to get it into a nice top down view?

18
00:01:00,600 --> 00:01:07,800
So what we do, this is a good use, good exercise to show you how to use contours to grab this image,

19
00:01:07,800 --> 00:01:10,290
just the box, the perimeter of our own image.

20
00:01:10,950 --> 00:01:18,170
So we're going to go through this good well, firstly, we converted to grayscale image convert to grayscale.

21
00:01:18,840 --> 00:01:19,800
Then we trust you with that.

22
00:01:19,800 --> 00:01:27,320
We do some binary thresholding here using jutsu to do the adaptive threshold, and we take that to.

23
00:01:27,360 --> 00:01:29,250
That's the output of this threshold function.

24
00:01:29,730 --> 00:01:36,810
And we put it into fine contours and we use the red external, which means we have no hierarchy and

25
00:01:36,810 --> 00:01:39,550
we just get the simple points because we don't want the whole line.

26
00:01:39,570 --> 00:01:40,650
We just want the points.

27
00:01:41,340 --> 00:01:46,890
So next to what we do, we draw the contours that we extracted from the image on one onto the image

28
00:01:46,890 --> 00:01:48,360
itself onto the original image.

29
00:01:48,750 --> 00:01:53,640
Using draw contours minus one means we're doing all we specify the color one.

30
00:01:53,640 --> 00:02:00,090
That screen here is b g r, and the thickness is going to be two so we can see the line and then we

31
00:02:00,090 --> 00:02:04,950
display the image here using this function, and then we just print the number of contours fund.

32
00:02:05,580 --> 00:02:08,730
So that's basically it for this function.

33
00:02:09,120 --> 00:02:10,830
Let's go ahead and run this.

34
00:02:11,520 --> 00:02:13,950
And you can just do it yourself and get the output.

35
00:02:14,370 --> 00:02:16,170
And you can see it worked very well.

36
00:02:16,170 --> 00:02:21,720
It got the exact parameter outline of this piece of paper, but that's because it's on a black tier,

37
00:02:21,720 --> 00:02:25,590
which which was great, which is fairly easy for this item to get.

38
00:02:26,160 --> 00:02:29,100
However, you can see it did get some contours done with the bottom hip.

39
00:02:29,520 --> 00:02:35,790
This Greenhills specs here, which are probably dust or maybe highlights from some light source that

40
00:02:35,790 --> 00:02:39,090
was nearby, which is why we got 54 contours.

41
00:02:39,810 --> 00:02:40,990
So what?

42
00:02:41,040 --> 00:02:41,760
That's a problem.

43
00:02:42,180 --> 00:02:44,700
We just want to get this first console out.

44
00:02:45,600 --> 00:02:50,070
So what we do, we firstly just sort of the contours into area by area.

45
00:02:50,520 --> 00:02:52,770
No, it's not actually necessary to do that.

46
00:02:52,800 --> 00:02:58,800
However, it does speed up programming because what we're going to do, we're going to look through

47
00:02:58,800 --> 00:03:04,080
all the contours here and we're going to get the parameter using Siri to linked to each contour.

48
00:03:04,920 --> 00:03:10,860
And then using C to approximate what these parameters here, which is five percent of the perimeter

49
00:03:11,700 --> 00:03:15,420
we're going to get to get the approximate points.

50
00:03:15,750 --> 00:03:18,300
We're approximating this, this these contours.

51
00:03:18,900 --> 00:03:23,250
And if it's equal to four, if the approximate point it goes before, then we break.

52
00:03:23,790 --> 00:03:31,110
Now the reason we're sorting it by area first is that we kind of already know that this is a quanto

53
00:03:31,110 --> 00:03:31,590
we want.

54
00:03:32,070 --> 00:03:36,090
That's it's by far the largest control because these controls are going to be tiny.

55
00:03:36,090 --> 00:03:38,310
They're like pixels are just groups of pixels.

56
00:03:39,180 --> 00:03:45,000
So in order to do that and remember, these can also be fought to if if this is big enough, you can

57
00:03:45,000 --> 00:03:46,500
have full points when you approximate it.

58
00:03:47,280 --> 00:03:51,960
So we need to actually we need to actually just run it.

59
00:03:51,960 --> 00:03:56,490
Firstly, first run this loop that it gets to first largest contour first.

60
00:03:57,450 --> 00:04:00,990
So we run this and you can see upwards of four quarter points here.

61
00:04:01,500 --> 00:04:03,630
It stores it indecipherable, called approx.

62
00:04:03,660 --> 00:04:04,080
Approx.

63
00:04:04,080 --> 00:04:07,470
Is this array of four corner points here.

64
00:04:08,010 --> 00:04:10,290
So how do we use that?

65
00:04:10,410 --> 00:04:12,810
How do we use that to get this?

66
00:04:12,960 --> 00:04:14,130
This is the output we want.

67
00:04:14,520 --> 00:04:18,690
It's a nice, flattened image that looks like it was scanned and it's going to make it.

68
00:04:18,700 --> 00:04:23,880
It looks like a top down perspective exactly compared to the stick of the original.

69
00:04:23,970 --> 00:04:24,540
Like this?

70
00:04:25,440 --> 00:04:27,480
So let's see how we do that.

71
00:04:28,170 --> 00:04:29,460
So what's done here?

72
00:04:30,360 --> 00:04:36,350
Well, firstly, we just converted data type here because we need it as as an MP, two MP Flow 2.2.

73
00:04:36,360 --> 00:04:41,460
So we convert that data type because that's what this perspective transform function takes into takes

74
00:04:41,460 --> 00:04:42,360
in as inputs.

75
00:04:42,930 --> 00:04:44,460
And then we declare the output point.

76
00:04:44,470 --> 00:04:48,260
So the output points are basically where we want this to be.

77
00:04:48,270 --> 00:04:50,160
So we're taking this point.

78
00:04:50,310 --> 00:04:51,240
This is a top left.

79
00:04:51,700 --> 00:04:52,860
I was specifying it.

80
00:04:52,860 --> 00:04:56,910
If we wanted on zero zero, this is the top left.

81
00:04:57,330 --> 00:04:59,610
This one here is the top.

82
00:04:59,970 --> 00:05:00,300
Right.

83
00:05:00,370 --> 00:05:07,560
You can see here zero 800, 500 800 at the bottom right there, St. this corner all the way down here.

84
00:05:08,220 --> 00:05:12,420
And the last one is the bottom left, which is 500 zero.

85
00:05:13,080 --> 00:05:18,120
OK, so that's how we get all of those points that we want to specify.

86
00:05:18,410 --> 00:05:20,070
Do so know.

87
00:05:20,550 --> 00:05:21,150
What do we do?

88
00:05:21,780 --> 00:05:23,550
These are the points we want to outline to.

89
00:05:23,610 --> 00:05:28,020
And with this, this if you put us on the 900, it changes the ratio and I'll do an experiment where

90
00:05:28,020 --> 00:05:29,460
we can see the outputs of that.

91
00:05:30,240 --> 00:05:36,180
So we just use the open view function here called Get Perspective, Transform, put into inputs and

92
00:05:36,180 --> 00:05:36,630
outputs.

93
00:05:37,020 --> 00:05:42,960
And this gives us M m, which is a stat which is short for metrics, which is why we call it M is our

94
00:05:42,960 --> 00:05:44,430
transform matrix.

95
00:05:44,820 --> 00:05:49,440
So now we're using the war perspective function c v2.1 perspective.

96
00:05:49,890 --> 00:05:54,060
We can take the input image and then use to transform Matrix here.

97
00:05:54,510 --> 00:05:59,710
And then this tells us the final size if we want the image, which which we already know up here anyway.

98
00:05:59,730 --> 00:06:06,480
But this can change if you wanted to resize it, and it does the warping of the image here to give us

99
00:06:06,480 --> 00:06:07,710
the output DSD.

100
00:06:07,950 --> 00:06:08,310
OK.

101
00:06:09,150 --> 00:06:10,940
So that's it for this.

102
00:06:10,950 --> 00:06:13,650
Let's run this code and you can see how it looks.

103
00:06:14,220 --> 00:06:17,760
No, let's change this to let's see 1000.

104
00:06:21,250 --> 00:06:22,630
And let's see how this looks.

105
00:06:24,250 --> 00:06:26,410
You can see it did change the orientation.

106
00:06:27,070 --> 00:06:31,570
It's basically this was a four point seven by 11 inch people.

107
00:06:32,020 --> 00:06:35,710
So you can adjust the pixels if you wanted to get that exact ratio back.

108
00:06:35,890 --> 00:06:38,350
I forgot what the actual ratio is.

109
00:06:38,830 --> 00:06:39,490
I didn't forget.

110
00:06:39,490 --> 00:06:42,370
It just didn't calculate the actual ratio in these parameters here.

111
00:06:42,940 --> 00:06:43,930
But that's the output.

112
00:06:44,380 --> 00:06:46,960
So here's an exercise for you to do.

113
00:06:47,650 --> 00:06:50,890
You remember when we got the full coordinate points here?

114
00:06:52,000 --> 00:06:55,750
These just happened to luckily be in the order.

115
00:06:55,750 --> 00:06:56,680
We wanted it here.

116
00:06:57,370 --> 00:07:00,850
Well, actually, to be fair, we actually much just order to this these points.

117
00:07:01,390 --> 00:07:06,880
However, if you wanted to order the points going top left clockwise, that's a good exercise for you.

118
00:07:07,000 --> 00:07:14,320
So what these four points in going top left from top to first going clockwise.

119
00:07:14,830 --> 00:07:16,330
So I want to see if you can do that.

120
00:07:16,330 --> 00:07:22,810
So that would just be sorting it from zero to this point to this point to that point.

121
00:07:23,680 --> 00:07:29,590
And then another not a good exercise for you to do is to get the initial contour aspect ratio and then

122
00:07:29,590 --> 00:07:31,600
justify or warp to it.

123
00:07:31,600 --> 00:07:34,720
But that aspect should be ratio correction.

124
00:07:36,160 --> 00:07:38,890
But yeah, so this one was the one that actually discussed.

125
00:07:38,890 --> 00:07:44,430
Previous Just now so that when you are actually doing the output, you just this is a very easy one

126
00:07:44,430 --> 00:07:47,710
to specify the ratio to be the size of people.

127
00:07:48,100 --> 00:07:54,940
And also, if you want to to add one, just remove this Green Line, which you should know how, because

128
00:07:54,940 --> 00:08:00,070
the green light only appeared because we actually applied, we draw contours on the image.

129
00:08:00,550 --> 00:08:01,960
So to get rid of the Green Line.

130
00:08:02,080 --> 00:08:08,050
The hint, I'll tell you, was just to keep a copy of the original image that isn't passed to draw contours.

131
00:08:08,530 --> 00:08:12,940
And then you can use that as the input here pretty much give you the answer there, but it's a good

132
00:08:13,090 --> 00:08:14,560
exercise for you to try as well.

133
00:08:15,250 --> 00:08:17,020
So that's it for this lesson.

134
00:08:17,380 --> 00:08:22,300
The next lesson is going to we're going to go through histograms and K means clustering to find the

135
00:08:22,300 --> 00:08:24,040
dominant colors of an image.

136
00:08:24,160 --> 00:08:25,570
So it's a pretty cool lesson as well.

137
00:08:26,110 --> 00:08:28,080
So I'll see you in the next lesson.

138
00:08:28,120 --> 00:08:28,930
Thank you for watching.
