1
00:00:00,120 --> 00:00:05,850
So now let's move on to troubling binaries, Asian and adaptive threshold in which is chapter nine and

2
00:00:05,850 --> 00:00:06,930
nine to not book here.

3
00:00:07,380 --> 00:00:08,820
So let's go to open this up.

4
00:00:09,000 --> 00:00:14,880
I've already got it open here, so let's just disclose that and we can begin.

5
00:00:15,360 --> 00:00:17,880
So firstly, what is binaries issued?

6
00:00:18,360 --> 00:00:21,990
So you may think it's making something binary and you'd be right?

7
00:00:22,440 --> 00:00:24,330
Well, what are we making binary exactly?

8
00:00:24,360 --> 00:00:25,860
Well, we're making the colors.

9
00:00:25,860 --> 00:00:31,110
Or should I say the pixels in an image zero or one?

10
00:00:31,650 --> 00:00:32,720
So how do we do that?

11
00:00:32,730 --> 00:00:37,890
So before we, let's just run this code just to declare this stimulus function.

12
00:00:38,880 --> 00:00:43,890
And now we move on onto this slide, where I'm going to show you an example of what binaries station

13
00:00:43,980 --> 00:00:44,340
is.

14
00:00:44,790 --> 00:00:47,580
So take a look at the original image right here.

15
00:00:48,120 --> 00:00:53,280
It's a smooth gradient from white to black with all the shades of gray in between.

16
00:00:54,360 --> 00:00:57,660
And now you see half of it is black, half of it is white.

17
00:00:58,230 --> 00:01:00,410
So that's exactly what finalization does.

18
00:01:00,440 --> 00:01:07,830
It turned that image into basically everything above a certain color threshold or intensity, which

19
00:01:07,830 --> 00:01:09,810
is probably 127 here.

20
00:01:10,440 --> 00:01:13,020
Everything above it basically goes to white.

21
00:01:13,200 --> 00:01:15,870
Everything below 127 goes to black.

22
00:01:16,350 --> 00:01:22,500
That's how we get this type of photocopy type effects on using computer vision, using computer vision

23
00:01:22,500 --> 00:01:25,740
algorithms to create a binary vision type effect.

24
00:01:26,220 --> 00:01:29,430
So we're going to take a look at the other types of binary session right now.

25
00:01:30,000 --> 00:01:34,980
This inverted binary addition, which is basically the opposite instead of everything going to white,

26
00:01:34,980 --> 00:01:35,950
everything goes to black.

27
00:01:35,970 --> 00:01:40,320
And so if everything going to black on this end, it goes to white.

28
00:01:40,360 --> 00:01:42,570
So you just sort of flip that around.

29
00:01:43,470 --> 00:01:46,440
Now what is trunk now?

30
00:01:46,620 --> 00:01:49,330
Truncation may not be immediately obvious.

31
00:01:49,350 --> 00:01:55,530
What is what's happening here, but if you look carefully, you can see it move some black degree and

32
00:01:55,530 --> 00:01:57,990
then it steers degree right around here.

33
00:01:58,320 --> 00:02:04,740
But retrogressive image and trunk basically means instead of sending everything to white to 255, which

34
00:02:04,740 --> 00:02:10,110
is a value up here, it sends everything to the last highest value, which is a truncation threshold,

35
00:02:10,110 --> 00:02:11,460
which is 127 mm here.

36
00:02:11,910 --> 00:02:15,360
So anything above 127 becomes 127.

37
00:02:16,170 --> 00:02:22,350
Now let's look at two zero two zero sort of does something similar, but it's very opposite exactly.

38
00:02:22,350 --> 00:02:29,760
As you can see here, everything below 127 goes to black, and it leaves this nice, smooth gradient,

39
00:02:30,240 --> 00:02:33,650
which which was existing in the original image right here.

40
00:02:33,660 --> 00:02:36,300
So everything above the threshold remains untouched.

41
00:02:36,780 --> 00:02:39,030
Everything below the threshold goes to zero.

42
00:02:39,300 --> 00:02:41,910
That's why it's close to zero and to zero.

43
00:02:41,910 --> 00:02:45,450
Invert is usually is exactly the opposite of what this is.

44
00:02:45,840 --> 00:02:49,590
So everything above the treasure instead of going to white, goes to black.

45
00:02:50,130 --> 00:02:58,230
So you're making all the values above 127 zero zero here and leaving all the values below 127, as is.

46
00:02:58,410 --> 00:02:59,660
So we don't touch those.

47
00:03:00,330 --> 00:03:02,130
So let's move on to this.

48
00:03:02,130 --> 00:03:03,480
Let's get a scan image here.

49
00:03:03,490 --> 00:03:08,520
So I'm going to use would get which is a nice little command you can use to download files of the internet

