1
00:00:00,510 --> 00:00:06,270
Hi and welcome to Chapter 10, where we take a look at dilation, erosion and edge detection.

2
00:00:06,840 --> 00:00:08,640
So let's go ahead and open this notebook.

3
00:00:08,890 --> 00:00:15,750
Already have it open here and you can see there's two more things added to the to this topic that we're

4
00:00:15,750 --> 00:00:16,260
learning here.

5
00:00:16,770 --> 00:00:22,200
It's going to be opening and closing, which are actually combinations of dilation and erosion, but

6
00:00:22,200 --> 00:00:24,450
you don't know what the dilation and erosion is yet.

7
00:00:24,450 --> 00:00:25,740
So let's take a look at this.

8
00:00:25,800 --> 00:00:33,540
So firstly, let's download our images and see all the major functions and all libraries, and that's

9
00:00:33,540 --> 00:00:33,840
done.

10
00:00:34,440 --> 00:00:36,510
So let's take a look at the definitions here.

11
00:00:36,660 --> 00:00:37,740
So let's zoom in a bit.

12
00:00:38,220 --> 00:00:42,630
Dilation adds pixels to the boundaries of an object in the image.

13
00:00:43,020 --> 00:00:50,610
Erosion removes pixels at the boundaries of an object, and opening is this erosion, followed by dilation

14
00:00:50,610 --> 00:00:52,680
and closing this dilation followed by erosion.

15
00:00:53,040 --> 00:00:54,930
So you can see this illustrated right here.

16
00:00:55,320 --> 00:00:56,540
This is the original image here.

17
00:00:56,550 --> 00:00:57,540
This is a number five.

18
00:00:58,020 --> 00:01:02,460
You can see the downward erosion issue, which we look at this.

19
00:01:03,090 --> 00:01:05,270
It removes the pixels from the boundaries.

20
00:01:05,280 --> 00:01:08,130
So all of these things that have boundaries here are gone.

21
00:01:08,730 --> 00:01:09,360
You can see it.

22
00:01:09,540 --> 00:01:11,250
This first column has gone.

23
00:01:11,550 --> 00:01:14,280
This top arrow here is gone as well.

24
00:01:14,790 --> 00:01:17,910
This one is gone, so everything basically thins out.

25
00:01:18,390 --> 00:01:19,560
So that's what erosion does.

26
00:01:19,560 --> 00:01:22,510
It erodes all the pixel around the objects.

27
00:01:22,530 --> 00:01:27,500
You only left with this in a line here, which luckily still looks like five just a very thin.

28
00:01:27,500 --> 00:01:33,270
The five dilation does the opposite dilation padgett of additional pixels around the boundary here,

29
00:01:33,840 --> 00:01:36,630
basically fattening it up to make it look like this.

30
00:01:37,440 --> 00:01:39,060
So that's pretty simple.

31
00:01:39,690 --> 00:01:44,820
But it's important to realize, though, that remember, I'm using zeros and ones here, but it's really

32
00:01:44,820 --> 00:01:48,060
zero and 255 for 255 wouldn't have fit here.

33
00:01:48,360 --> 00:01:55,740
So this relative binary scale, remember this is the white is what the algorithm sees as the image,

34
00:01:55,770 --> 00:01:56,790
as the object.

35
00:01:57,000 --> 00:02:05,940
I should say yes, because a lot, lot, lot of times people have people who frequently think if it's

36
00:02:05,940 --> 00:02:10,950
a black line, like if this was inverted, inverted and it was a black five on a white background,

37
00:02:11,490 --> 00:02:16,440
if you wrote it, you would get a thinner five when in fact you would get the opposite people, lots

38
00:02:16,440 --> 00:02:20,230
of people for a long time to open to the open to these algorithms.

39
00:02:20,730 --> 00:02:23,970
What's in reverse to what actually said it did, but it's not true.

40
00:02:23,970 --> 00:02:25,380
It's because it needs to.

41
00:02:25,380 --> 00:02:31,680
Actually white is what the object is, not the black counter-intuitive because we tend to think black

42
00:02:31,680 --> 00:02:34,980
is the object that we wrote like with a pair of black pen.

43
00:02:35,460 --> 00:02:36,450
But it's the opposite.

44
00:02:37,410 --> 00:02:44,100
So let's just run this code and you can see we've implemented our own erosion and deletions here together

45
00:02:44,100 --> 00:02:45,120
with opening and closing.

46
00:02:45,120 --> 00:02:50,910
And I'll explain opening closing before at first before I explain the rest of the code.

47
00:02:51,450 --> 00:02:53,250
So you can see this is the original here.

48
00:02:53,370 --> 00:03:00,870
Handwritten open CVI eroding it turns it out, significantly diluting it makes it nice, and Taco Bell

49
00:03:00,870 --> 00:03:05,190
basically molds both the font and opening you.

50
00:03:05,190 --> 00:03:11,820
Now opening the second definition of opening again, opening is erosion, followed by deletion.

