Let's consider one additional example of pointwise operations. It's called image binarization or image thresholding in which you convert an import. Let's assume gray scale image to the binary format to represent the whole image as a mixture of object and background. In this case, it's typical to use so-called global thresholding techniques in which you just fix some parameter T as a threshold. If the value of pixel is greater than T, then we can say that, for example, this function will be equal to 0. Otherwise, the function will be equal to the maximum value like 255. There is so-called threshold function in OpenCV library, and it has several parameters in which helps you to transform the input image to the grayscale format to their binary format by choosing the parameter of the threshold. Particular example here is, you can say that T will be equal to 128 and all the values of pixel higher than 128 will be considered to be a white. Otherwise, all other pixels will be considered to be black. It's very difficult to choose the right parameter of this threshold. It's very typical to try to find the best value of this threshold in automatic way. Let me show you some details. What we know is the image produced by myself in which I draw some rectangle in the center of the image. After that, I added some awesome noise and you see that it's really difficult to find the right way to consider the threshold, but what can be done here, you can again compute the grayscale histogram here and you see that there are two bars. One, near the zero. It's something like black, part of the image and another mode of the histogram is in the right. It's right part. It's something close to white part, 255. One can just compute the histogram of the grayscale image. Then based on this histogram, try to obtain the best threshold. Something like this line in the middle of this histogram, but how to do it perfectly because you know that it's just an ideal case in which you have two modes, but in general images, you have many modes and how to choose the best one of them. One particular useful algorithm here is automatic choice of threshold is the smoothing of the histogram using one dimensional Gaussian filter. Generally speaking, I will discuss Gaussian filters in the next week when we consider a local transformation and local operations, but current time, just consider this like a smoothing of one dimensional signal. You have an input image and you see that it's some map and you see that there were a different way of pixel, so how to binarize it, how to obtain, where is the foreground and where is the background. At first, you can compute the histogram and you see that there are at least four modes in this histogram. Four peaks in this histogram, so you can't easily find the best value of threshold. You have four peaks and you have at least three local minimum. For sure, you have to choose one of them, but what is the best value of the local minimum of this histogram? How to choose this. In this case, you can just smooth this histogram using some Gaussian filter with some parameter of its Sigma, and you can obtain this smooth histogram or something like this. You see that, there is only one local minimum. The algorithm itself looks so really pretty simple. Just compute different Gaussian blurring of the histogram and you increase the Sigma parameter of the Gaussian kernel until you obtain on the one local minima. This local menu will be equal to the threshold. In this particular case, you see that, all the values lower than this threshold will be considered like background, for example, or foreground. It will be a color that's red and the remaining pixels will be drawn as a background like grid. We obtain something like this and can easily use black and white colors instead of green and red. The most important part and the most interesting part here is how to choose this threshold optimally. Before I show you a some heuristic, but what is the optimal choice of threshold hot say that my threshold is the best one. In this case, you should understand that the whole image can be considered as a mixture of two parts, background and foreground or object. For example, in this particular histogram, we compute the histograms of pixels correspond to object. It's curve 1 and the histogram of pixels that correspond to the background it is the curve 2. They intersect. We can't choose the thresholds so that all pixels low than threshold will be considered to be an object part, and all pixels higher than the threshold T will be considered as a background. But we need to choose this threshold in the optimal way. What is optimal? In this case, we can consider two errors. The first one is so-called false positive error. When we consider background as an object so that we have a value of pixel as a background, but algorithm say that it's part of an object. Another error is probability of meeting an object. We have some part of an object, but by mistake, we say that this value of this pixel is higher than our threshold. We say that this part of an object is the background. We have two errors and what we want to minimize, we want to minimize the overall error. For example, if we know the prior probabilities of a part of the image to be an object or to be a background, then you can compute the average loss or probability of our mistake and we just simply minimize this average error. In this case, for sure we need some assumptions about distributions of pixels inside the object and inside the background. It's typical to use some normal distribution and in this case here is the best the optimal choice of threshold, you compute the mean values of pixels inside an object part, you compute mean values of pixels inside the background. Then you compute the standard deviation of all pixels in the input image, and here is the equation for the threshold. But in practice, it's not convenient to assume that our pixels of object, or especially of a background, have normal distribution. In such case, one can use so-called Otsu's method or the method or proposed by Otsu's. It's again, it's very close to the optimal solution, but we don't need to assume to make some assumptions about Gaussian or normal distribution of pixels. In this case, you can again, you have let me assume that we computed again the color or the grayscale histogram and L parameter here is just a maximal value of pixels. Something like 256 in this particular case and what is our solution takes for. So we need to choose some threshold as an integer number between one and this maximum value of L. In this case, we can use any threshold T between one and L and in this case we can compute our error. The number of pixels lower than this, which has lower than this threshold and its Q_b its Q background and the number of pixels which are higher than this threshold, it's Q_o in this equation. Q object, it's the frequency of object pixels and for sure their sum to one, because we just split the whole image that two parts each pixel will be considered either to be part of an object or to be a part of an image and what we can do after we fixed the threshold, we can compute the variants of pixels inside each part, inside object part and inside background part. How to do it efficiently and that was the main idea of Otsu. At first let's compute just the standard deviation of pixels inside the whole image without division in the background and foreground, without any binarization techniques. Just their standard deviation of all pixels can be computed easily based on the grayscale histogram and the main idea here is that we can represent these variants, as the sum of two different variances. The first one is average inter-class variance. It's a variance inside pixels, inside object and pixels inside background and this is average inter-class variance. In the right-hand side you see that it's a variance of so it's some error. In general, we would like that our inter-class variance will be minimum, because we don't want to mix parts of objects and parts of background but it's more convenient from computational point of view not to minimize the inter-class variance, but to maximize the inter-class variance so the left part. To maximize the inter-class variance because it can be computed really efficiently just by recursive formulas. In this case, the algorithm is very simple. You just use any potential variance of threshold T starting from one till we reach the L, till we reach the value of maximal value of pixels and compute iteratively, compute the values of inter-class variance and in this particular case you can choose a threshold which maximizes the inter-class variance. That's the main idea of this technique. Unfortunately, in general, we don't expect that we can use only one threshold to perform really really good binarization. Let me show you one additional technique, so-called histeresis thresholding. Here is some example of very noisy image and the if we choose even the best possible threshold here, you can see that in this case, we have some part of object which I consider to be a background to see white halls inside this black image and you see also some part of the background in the left part of the image, which should be considered as the background, but they have a rather high value of pixel intensity and that's why we need to, our algorithm assumes that it's a part of objects. We need some additional post-processing procedure and it's the histeresis. What should be done? You should choose two thresholds, you should choose one threshold for background and one threshold for object or for foreground and you say that I'm pretty sure I'm 100 percent sure that if our pixel is less than the second threshold, then it's considered as an object. Otherwise, if this pixel is higher than the first threshold for background, then this part can be considered to be a background. But we have some part of pixels between these two thresholds and we need to consider if these pixels in the background or in the border between these two thresholds are in the background or in the object. But so in this case, we can consider the rest points and classify them on the object as an object part only if they have some corresponding, if they have some neighbors near the other object, so you start from the original image and then you remove all parts of pixels that cannot be considered as a part of object because they have some distances between object and this part. You see that we don't have left part here and you see that this threshold really works well. One additional technique here is widely used for binarization of scans of text. We have let's imagine you have some book and you perform some scans or you can just make a photo of a page of this book and in general, you know that there are some symbols or characters inside these books and all the rest is the background. In this case, so something like this, in this case, you can compute again the greyscale histogram and you see several peaks and you know that one of these peaks correspond to the white part of the paper to the background and only small part of this peaks correspond to the black values of characters. In this case, you can choose the threshold so that based on some predefined parameter of percent, which should be chosen as follows. We move to the left part of the histogram and choose the threshold in such a way that we are pretty sure that all pixels that are lower than this threshold should be considered as a character, as a symbol. You see the histogram with two local minimums, but we choose the left, the minimal value of this local minimum. You see the result, something like this.