1
00:00:02,190 --> 00:00:09,330
So now let's move on to movements, which we will explain in the following slide and then how we can

2
00:00:09,330 --> 00:00:13,440
use that and sort contours and approximate, as well as matching contours.

3
00:00:13,950 --> 00:00:20,160
So this is a fairly big chapter, but I'll go through it in quite know, quite a bit of detail so that

4
00:00:20,160 --> 00:00:23,610
you get a good understanding of this topic because it's quite important.

5
00:00:24,360 --> 00:00:29,100
So as usual, we import our libraries and our major function and download images.

6
00:00:30,300 --> 00:00:31,310
And let's do this.

7
00:00:31,320 --> 00:00:32,280
Let's run this example.

8
00:00:32,760 --> 00:00:36,260
Let's find contours as per usual, like we did in the previous chapter.

9
00:00:36,290 --> 00:00:37,620
So what we're going to do is lower.

10
00:00:37,620 --> 00:00:42,810
This image here, called bunch of shapes, showed your original image grayscale.

11
00:00:42,810 --> 00:00:48,570
This image, then use can edges on it and then find the contours using that, which means we don't want

12
00:00:48,570 --> 00:00:51,960
any hierarchy as well as giving you full control lines.

13
00:00:52,660 --> 00:00:57,870
We're printing number of contours that we found, and then we're drawing the contours onto the image.

14
00:00:58,380 --> 00:00:59,580
So let's do that.

15
00:01:01,910 --> 00:01:02,900
And we get this here.

16
00:01:03,350 --> 00:01:08,840
Actually, let's just do something here, let's change the size this default size to something larger,

17
00:01:08,840 --> 00:01:09,860
let's try 16.

18
00:01:10,520 --> 00:01:18,160
So I saw you run this again, probably should put it in a separate block next time.

19
00:01:18,710 --> 00:01:19,940
Let's just do that.

20
00:01:21,080 --> 00:01:25,010
And so this now prints in the bigger image here.

21
00:01:25,220 --> 00:01:26,300
So it's a bit bigger.

22
00:01:27,110 --> 00:01:29,320
So let's take a look at this here.

23
00:01:29,330 --> 00:01:33,290
So you can see this in the images that we displayed in the original image.

24
00:01:33,740 --> 00:01:35,090
These are the kind of edges for it.

25
00:01:35,090 --> 00:01:39,440
Yes, it looks quite nice and defined, and look at the contours of the contours look great.

26
00:01:39,560 --> 00:01:43,610
They're exactly around the object as we wanted, and that's because it's a nice, clean image here.

27
00:01:44,060 --> 00:01:48,620
It's not an actual photograph, which is a lot has a lot more noise going on.

28
00:01:49,400 --> 00:01:51,410
So let's just zoom out again.

29
00:01:51,740 --> 00:01:53,090
So now let's take a look at something.

30
00:01:53,570 --> 00:01:56,360
What if we wanted to sort by area of these contours?

31
00:01:56,750 --> 00:01:59,300
How do we get the area of each of these objects?

32
00:01:59,870 --> 00:02:06,200
So to do that, we actually going to use this function inside of OpenSea because contour area, you

33
00:02:06,200 --> 00:02:07,370
can take a contour.

34
00:02:07,400 --> 00:02:13,160
So what we're doing here that we created is a function that just takes all the contours of the output

35
00:02:13,160 --> 00:02:21,170
from the fine contours, which is, oh yeah, well, actually, it's in the previous it's over here.

36
00:02:21,830 --> 00:02:26,020
So we actually don't run the fine contours and discord block it just so, you know, just dismissed

37
00:02:26,030 --> 00:02:26,630
that, actually.

38
00:02:27,290 --> 00:02:28,280
So now I'm moving on.

39
00:02:28,820 --> 00:02:32,840
So what we do, we just take a look or take all the contours we had before.

40
00:02:33,480 --> 00:02:37,880
So on a group of contours here and then we'll look into it.

41
00:02:38,150 --> 00:02:41,120
We're going through each contour in this line right here.

42
00:02:42,010 --> 00:02:46,870
Well, this is a loop right here, and we're getting the area of each contour, installing that into

43
00:02:46,870 --> 00:02:48,560
every where he is here.