51
00:03:12,330 --> 00:03:15,150
So before you go to the image again, hopefully you forgot what it looks like.

52
00:03:15,600 --> 00:03:22,530
If you want to do erosion before deletion, erosion turns it out and dilation puts it back up.

53
00:03:22,920 --> 00:03:27,970
So you can publish, you should get something that looks similar to the original, but it doesn't quite.

54
00:03:27,990 --> 00:03:33,030
And the reason for that full is because when you wrote this here, you remember we were erasing pieces

55
00:03:33,030 --> 00:03:33,510
of the image.

56
00:03:33,840 --> 00:03:36,480
If you raise piece of this image, you're never going to get it back in.

57
00:03:36,480 --> 00:03:42,060
No amount of deletion is going to add pixels to something that doesn't exist, which is why you end

58
00:03:42,060 --> 00:03:44,310
up with this here, which looks kind of patchy.

59
00:03:45,120 --> 00:03:46,500
No closing.

60
00:03:46,560 --> 00:03:47,580
Closing is the opposite.

61
00:03:47,970 --> 00:03:49,080
Closing, what do you do?

62
00:03:49,110 --> 00:03:53,490
You delete this, so you kind of flattened image first and then you turn it out.

63
00:03:53,520 --> 00:03:59,190
So closing, actually, it would look pretty decent, meaning mainly because you painted it first and

64
00:03:59,190 --> 00:04:00,030
then you turned it out.

65
00:04:00,300 --> 00:04:01,860
So that's how you get to closing here.

66
00:04:02,340 --> 00:04:04,620
So let's take a look at the code and see how this is done.

67
00:04:04,770 --> 00:04:05,670
So it's quite simple.

68
00:04:05,700 --> 00:04:11,970
Firstly, you define a kernel, and the kernel basically specifies how much of of the erosion of that

69
00:04:11,970 --> 00:04:13,650
relation we want to do.

70
00:04:14,220 --> 00:04:18,540
Iterations basically tells you how much time you want to do it, so you can remember you can do it in

71
00:04:18,540 --> 00:04:20,950
a region over and over and over again.

72
00:04:21,780 --> 00:04:26,880
You just basically eroding it and then eroding it again or diluting dilating again, vice versa.

73
00:04:27,720 --> 00:04:34,980
So all we need to do is you see v2.0 would use the input image, have reloaded, set the kernel using

74
00:04:34,980 --> 00:04:36,180
a five by five kernel?

75
00:04:36,210 --> 00:04:38,250
That's basically how it how it works.

76
00:04:39,030 --> 00:04:44,160
So the kernel is a matrix that we use for the 2D convolution that is done in these functions here.

77
00:04:44,640 --> 00:04:50,520
So the logic, you know, you'll actually notice it's going to be more dilation or more iteration being

78
00:04:50,520 --> 00:04:50,940
applied.

79
00:04:51,480 --> 00:04:55,380
Similarly, we can do dilate using almost the exact same function.

80
00:04:55,680 --> 00:04:59,070
Actually, it pretty much is the same arguments, except.

81
00:04:59,780 --> 00:05:05,530
Dilate, we replace the road with dilate and then to do opening and closing.

82
00:05:05,890 --> 00:05:08,470
It's a bit different, but quite simple as well.

83
00:05:09,010 --> 00:05:16,340
You use CV 2.0 Morphology X and then just put into the image you set when you were to open a close.

84
00:05:16,340 --> 00:05:21,040
So it's more when the score open and more open to score close, depending on what you're doing.

85
00:05:21,640 --> 00:05:22,560
And that's it.

86
00:05:22,600 --> 00:05:23,510
Quite simple to run.

87
00:05:24,280 --> 00:05:25,990
So let's take a look at edge detection.

88
00:05:26,530 --> 00:05:30,670
So before I took a look at Kanae edge detection, I want to explain to you what edge detection is.

89
00:05:31,090 --> 00:05:33,340
So imagine this is a line, you know, image.

90
00:05:34,300 --> 00:05:36,910
So imagine this is the blue line we're looking at here.

91
00:05:37,930 --> 00:05:40,750
This is the intensity across the blue line you can see in the graph here.

92
00:05:41,020 --> 00:05:46,990
Density 255 is a brightness, so you can see it's bright and it's dark and it's bright again.

93
00:05:47,740 --> 00:05:52,900
So I don't know if you remember your mats from high school university, but if you do the first or the

94
00:05:52,900 --> 00:05:57,950
derivatives, which is the rates of change these points here you can see this is this.

95
00:05:58,120 --> 00:06:01,060
This point here is it's consistent as well.

96
00:06:01,390 --> 00:06:06,520
Firstly, here, but then there's a high rate of change here, which is why you reach this minimum point

97
00:06:06,520 --> 00:06:06,760
here.

98
00:06:07,270 --> 00:06:10,330
And then similarly, at this point, it goes up again quite quickly.

99
00:06:10,750 --> 00:06:12,160
And that's where you get the maximum points.