50
00:03:08,520 --> 00:03:09,360
to your terminal.

51
00:03:10,770 --> 00:03:15,870
So we just don't have that file and that file is this file scandal, JPEG.

52
00:03:17,100 --> 00:03:18,270
So what we're going to do?

53
00:03:18,900 --> 00:03:23,460
We're going to open this up and display the original image here, which you can see below.

54
00:03:24,600 --> 00:03:30,600
This nice original image here, which is a scan of a photocopy of a book of I believe it's a book,

55
00:03:30,600 --> 00:03:34,680
is a picture I took, yes, of all of my data science textbooks.

56
00:03:35,490 --> 00:03:40,560
And what we're going to do, we're going to apply each of those different thresholds that you saw above.

57
00:03:41,130 --> 00:03:43,650
So let's take a look and see how they perform.

58
00:03:47,030 --> 00:03:49,730
All right, so let's take a look at our thresholds.

59
00:03:50,270 --> 00:03:54,890
So the first one we do was a binaries and threshold at 127.

60
00:03:55,790 --> 00:03:57,110
Now this looks quite bad.

61
00:03:57,410 --> 00:03:59,780
Isn't it quite different from the original image?

62
00:04:00,260 --> 00:04:08,570
But that's just because everything at 1:27 remember, let's go up to the top in binaries issue everything

63
00:04:08,570 --> 00:04:10,400
below a value becomes black.

64
00:04:10,910 --> 00:04:14,690
So what this is seeing is that everything that was below 127.

65
00:04:15,230 --> 00:04:18,380
And when I say 127, I hope, you know, I'm talking about the pixel value.

66
00:04:18,440 --> 00:04:22,700
I remember pixel values and grayscale images range from zero to 255.

67
00:04:23,540 --> 00:04:31,100
So everything below what the 127 intensity goes to black and everything above it remains the same.

68
00:04:31,610 --> 00:04:33,080
That's what actions are.

69
00:04:33,290 --> 00:04:38,420
Everything above it goes to 255, which is why you have these little patches of white in between.

70
00:04:39,410 --> 00:04:41,600
Now let's take a look at the other trestles.

71
00:04:44,140 --> 00:04:51,880
So the inverse binary threshold here at one threshold 127 gives us is actually a pretty nice image.

72
00:04:51,940 --> 00:04:55,960
So it actually looks a lot better than the original threshold that we did.

73
00:04:56,590 --> 00:05:01,190
So you can see all the text now becomes white and all the white spots become black.

74
00:05:01,210 --> 00:05:03,520
Well, the lighter colored spots, I should say.

75
00:05:04,150 --> 00:05:07,930
However, you can see there's some sort of clipping area going on here.

76
00:05:07,960 --> 00:05:09,670
So basically, these are overblown.

77
00:05:10,180 --> 00:05:10,990
So these are bad.

78
00:05:11,170 --> 00:05:12,760
These are literally bad thresholds.

79
00:05:12,760 --> 00:05:18,980
But these distress rules are set to I would say this is too low here that would give us this.

80
00:05:19,270 --> 00:05:22,900
So you need to increase this and it probably would get some of this text texture.

81
00:05:25,550 --> 00:05:26,330
So let's take a look.

82
00:05:26,510 --> 00:05:31,220
The trunk removed would trunk just keeps everything like water, moderate, medium, gray.

83
00:05:31,880 --> 00:05:36,670
So everything above that was supposed to be nice, bright and white kind of stays at gray.

84
00:05:37,010 --> 00:05:40,280
It's a lot of green smudge threshold to zero.

85
00:05:40,880 --> 00:05:44,000
Basically, everything that was below lower value goes to black.

86
00:05:44,000 --> 00:05:48,320
As you can see, the darker is an image here of everything goes to black, whereas everything above

87
00:05:48,320 --> 00:05:50,090
127 remains the same.

88
00:05:50,870 --> 00:05:54,200
And no threshold to zero in this is basically the opposite of that.

89
00:05:54,410 --> 00:05:55,850
So you can see the effect here.

90
00:05:57,050 --> 00:06:02,510
So remember, in those examples above, we had to set that threshold.

91
00:06:02,990 --> 00:06:08,360
But what if we wanted to calculate that this pressure automatically like, let's suppose we were taking

92
00:06:08,360 --> 00:06:13,460
scans of different documents that you have you need, like different ranges of thresholds to get the

93
00:06:13,460 --> 00:06:16,340
exact binaries so you don't.

94
00:06:17,120 --> 00:06:18,710
So, you know, it's nice and neat.

95
00:06:18,980 --> 00:06:22,010
So that's where we use adaptive threshold.

96
00:06:22,250 --> 00:06:28,100
These are little algorithms that actually run some calculations on the image and try to figure out what's