44
00:02:49,040 --> 00:02:53,360
And then we just keep spending to each area to to the jury and retune this afterward.

45
00:02:53,870 --> 00:03:00,170
So it's a simple function that gives us all the areas for all our contours that we will find contours

46
00:03:00,170 --> 00:03:01,340
found in that image.

47
00:03:01,940 --> 00:03:08,630
So now that we have an array of areas, we can use python functions as sorted that takes an eye to eye

48
00:03:08,630 --> 00:03:16,850
trouble right here and we can sort we saw thing on this here calls contour area, and that's one quick

49
00:03:16,850 --> 00:03:19,500
way we can actually get this sorted here.

50
00:03:19,520 --> 00:03:21,620
We don't even actually have to use this function anymore.

51
00:03:22,130 --> 00:03:27,170
We can just quickly get it here, so we can actually this function to just basically to give us the

52
00:03:27,170 --> 00:03:27,620
areas.

53
00:03:28,100 --> 00:03:29,720
But this function actually sorts it here.

54
00:03:29,720 --> 00:03:36,320
So we this is a very cool one line for one liner here that does sorting out contours of that area.

55
00:03:36,920 --> 00:03:42,410
Now we can you can print our contours clear area here by using the function we created here to this

56
00:03:42,440 --> 00:03:46,370
prince theory design prince theory of all the control areas.

57
00:03:46,370 --> 00:03:51,100
So we have it for all use cases and now we're moving on to movements.

58
00:03:51,110 --> 00:03:51,920
So moments.

59
00:03:52,100 --> 00:03:55,310
Effectively, the center point of the contour.

60
00:03:55,760 --> 00:04:00,290
So closed moments because it's a moment in physics, movements are basically a way to send a point of

61
00:04:00,290 --> 00:04:01,070
a mass X.

62
00:04:01,520 --> 00:04:03,360
Effectively, that's what this is doing here.

63
00:04:03,390 --> 00:04:05,870
Finally, the center point of each of the objects here.

64
00:04:06,800 --> 00:04:12,950
So what we're going to do, we're going to get the X and Y coordinates because moments are puts this

65
00:04:13,250 --> 00:04:20,420
this variable called M, but we call it them, but standardize space until we call it.

66
00:04:21,260 --> 00:04:26,960
And from that, we can actually use that to compute the X and Y of points with this formula here.

67
00:04:27,590 --> 00:04:37,000
So you can take the first index here and one zero divided by zero zero and zero one, and then we get

68
00:04:37,000 --> 00:04:39,410
the X and Y coordinates of the center point.

69
00:04:39,560 --> 00:04:40,250
That's pretty cool.

70
00:04:41,300 --> 00:04:43,550
So what we're doing here with it protects you.

71
00:04:43,550 --> 00:04:44,960
Remember this from the previous chapter?

72
00:04:45,500 --> 00:04:48,350
We're just putting the text one digit here.

73
00:04:48,860 --> 00:04:51,010
Well, this putting getting it from this loop here.

74
00:04:51,020 --> 00:04:55,250
So every time it isolates cycle increments, this I buy one.

75
00:04:55,700 --> 00:04:57,830
So we're adding one two three four.

76
00:04:58,220 --> 00:05:04,460
And if you notice that's largest area to smallest area, the square is the smallest area and a big square

77
00:05:04,460 --> 00:05:04,760
here.

78
00:05:04,820 --> 00:05:05,750
It's the largest area.

79
00:05:06,380 --> 00:05:07,610
So let's just run this.

80
00:05:07,880 --> 00:05:09,260
Make sure everything runs correctly.

81
00:05:10,670 --> 00:05:12,230
Oh, let's see what happened.

82
00:05:12,650 --> 00:05:13,520
Maybe we didn't run.

83
00:05:18,240 --> 00:05:18,900
Oh, I see.

84
00:05:19,080 --> 00:05:25,320
So what is needed is a mistake in my could we just call it in the original image, but we didn't actually

85
00:05:25,320 --> 00:05:26,100
have a defined.

86
00:05:27,120 --> 00:05:31,170
So no, we have a defined death, so everything is good to go.

87
00:05:31,470 --> 00:05:33,480
So we're just putting text onto the image here.