100
00:06:12,160 --> 00:06:16,030
So that's the rates of change being given here by first or the derivatives.

101
00:06:16,750 --> 00:06:20,980
So that's one way I can show you illustrating what an edge is.

102
00:06:21,160 --> 00:06:22,090
So you know what an edge.

103
00:06:22,480 --> 00:06:27,190
And you know, algorithmic algorithmically how we can find the edge using the first or the derivatives.

104
00:06:28,150 --> 00:06:29,530
So what are we going to do?

105
00:06:29,560 --> 00:06:32,530
We're going to take a look at Cannae edge detection, which is an algorithm.

106
00:06:32,950 --> 00:06:34,570
Very good algorithm, actually.

107
00:06:34,720 --> 00:06:35,590
It's a bit complicated.

108
00:06:35,590 --> 00:06:40,290
It's actually entire people on it that can give us the edges here.

109
00:06:40,300 --> 00:06:42,250
So let's take a look at a county edge function.

110
00:06:42,820 --> 00:06:46,870
So the cutting edge function should have been augments, the better.

111
00:06:47,260 --> 00:06:53,200
The the first argument is the input image, the second and two documents that women and Maxwell, respectively.

112
00:06:53,530 --> 00:06:58,630
The fourth argument, which is optional, is the aperture size, which is the size of the symbol colonel

113
00:06:58,630 --> 00:07:02,860
used for the image gradients finding image gradients default history.

114
00:07:03,190 --> 00:07:05,710
But we don't use them, but you use the default value here.

115
00:07:06,430 --> 00:07:08,290
So what we can see is kind of edge.

116
00:07:08,290 --> 00:07:09,460
We can suggest for shields.

117
00:07:09,460 --> 00:07:16,660
Yeah, just a minute max values here, and let's run just changed thresholds to take a look so we can

118
00:07:16,660 --> 00:07:17,650
widen the threshold.

119
00:07:17,650 --> 00:07:20,050
We can narrow the threshold and use them.

120
00:07:20,050 --> 00:07:20,880
Ed Schultz Yeah.

121
00:07:20,890 --> 00:07:22,660
And let's see what the results are.

122
00:07:25,840 --> 00:07:33,370
So look, this is the canny edge of the first image here with those parameters and put sort of parameters

123
00:07:33,370 --> 00:07:38,200
we used and you can see it looks, it looks quite good actually, in my opinion.

124
00:07:39,100 --> 00:07:41,490
This one would have wide thresholds look similar.

125
00:07:41,490 --> 00:07:42,850
It's not much different.

126
00:07:43,510 --> 00:07:48,220
This one with narrow thresholds definitely has less lines, and it's it's a bit harder to make out that

127
00:07:48,220 --> 00:07:49,250
there's a person here.

128
00:07:49,300 --> 00:07:55,420
You can probably see the car here, but it's a bit more visible, at least sort of in the sort of images

129
00:07:56,020 --> 00:08:01,390
and forward sort of thresholds we use basically again gave it a lot more lines as well.

130
00:08:01,780 --> 00:08:07,300
So there's something called up to a canny, not a canny was just a quick method that someone developed

131
00:08:07,780 --> 00:08:08,350
online.

132
00:08:08,410 --> 00:08:13,120
I think this year on StackOverflow should give you a lot of credit, but I can't remember and I can't

133
00:08:13,120 --> 00:08:14,170
find a way to sources.

134
00:08:14,170 --> 00:08:17,980
I remember pasting it into some of my code and using it.

135
00:08:18,790 --> 00:08:25,180
So what it does, it basically calculate it does do some blurring first, then gets a median image value

136
00:08:25,630 --> 00:08:29,040
and then uses its max value times.

137
00:08:29,050 --> 00:08:34,060
0.66 then points the max value four and five 1.3 three.

138
00:08:34,840 --> 00:08:40,390
So what we do after that is basically use these to get the thresholds or more thresholds range from

139
00:08:40,390 --> 00:08:41,500
zero to 255.

140
00:08:42,100 --> 00:08:45,660
So we're going it's going to get the values from that or to a function here.

141
00:08:46,300 --> 00:08:50,650
And let's see how it works because setting these strategies for kind of isn't always easy.

142
00:08:51,160 --> 00:08:55,630
So this looks quite nice, in my opinion, but you probably have a different opinion.

143
00:08:55,900 --> 00:09:00,100
Feel free to explore, even tweak this auto kidney function function.

144
00:09:00,580 --> 00:09:01,750
Tweak these thresholds.

145
00:09:01,990 --> 00:09:06,900
It's always good to experiment with of these functions on your own and with different images together.

146
00:09:07,150 --> 00:09:08,800
Get a few of them and what they do.

147
00:09:09,340 --> 00:09:09,850
Thank you.

148
00:09:09,850 --> 00:09:13,730
And let's move on to the next lesson, which is contours.

149
00:09:13,780 --> 00:09:14,770
Very important lesson.

150
00:09:15,160 --> 00:09:15,730
Stay tuned.