97
00:06:28,100 --> 00:06:29,570
the optimum threshold value.

98
00:06:30,020 --> 00:06:35,960
So OpenCV has two implementations that are quite useful trust us to which the second one I want to show

99
00:06:35,960 --> 00:06:39,190
you which is the better one and adaptive trust means see?

100
00:06:39,950 --> 00:06:43,070
So you can see these are the parameters these threshold and algorithms stick.

101
00:06:43,840 --> 00:06:48,440
I'm not going to go through this in too much detail because you can read the breakdown of this here.

102
00:06:49,100 --> 00:06:50,960
But now let's just test this out.

103
00:06:51,230 --> 00:06:55,340
OK, so let's run this code and explain to cool it as we go along.

104
00:06:56,210 --> 00:07:00,110
So what we're doing here, we're firstly doing a regular threshold value.

105
00:07:00,290 --> 00:07:00,650
All right.

106
00:07:01,250 --> 00:07:05,330
So I should have probably set above what are we doing when you're using these thresholds?

107
00:07:05,750 --> 00:07:08,360
We set two parameters in the threshold here.

108
00:07:08,840 --> 00:07:14,630
You said 127, which is a threshold value and 255 which is a max value that you wanted to go to.

109
00:07:15,350 --> 00:07:17,300
So everything above that goes to 255.

110
00:07:17,360 --> 00:07:23,360
This is a way we can actually control the threshold so you can use these values to actually do threshold

111
00:07:23,530 --> 00:07:24,280
if you wanted to.

112
00:07:25,040 --> 00:07:31,210
So no, actually, I like to do it, but it's a good practice to actually do some Gaussian blur before

113
00:07:31,230 --> 00:07:33,740
threshold limit just to kind of smoothing the image how it looks.

114
00:07:33,740 --> 00:07:40,000
It has less noise in it, but we're not going to do it here, just left it in for offers as as it's

115
00:07:40,010 --> 00:07:40,760
good practice.

116
00:07:41,570 --> 00:07:45,180
So what we're going to do, we're going to take a look at the adaptive threshold.

117
00:07:45,680 --> 00:07:52,610
So if you use the adaptive threshold, then we use it to see V2 Dot Adaptive Threshold used it for the

118
00:07:52,610 --> 00:07:58,190
input image said the maximum value we wanted to go to, said the algorithm we want to use.

119
00:07:58,190 --> 00:08:03,860
This one is key to adaptive trust me and C and what type of threshold we're going to do.

120
00:08:03,870 --> 00:08:05,840
So we're going to do a normal threshold binary.

121
00:08:05,960 --> 00:08:08,750
You can do threshold inverse binary if you wanted to as well.

122
00:08:09,560 --> 00:08:15,470
And what we're going to do is take a look at a tree and five others a block size because those are calculations

123
00:08:16,460 --> 00:08:21,110
parameters to set the calculation how how does how would those two calculations both here?

124
00:08:21,680 --> 00:08:24,920
So you can use block size, which is usually the neighborhood of pixels.

125
00:08:24,920 --> 00:08:31,160
If you wanted to take into consideration a bigger area, you increase the block size and constancy,

126
00:08:31,550 --> 00:08:33,800
which is subtracted and a weighted mean no.

127
00:08:34,220 --> 00:08:38,120
This is something you can really play, which I haven't actually done much experimentation with this.

128
00:08:38,720 --> 00:08:43,740
However, these values with the default values use of open CVS documentation.

129
00:08:43,790 --> 00:08:47,750
So let's just go with them and you can feel free to experiment if you want.

130
00:08:48,410 --> 00:08:51,860
So another take a look at what suits threshold.

131
00:08:52,010 --> 00:08:54,620
So to run this usage threshold?

132
00:08:54,650 --> 00:09:03,140
However, what you do, we just put plus US-EU in here and you set the threshold to zero in the beginning

133
00:09:03,140 --> 00:09:03,410
here.

134
00:09:04,100 --> 00:09:10,340
So it's a bit of a it's a bit finicky, I should say, not finicky, but a little bit unintuitive.

135
00:09:10,340 --> 00:09:16,370
And how to use our to threshold is probably going to have to look at this, this statement in some documentation

136
00:09:16,370 --> 00:09:20,090
when my code to get it, but also works quite well.

137
00:09:20,960 --> 00:09:23,270
So we'll take a look at the results below.

138
00:09:23,450 --> 00:09:24,170
So let's see.

139
00:09:25,130 --> 00:09:26,850
This was our adaptive thresholding.

140
00:09:27,710 --> 00:09:29,600
Sorry, this is this is the original image.

141
00:09:30,290 --> 00:09:32,480
This is our regular thresholding binary here.

142
00:09:33,380 --> 00:09:35,480
This is our adaptive mean thresholding.