88
00:05:34,380 --> 00:05:36,750
So now let's take a look at something else.

89
00:05:37,110 --> 00:05:41,910
We're going to use moments to calculate the center and then get the X coordinate to sort these controls

90
00:05:41,910 --> 00:05:42,600
from left to right.

91
00:05:42,640 --> 00:05:47,760
So if you wanted to do something like OCR and you wanted to get the letters and sequence in the order

92
00:05:47,760 --> 00:05:50,670
of left to right, I'll show you how to do that now.

93
00:05:50,970 --> 00:05:52,800
So let's create some little hope functions.

94
00:05:53,310 --> 00:05:58,980
This function here, called X, could what this is doing is returns the X coordinate for the control

95
00:05:58,980 --> 00:05:59,220
for a.

96
00:06:00,090 --> 00:06:02,610
So you remember how we got the X coordinate here?

97
00:06:03,090 --> 00:06:04,400
All it does was right.

98
00:06:04,680 --> 00:06:09,990
All it does now is to X gets a moment of that.

99
00:06:11,490 --> 00:06:14,730
And then and then basically returns excluding it right here.

100
00:06:15,390 --> 00:06:16,310
So it's quite simple.

101
00:06:16,660 --> 00:06:22,350
And what we do, we just show that for a minimum size, we once the contours of a certain size, we

102
00:06:22,350 --> 00:06:23,430
would send the X coordinate.

103
00:06:24,090 --> 00:06:26,520
This is basically just to reject smaller contours.

104
00:06:27,270 --> 00:06:28,630
Also what we can do here.

105
00:06:28,650 --> 00:06:33,090
So the Function Label Control Center just puts a red circle on the control center.

106
00:06:33,600 --> 00:06:38,160
So we just take an image and take the contour and it just places a red circle on it.

107
00:06:38,370 --> 00:06:45,300
So get get it should probably get used to making some little functions like this using open with calls.

108
00:06:45,750 --> 00:06:52,080
It becomes quite handy to do some simple or even something relatively complex pre-processing type image

109
00:06:52,080 --> 00:06:52,350
work.

110
00:06:53,190 --> 00:06:55,320
So let's run this function here.

111
00:06:56,040 --> 00:07:00,540
So now when we run this block of could, you can see we get the output that we said we would.

112
00:07:01,020 --> 00:07:06,450
We now have the center points label that's a red dot in the center of each shape and zoom in a bit.

113
00:07:07,380 --> 00:07:11,880
And we also now have it labeled left to right numerically one two three four.

114
00:07:12,330 --> 00:07:16,500
So let's take a look at the function and see how we programmatically did that.

115
00:07:17,100 --> 00:07:19,740
So taking a look, look at this here.

116
00:07:20,280 --> 00:07:25,440
Remember, we have the contours we calculated before in a previous cell block that was done done above.

117
00:07:26,040 --> 00:07:27,530
So we have the contours here.

118
00:07:27,540 --> 00:07:33,420
We just loop through the contours and run our label contour center, which is this function here.

119
00:07:34,320 --> 00:07:40,620
So this function here and what that is, I just basically loop through the contours and draws a red

120
00:07:40,620 --> 00:07:43,440
dot in the center using the to circle function.

121
00:07:43,590 --> 00:07:44,910
So that line is simple enough.

122
00:07:45,450 --> 00:07:51,270
So we didn't show that image here because remember, we kept the original image, so we know it is operating.

123
00:07:51,270 --> 00:07:53,280
We're putting the circles onto the image.

124
00:07:53,280 --> 00:07:58,080
You know what we're doing here, we're sorting the contours again.

125
00:07:58,470 --> 00:08:02,220
But what we're doing was sorting using our own function called X court.

126
00:08:02,730 --> 00:08:09,360
What is this recently this this phone was this python function allows us to call a wooden sorting function

127
00:08:09,360 --> 00:08:15,570
footy area so we could pass, and it takes an array list here and applies.

128
00:08:15,570 --> 00:08:19,770
This function would function would source this returns a sorted vision of this tree.

129
00:08:20,220 --> 00:08:22,130
And you can sort by any means you want.

130
00:08:22,140 --> 00:08:27,480
We're sorting this contour by area, I should say.

