So now let's move on to troubling binaries, Asian and adaptive threshold in which is chapter nine and
nine to not book here.
So let's go to open this up.
I've already got it open here, so let's just disclose that and we can begin.
So firstly, what is binaries issued?
So you may think it's making something binary and you'd be right?
Well, what are we making binary exactly?
Well, we're making the colors.
Or should I say the pixels in an image zero or one?
So how do we do that?
So before we, let's just run this code just to declare this stimulus function.
And now we move on onto this slide, where I'm going to show you an example of what binaries station
is.
So take a look at the original image right here.
It's a smooth gradient from white to black with all the shades of gray in between.
And now you see half of it is black, half of it is white.
So that's exactly what finalization does.
It turned that image into basically everything above a certain color threshold or intensity, which
is probably 127 here.
Everything above it basically goes to white.
Everything below 127 goes to black.
That's how we get this type of photocopy type effects on using computer vision, using computer vision
algorithms to create a binary vision type effect.
So we're going to take a look at the other types of binary session right now.
This inverted binary addition, which is basically the opposite instead of everything going to white,
everything goes to black.
And so if everything going to black on this end, it goes to white.
So you just sort of flip that around.
Now what is trunk now?
Truncation may not be immediately obvious.
What is what's happening here, but if you look carefully, you can see it move some black degree and
then it steers degree right around here.
But retrogressive image and trunk basically means instead of sending everything to white to 255, which
is a value up here, it sends everything to the last highest value, which is a truncation threshold,
which is 127 mm here.
So anything above 127 becomes 127.
Now let's look at two zero two zero sort of does something similar, but it's very opposite exactly.
As you can see here, everything below 127 goes to black, and it leaves this nice, smooth gradient,
which which was existing in the original image right here.
So everything above the threshold remains untouched.
Everything below the threshold goes to zero.
That's why it's close to zero and to zero.
Invert is usually is exactly the opposite of what this is.
So everything above the treasure instead of going to white, goes to black.
So you're making all the values above 127 zero zero here and leaving all the values below 127, as is.
So we don't touch those.
So let's move on to this.
Let's get a scan image here.
So I'm going to use would get which is a nice little command you can use to download files of the internet
to your terminal.
So we just don't have that file and that file is this file scandal, JPEG.
So what we're going to do?
We're going to open this up and display the original image here, which you can see below.
This nice original image here, which is a scan of a photocopy of a book of I believe it's a book,
is a picture I took, yes, of all of my data science textbooks.
And what we're going to do, we're going to apply each of those different thresholds that you saw above.
So let's take a look and see how they perform.
All right, so let's take a look at our thresholds.
So the first one we do was a binaries and threshold at 127.
Now this looks quite bad.
Isn't it quite different from the original image?
But that's just because everything at 1:27 remember, let's go up to the top in binaries issue everything
below a value becomes black.
So what this is seeing is that everything that was below 127.
And when I say 127, I hope, you know, I'm talking about the pixel value.
I remember pixel values and grayscale images range from zero to 255.
So everything below what the 127 intensity goes to black and everything above it remains the same.
That's what actions are.
Everything above it goes to 255, which is why you have these little patches of white in between.
Now let's take a look at the other trestles.
So the inverse binary threshold here at one threshold 127 gives us is actually a pretty nice image.
So it actually looks a lot better than the original threshold that we did.
So you can see all the text now becomes white and all the white spots become black.
Well, the lighter colored spots, I should say.
However, you can see there's some sort of clipping area going on here.
So basically, these are overblown.
So these are bad.
These are literally bad thresholds.
But these distress rules are set to I would say this is too low here that would give us this.
So you need to increase this and it probably would get some of this text texture.
So let's take a look.
The trunk removed would trunk just keeps everything like water, moderate, medium, gray.
So everything above that was supposed to be nice, bright and white kind of stays at gray.
It's a lot of green smudge threshold to zero.
Basically, everything that was below lower value goes to black.
As you can see, the darker is an image here of everything goes to black, whereas everything above
127 remains the same.
And no threshold to zero in this is basically the opposite of that.
So you can see the effect here.
So remember, in those examples above, we had to set that threshold.
But what if we wanted to calculate that this pressure automatically like, let's suppose we were taking
scans of different documents that you have you need, like different ranges of thresholds to get the
exact binaries so you don't.
So, you know, it's nice and neat.
So that's where we use adaptive threshold.
These are little algorithms that actually run some calculations on the image and try to figure out what's
the optimum threshold value.
So OpenCV has two implementations that are quite useful trust us to which the second one I want to show
you which is the better one and adaptive trust means see?
So you can see these are the parameters these threshold and algorithms stick.
I'm not going to go through this in too much detail because you can read the breakdown of this here.
But now let's just test this out.
OK, so let's run this code and explain to cool it as we go along.
So what we're doing here, we're firstly doing a regular threshold value.
All right.
So I should have probably set above what are we doing when you're using these thresholds?
We set two parameters in the threshold here.
You said 127, which is a threshold value and 255 which is a max value that you wanted to go to.
So everything above that goes to 255.
This is a way we can actually control the threshold so you can use these values to actually do threshold
if you wanted to.
So no, actually, I like to do it, but it's a good practice to actually do some Gaussian blur before
threshold limit just to kind of smoothing the image how it looks.
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
good practice.
So what we're going to do, we're going to take a look at the adaptive threshold.
So if you use the adaptive threshold, then we use it to see V2 Dot Adaptive Threshold used it for the
input image said the maximum value we wanted to go to, said the algorithm we want to use.
This one is key to adaptive trust me and C and what type of threshold we're going to do.
So we're going to do a normal threshold binary.
You can do threshold inverse binary if you wanted to as well.
And what we're going to do is take a look at a tree and five others a block size because those are calculations
parameters to set the calculation how how does how would those two calculations both here?
So you can use block size, which is usually the neighborhood of pixels.
If you wanted to take into consideration a bigger area, you increase the block size and constancy,
which is subtracted and a weighted mean no.
This is something you can really play, which I haven't actually done much experimentation with this.
However, these values with the default values use of open CVS documentation.
So let's just go with them and you can feel free to experiment if you want.
So another take a look at what suits threshold.
So to run this usage threshold?
However, what you do, we just put plus US-EU in here and you set the threshold to zero in the beginning
here.
So it's a bit of a it's a bit finicky, I should say, not finicky, but a little bit unintuitive.
And how to use our to threshold is probably going to have to look at this, this statement in some documentation
when my code to get it, but also works quite well.
So we'll take a look at the results below.
So let's see.
This was our adaptive thresholding.
Sorry, this is this is the original image.
This is our regular thresholding binary here.
This is our adaptive mean thresholding.
And you can see that she looks quite nice, it looks very good, actually.
And Ozu, which I was saying was better, actually performed worse in this example.
It probably depends on what you're doing, obviously.
So maybe you just experiment with different with different methods if you want, depending on your use
case.
There's one thing I should know there was a bit of a mistake I made in the previous five minutes ago.
Roughly, you can see the threshold binary how it looks here.
That's the standard threshold binary.
However, when asked to run threshold binary here, it actually looked like this big blob of mess.
And it's not supposed to, actually.
So we can take a look at the code and see and I already saw the mistake that was in that code and not
sure why I left it in and why I didn't see it.
But this line is supposed to be this here.
So you can see if I can do this, you can see the differences here is that I've said the threshold to
200 initially now rather than 127.
So that's if you run this again, you would get the image we want below.
All right.
So that's it for finalization and adopted by finalization.
We're now going to take a look at Esky images.
Chushul Local Treasure Local is actually a very good algorithm I've used for actually in real life.
For documents, getting it performs very, very well in terms of how it calculates the optimum threshold
value, and we can see an example of it here, and you can see it looks quite good.
It looks pretty much pretty similar to the adaptive mean thresholding.
However, you can see it is better quality.
There's some sort of noise around the words which make it a bit illegible, at least to me, but skeletons.
Getty Images.
I should say yes.
I get images that show a local, which is done here, so let's take a look at the code.
So you put Chushul local from ASCII image filters, so it's in the filter package.
Probably a lot more the filters available there.
And what we do, it actually converts image to HSV.
We actually have to convert the image to HSV color space before implementing this, this type of threshold.
And then what we do, we just take the HSV image here to call it V.
We set this, we set these parameters here, which is block size and offset.
These are probably configuration of parameters that you should probably play with so that you can actually
get the most optimum threshold.
However, just so you know, this is an adaptive threshold, and that's it.
You don't actually have to set the threshold like you had previously admitted.
So now we can run this and generate this nice image and this this is just a little note I put in here
and white threshold by blurring before threshold is important.
You can see this is a big reason for that.
And actually, it's because it removes noise in the image.
See this noise in the image here around the words that actually made it illegible.
That's what removing.
That's what we that's why we do some blur before.
So that concludes, this lesson will go on to Chapter 10, which is dilation, erosion and edge detection.
Thank you.