143
00:09:38,520 --> 00:09:43,080
And you can see that she looks quite nice, it looks very good, actually.

144
00:09:43,620 --> 00:09:47,850
And Ozu, which I was saying was better, actually performed worse in this example.

145
00:09:48,660 --> 00:09:51,000
It probably depends on what you're doing, obviously.

146
00:09:51,450 --> 00:09:57,900
So maybe you just experiment with different with different methods if you want, depending on your use

147
00:09:57,900 --> 00:09:58,320
case.

148
00:09:59,250 --> 00:10:05,430
There's one thing I should know there was a bit of a mistake I made in the previous five minutes ago.

149
00:10:05,550 --> 00:10:08,760
Roughly, you can see the threshold binary how it looks here.

150
00:10:09,300 --> 00:10:10,950
That's the standard threshold binary.

151
00:10:10,980 --> 00:10:18,300
However, when asked to run threshold binary here, it actually looked like this big blob of mess.

152
00:10:18,900 --> 00:10:20,940
And it's not supposed to, actually.

153
00:10:21,360 --> 00:10:27,630
So we can take a look at the code and see and I already saw the mistake that was in that code and not

154
00:10:27,630 --> 00:10:31,770
sure why I left it in and why I didn't see it.

155
00:10:31,890 --> 00:10:34,920
But this line is supposed to be this here.

156
00:10:35,910 --> 00:10:40,710
So you can see if I can do this, you can see the differences here is that I've said the threshold to

157
00:10:40,710 --> 00:10:44,280
200 initially now rather than 127.

158
00:10:44,850 --> 00:10:50,740
So that's if you run this again, you would get the image we want below.

159
00:10:51,720 --> 00:10:52,180
All right.

160
00:10:52,320 --> 00:10:57,900
So that's it for finalization and adopted by finalization.

161
00:10:58,260 --> 00:11:01,250
We're now going to take a look at Esky images.

162
00:11:01,270 --> 00:11:07,170
Chushul Local Treasure Local is actually a very good algorithm I've used for actually in real life.

163
00:11:07,710 --> 00:11:13,620
For documents, getting it performs very, very well in terms of how it calculates the optimum threshold

164
00:11:13,620 --> 00:11:19,290
value, and we can see an example of it here, and you can see it looks quite good.

165
00:11:19,290 --> 00:11:22,590
It looks pretty much pretty similar to the adaptive mean thresholding.

166
00:11:23,220 --> 00:11:25,860
However, you can see it is better quality.

167
00:11:26,250 --> 00:11:34,490
There's some sort of noise around the words which make it a bit illegible, at least to me, but skeletons.

168
00:11:34,800 --> 00:11:35,340
Getty Images.

169
00:11:35,340 --> 00:11:35,940
I should say yes.

170
00:11:35,940 --> 00:11:41,070
I get images that show a local, which is done here, so let's take a look at the code.

171
00:11:41,550 --> 00:11:46,890
So you put Chushul local from ASCII image filters, so it's in the filter package.

172
00:11:47,370 --> 00:11:49,230
Probably a lot more the filters available there.

173
00:11:49,890 --> 00:11:54,060
And what we do, it actually converts image to HSV.

174
00:11:54,070 --> 00:11:59,850
We actually have to convert the image to HSV color space before implementing this, this type of threshold.

175
00:12:00,570 --> 00:12:05,340
And then what we do, we just take the HSV image here to call it V.

176
00:12:05,940 --> 00:12:09,810
We set this, we set these parameters here, which is block size and offset.

177
00:12:10,860 --> 00:12:16,710
These are probably configuration of parameters that you should probably play with so that you can actually

178
00:12:16,710 --> 00:12:19,050
get the most optimum threshold.

179
00:12:19,500 --> 00:12:22,560
However, just so you know, this is an adaptive threshold, and that's it.

180
00:12:22,890 --> 00:12:28,230
You don't actually have to set the threshold like you had previously admitted.

181
00:12:29,250 --> 00:12:36,090
So now we can run this and generate this nice image and this this is just a little note I put in here

182
00:12:36,090 --> 00:12:39,000
and white threshold by blurring before threshold is important.

183
00:12:39,570 --> 00:12:41,860
You can see this is a big reason for that.

184
00:12:41,880 --> 00:12:44,460
And actually, it's because it removes noise in the image.

185
00:12:44,850 --> 00:12:48,060
See this noise in the image here around the words that actually made it illegible.

186
00:12:48,480 --> 00:12:49,910
That's what removing.

187
00:12:49,920 --> 00:12:52,200
That's what we that's why we do some blur before.

188
00:12:53,010 --> 00:12:59,850
So that concludes, this lesson will go on to Chapter 10, which is dilation, erosion and edge detection.

189
00:13:00,120 --> 00:13:00,390
Thank you.