131
00:08:27,490 --> 00:08:27,750
Yeah.

132
00:08:28,410 --> 00:08:34,230
So, so think about area and returning and leaves hitting the X coordinates of those contours.

133
00:08:34,590 --> 00:08:40,350
So now you can see why after using this function where we just sort of on the X coordinate that we output

134
00:08:40,800 --> 00:08:46,860
from above here, it just uses the X coordinates as a sorting metric and we reverse the order to get

135
00:08:46,860 --> 00:08:47,520
it left to right.

136
00:08:47,520 --> 00:08:52,880
Otherwise it would have been right to left, and that's how we sort our contours or contours.

137
00:08:52,890 --> 00:08:57,270
Remember those full list of contours that we got, which is actually just for in this case.

138
00:08:57,990 --> 00:09:04,980
Now that list is sorted left, right and now we can use the C v2.0 central function draw contours.

139
00:09:05,160 --> 00:09:12,150
Sorry to draw the contours onto the original image, and we just set the moment again to put the labels

140
00:09:12,300 --> 00:09:14,700
for the numerical value.

141
00:09:15,270 --> 00:09:22,740
And we just because we're iterating enumerating this, which means we're implementing every iteration.

142
00:09:23,460 --> 00:09:26,880
You can actually put this in the context in the string part of it.

143
00:09:27,160 --> 00:09:31,500
The string argument so you can put the ones here, the new number, the C, I should say.

144
00:09:32,310 --> 00:09:34,440
So that's a pretty cool function.

145
00:09:35,910 --> 00:09:37,920
It's quite useful for a lot of applications.

146
00:09:39,150 --> 00:09:43,040
So now we're moving on to something called approximate poly ADP.

147
00:09:43,590 --> 00:09:49,920
So this is a function that can take a contour and approximate it around so you can take a look at the

148
00:09:49,920 --> 00:09:50,220
code.

149
00:09:50,670 --> 00:09:56,430
So we load an image, get to the copy of the original, which binaries, which threshold.

150
00:09:56,520 --> 00:10:03,300
We do regular fine contours on the threshold of image, and then we iterate to each contour here just

151
00:10:03,300 --> 00:10:05,280
to get the bounding rectangle.

152
00:10:05,580 --> 00:10:07,230
I'm not sure why we're doing that.

153
00:10:07,230 --> 00:10:13,200
Oh yeah, that's because we just want to get the X draw the contours to rectangles onto the image,

154
00:10:14,550 --> 00:10:17,400
and then we just output the two images here first.

155
00:10:17,460 --> 00:10:21,210
One is just a drawing of all contours, and the second one is about rectangles.

156
00:10:21,690 --> 00:10:23,040
So you can take a look at this here.

157
00:10:23,190 --> 00:10:25,980
These are drawing of all contours, it contours of God.

158
00:10:26,460 --> 00:10:27,820
And these are two rectangles.

159
00:10:27,820 --> 00:10:32,870
So the rectangles are basically the a box that is around the control.

160
00:10:32,940 --> 00:10:38,550
So each contour, we draw a box around it, which is why you have boxes within boxes because of destroying

161
00:10:38,550 --> 00:10:41,160
on top of the contours that we've created from the floor.

162
00:10:41,670 --> 00:10:42,780
That's why we did this.

163
00:10:43,200 --> 00:10:48,420
But what we're going to do, we're going to apply something called approximate poly ADP approx.

164
00:10:48,540 --> 00:10:49,530
Probably DP for short.

165
00:10:50,160 --> 00:10:52,950
And what does does it takes a contours an image here.

166
00:10:53,430 --> 00:10:57,190
It takes an accuracy and accuracy as a percent of the contour parameter.

167
00:10:57,210 --> 00:11:00,720
So you just take two percent here off the quantal parameter.

168
00:11:01,440 --> 00:11:03,090
And you said this is true.

169
00:11:03,090 --> 00:11:10,380
I'm not sure what variable this is, where we can always check it in the documentation and we just draw

170
00:11:10,620 --> 00:11:12,620
the approximate outputs, which is due.

171
00:11:13,050 --> 00:11:15,700
This all puts its own vision of an approximate contour here.

