Let us consider one additional way to implement the image binarization using so called local thresholding. So let me show you the differences between the previous methods, which can be called like global thresholding. So you see here some kind of input image but you can notice that the lighting and illumination inside this image is not constant. So you see that there are some more bright part and there are some more dark part here. And if you try to choose the threshold, even in the optimal way, you can obtain something like this. So for sure, you see that there are some digits which can be considered as an object part here. But you see in the bottom part of this image, there are some images, there are some pixels which are considered as a part of an object. But in general, they are a part of a background and some spot in the right hand side of this image. And these issues can be dealt by using, for example, hysteresis thresholding because you see that it's practically all part of the bottom of this image considered as an object part. So you can't use hysteresis thresholding in this particular way. So the idea here is to process image using the, some neighborhoods of pixels so some local neighborhoods. And it's an example of local operations, local processing of local image processing. So let me show you the grid of pixels here in this particular pixel. And you see that you choose some kind of pixel and you draw some neighborhood around these pixels. So here, you see two neighborhoods, blue and yellow. And in order to compute the threshold to consider this particular pixel as a part of object over ground, you automatically compute threshold using only pixels inside this neighborhood. So you assume that all the illumination inside all pixels in this particular neighborhood are more or less constant. So in this case, you can compute the global threshold efficiently. But for sure, it's really time consuming preparation. So you need to consider each pixel individually. You need to compute many local histograms. For sure, they are pretty small. But anyway, you should do it for each individual pixel. And after that, you can compute the best threshold for each histogram individually. So you see, for example, in the blue neighborhood, the threshold is higher than the threshold in the yellow neighborhood. So in this particular case, we can choose the size of the neighborhood in order to remove all the parts of the image, which have more or less constant intensity. We assume that constant intensity is something related to background. And in this case, by choosing the right layers of parameters, we can easily select only the digits inside this image. So local thresholding really works here like a shot. But it's really difficult to choose the threshold and it's really difficult to choose the size of the local neighborhood here. So how to do it in an automatic way and how to make it better, how to achieve the best quality. And in this case, we can recall about the so called Niblack`s method. So it's a local thresholding technique which was proposed by Niblack. And it uses absolutely the same idea, but it tries to choose threshold using some kind of pretty simple rule. So here is the equation for this method, we compute the local mean. So we, again, we divide the whole image into some local neighborhoods of pixels. We compute, in each local neighborhood, you compute the mean value of all pixels inside this. Even then you compute standard deviation, so called local sigma here. And then you have some pre-defined parameter K. So the real value of threshold for this particular local neighborhood is equal to local mean, subtracting some local sigma, multiplied by this parameter K. So if you use, for example, K equal to zero. I don't recommend to use it. But anyway, if you use K equal to zero, then what will be done? You compute the local mean, local average of all pixels and all pixels that have intensity lower than this mean, will be considered as an object. Otherwise, all pixels which are higher than this mean will be considered the background. In general, this particular technique won't work if K is equal to zero, because you say that it's actually, the result will have too much pixels of background. And in general, for sure we assume that the number of pixels inside an object are much lower when compared to the number of pixels inside the background. So we need to select the pixels to be a part of the object. We are 100% sure that they are part of the objects so that we have to reduce the threshold. And in this case, you can use such kind of formula. So for example, if you recall our cap some probability theory here, you will remember the rule of three sigmas. When if the distribution of pixels here is normal, then you can be practically assured that all pixels allocated between the mean of all pixels minus three sigma and mean of the pixels plus three sigma. So I recommend to use K, something higher than zero and something lower than three, so in some particular case. But anyway K is a parameter and you have to choose it carefully. And the result of the image thresholding will be definitely depend on the choice of parameter K. But anyway, just let me recap you that we considered very similar rule for characters thresholding in text. And it's not surprising that the Niblack's method is also used for segmentation of scanned text. So like in this example, and you see that. But what is more interesting here when compared to the characters thresholding and character global thresholding is you see that the illumination of different part of this input image are really different. So the left part is more dark and the right part is more white, has more white color because probably when you made the scan, then the source of illumination was located to the right part of the camera. So you see here is the result. And you see that the local thresholding here really works. But what should I say here and what should what I should warn you. The choice of the size of the neighborhood is really extremely important here. Because for example, if you use very, very large neighborhood, then you can't assume that all the pixels inside the neighborhood has practically the same illumination. But on the contrast, if you choose very low neighborhood, something like five by five pixels, in this case, it's typical that you can see that most parts of the object will be considered here. And according to the Niblack's formula, you will be sure that this part will be missed. So it's really difficult to choose the best possible way to split the whole image into this local parts. And I believe that, in general, the proper choice of such kind of neighborhoods will strongly believe on the kind of the input image. So you need some prior information about input image, about sources of illumination and so on and so forth to understand what is the best possible way to choose the threshold and to choose the size of the neighborhood. So let's move to some practical examples of how to choose thresholds using OpenCV Library.