172
00:11:16,350 --> 00:11:20,760
And we just put it inside a full list here, and let's see what this looks like.

173
00:11:22,230 --> 00:11:24,000
So what what do you see here?

174
00:11:24,540 --> 00:11:30,060
This is actually basically the contours we've got here, but there actually are approximately no interval

175
00:11:30,420 --> 00:11:32,790
the polygons or stronger shapes.

176
00:11:33,420 --> 00:11:36,990
So you can see instead of having jagged edges here or no more defined lines.

177
00:11:37,440 --> 00:11:42,270
So it's a way of cleaning up your contours and approximately approximating it, which has proven quite

178
00:11:42,270 --> 00:11:44,670
useful to me in a lot of applications.

179
00:11:45,210 --> 00:11:47,010
Now let's take a look at convex hull.

180
00:11:47,220 --> 00:11:53,460
Convex Hull will look similar to contour approximation, but it's not, and even in some cases, it

181
00:11:53,750 --> 00:11:54,960
would provide the same results.

182
00:11:55,440 --> 00:12:00,810
So a good way to illustrate this before in the code is to take a look at the original image here.

183
00:12:01,470 --> 00:12:03,220
This is the image with the contours here.

184
00:12:03,690 --> 00:12:05,190
And this is a convex hole.

185
00:12:05,790 --> 00:12:07,770
Now what a convex folded is.

186
00:12:07,770 --> 00:12:12,510
It actually drew a approximate line across this here.

187
00:12:13,170 --> 00:12:17,160
It's a bounding line across a pond about the contours.

188
00:12:17,700 --> 00:12:23,610
So that is maybe confusing because approximate the approximate one wouldn't have given you this new,

189
00:12:23,610 --> 00:12:25,500
more jagged line here.

190
00:12:25,860 --> 00:12:28,380
This one, no actually approximates the outer point here.

191
00:12:28,710 --> 00:12:32,010
So you get sort of a convex hull, which is why it's named of it.

192
00:12:32,130 --> 00:12:37,410
So it's a nice way to get a bounding area off a contour as well and to implement this.

193
00:12:37,830 --> 00:12:42,540
What you do is you run the fine contours function normal, as you normally did before.

194
00:12:43,590 --> 00:12:47,610
We can sort to come to us by area so we can move the largest contour of the image.

195
00:12:48,180 --> 00:12:54,540
That's because in these images here, the largest contour was a big square across the image, so we

196
00:12:54,540 --> 00:12:56,240
wanted to remove that from the image here.

197
00:12:56,250 --> 00:12:58,080
So that's what we do with this here.

198
00:12:58,270 --> 00:13:02,610
Just keep so little area and keep everything except for this room, basically.

199
00:13:03,930 --> 00:13:06,690
And so the highest contour, I should say.

200
00:13:07,620 --> 00:13:09,900
And then what we do, we just loop through the contours.

201
00:13:10,260 --> 00:13:13,550
Convex wholeness takes contours of inputs and outputs.

202
00:13:13,560 --> 00:13:14,910
The Hulk quite simple.

203
00:13:15,300 --> 00:13:16,710
And then we plotted your contour.

204
00:13:16,740 --> 00:13:20,030
Same thing with placing into internally and drew it here.

205
00:13:20,070 --> 00:13:20,850
So that's it.

206
00:13:21,480 --> 00:13:26,820
So now let's take a look at the matching contours, as much in control is actually quite important and

207
00:13:26,820 --> 00:13:27,780
quite useful to do.

208
00:13:28,350 --> 00:13:30,540
So what what do we mean by matching contours?

209
00:13:31,080 --> 00:13:37,140
Let's take a look at this up until you can see this is the template we wanted to match here, and we

210
00:13:37,140 --> 00:13:39,570
wanted to match it to one of these shapes here.

211
00:13:39,960 --> 00:13:41,880
So how do we how do we do that?

212
00:13:43,260 --> 00:13:43,950
Let's take a look.

213
00:13:44,730 --> 00:13:47,850
So to do that, we use something called match shapes.

214
00:13:48,360 --> 00:13:56,520
Match Shapes basically takes a contour template as a as the updating the control we want to match to

215
00:13:57,000 --> 00:14:01,260
takes the input, contour, the control we were trying to see if it if it's matching with this or we're

216
00:14:01,260 --> 00:14:07,020
just testing it with this animated contour type of content matching method of their true methods implemented

217
00:14:07,020 --> 00:14:13,080
here and a method parameter, which is probably a parameter that's at configures.

218
00:14:13,110 --> 00:14:13,710
Each method.

219
00:14:13,830 --> 00:14:15,420
So let's leave it as default for now.

220
00:14:16,050 --> 00:14:19,890
So what we do, we firstly need to get our template threshold.

221
00:14:20,460 --> 00:14:24,570
So we know the attempted image, which is this four star image here.

222
00:14:25,410 --> 00:14:26,280
4.8 star.

223
00:14:28,200 --> 00:14:30,290
And then what we do, we just get to regular.

224
00:14:30,330 --> 00:14:36,930
We do the regular thresholds, grayscale it and then we get find these fine contours of that here as

225
00:14:36,930 --> 00:14:37,200
well.

226
00:14:39,210 --> 00:14:40,110
So what do we do?

227
00:14:40,140 --> 00:14:46,350
We then saw the contours in two by area again, and then we extract a second largest conto, which is

228
00:14:46,350 --> 00:14:48,510
our template contours in that a way to do it as well.

229
00:14:50,100 --> 00:14:51,900
I showed you previously in the previous slide.

230
00:14:52,620 --> 00:14:56,430
And that's the reason is because you would have a bigger square block across it.

231
00:14:56,440 --> 00:14:59,550
You could trust anyone to see, but that's a way just to get rid of it.

232
00:15:00,420 --> 00:15:01,560
And what do we do?

233
00:15:01,650 --> 00:15:03,000
We extract the contours.

234
00:15:03,040 --> 00:15:06,930
Now we get the contours from the second target target image.

235
00:15:07,320 --> 00:15:09,930
Those are the controls we want to see if they're matching.

236
00:15:10,380 --> 00:15:12,180
So remember, we have this, we have target.

237
00:15:12,300 --> 00:15:13,140
We have target group.

238
00:15:14,220 --> 00:15:16,890
That's how we have threshold at twice here.

239
00:15:17,330 --> 00:15:22,610
So we're trying to see which and which control, you know, output, you know, image here.

240
00:15:23,070 --> 00:15:27,710
This image, which condo matches most closely with that condo.

241
00:15:28,190 --> 00:15:33,050
So to do that, we actually just to get the contours of the second image as trash, too.

242
00:15:33,920 --> 00:15:35,750
And then we just run much higher ups on it.

243
00:15:35,750 --> 00:15:39,830
So we attempted contour, which means consistent in this loop.

244
00:15:40,010 --> 00:15:41,320
We don't change template control.

245
00:15:41,330 --> 00:15:46,940
It's the same template, but we're comparing it to each contour in our contours, outputs it from the

246
00:15:47,300 --> 00:15:49,220
using fine contours on the second image.

247
00:15:50,090 --> 00:15:55,460
And if the threshold is less than 0.1 five, which is an arbitrary threshold we've set here, you can

248
00:15:55,460 --> 00:15:57,980
probably experiment with different thresholds for different images.

249
00:15:58,460 --> 00:15:59,650
We say it's a match.

250
00:15:59,660 --> 00:16:00,920
If not, it's not a match.

251
00:16:02,000 --> 00:16:08,210
And then what we do once we get a match, we just make this closest going equal to see which is this

252
00:16:08,420 --> 00:16:12,350
the kind of work that's currently being iterated on in this loop?

253
00:16:12,890 --> 00:16:14,270
And then we can just use that.

254
00:16:14,270 --> 00:16:16,040
Do you stroke onto us to draw the control?

255
00:16:16,490 --> 00:16:23,150
And then this is how we get all the nice, cool image where we matched this contour to this contour.

256
00:16:23,960 --> 00:16:25,490
So hope you enjoyed this lesson.

257
00:16:25,970 --> 00:16:26,900
That's it for now.

258
00:16:26,900 --> 00:16:31,910
And then we'll move on to line circle and bluff protection in the next section.

259
00:16:32,090 --> 00:16:32,600
Thank you.
