Let us now present a very simple extension of histogram equalization which is called histogram matching. So let us illustrate the problem. We saw before, histogram equalization where we take any histogram, any pixel distribution, and we match it to something which is as uniform as possible. But imagine that we have this pixel distribution, which is the one we have seen before, and somebody comes and tells us, this is your target. I don't want to do an equalization. I want you to finish with a histogram which is as close as possible to this one. So the idea is as before, we're going to have to find a map, which is written here. This is the map that we have been doing before. A map such that from here from this distribution we can go to as close as possible to this one in this craze, being the way that I'm going to describe in a second. What we are going to obtain is this, which is very similar, not exactly, but very, very similar. We already saw in the previous video that we cannot always get exactly the specified histogram. In the previous video, was equalized. But we can get very close to it. And this is what we see here. So how do we do that? You already have all the tools to do that. Let me illustrate how. We start from the distribution of the current image. Whatever that distribution is, it's the image that is given to you. You know how to map that to a uniform distribution. Okay? Now, you are giving a desire distribution. You know how to map this desired distribution, the target distribution also to a uniform distribution. Therefore, if you know how to go from here to here and from here to here, if you invert this, you know how to go all the path that is the desired path. So, you're going to map a pixel value from here to here, then you're going to ask, okay, where is this pixel coming from, this pixel value coming from when I want to go here, and you just invert that. So, you basically have one map and our map and you just invert the second map. So let's say that pixel value a, a pixel with a gray value seven goes to ten here. Then you ask, okay, who, from here. What peaks a value, one to ten, and somebody, you know, this map tells you fifteen. So you end up going from seven to ten, and inverse of ten is fifteen, so you went from seven to fifteen. And that's how you basically get a histogram matching. Just by doing the histogram equalizations, now you know how to match any two desired distributions. There's one caveat, as we saw in the previous video, the, the maps don't have to be one to one. So, maybe there is more than one pixel value here that ends up at the same pixel value there. So, when you go from seven, let's say to ten, maybe there is fifteen, from here goes to ten. And maybe also twenty goes to ten. So. Am I going to map seven to fifteen or I'm going to map seven to twenty? You're going to have to make a decision. The most common decision is to pick the one that is closest to the original, so you do the less deformation of the pixel values of the image. But you can make other decisions, as long as those decisions are consistent and as long as those decisions guarantee that you end up with an increase in function. Remember that we don't want to flip pixel values. So once again, you do two histogram equalizations, you invert one of them. And then you basically obtain histogram matching. So let's see how that works. This is a particular example. We have a very dark image here. And we see that, in the histogram mostly dark values are used. So it's hard for us to see what's going on here, because it's very, very dark. If we do histogram equalization, we get a too bright image as we see here. This is actually the mapping, the T R mapping that we always are looking for. And this is going to be the new histogram. And we see both from the histogram and from the actual image that it's way too bright. So we haven't achieved what we really wanted which is a nicer looking image that we can actually observe the different regions of that image. If you still have that, we do histogram matching so if somebody comes and tells us I know that this is the distribution that is going to be great for my image. Then what we do is again, we go form the original distribution to equalization. We go from, this distribution, the new distribution here, we go to equalization, which is, going to be this map. We invert that map, which is going to be this. And then we go in two steps from the original histogram to the equalized one. From the equalized one through the inverse map to the new pixel value. And we get this distribution, which gives us this image, which looks much nicer than this one and also help us to look at some of its details. So we basically got histogram matching by doing twice histogram equalization. Once direct and the other which is inverted. And again, if we have ambiguities we just make a decision by using for example, the pixel value closest to the one that we started in the original image. So you got two for the price of one. If you know how to do histogram equalization, you know how to do histogram matching. Of course, there is a big art, in designing your target histogram and that's very application dependent. Let's just mention one last thing about histogram equalization or histogram matching. So far, we considered the whole image, and we say, let's just look at the whole image, let's look at the distribution, and let's match those pixel distributions to a new distribution, either an equalization or a specified distribution. You can do the same operations locally. So for example, if we take this, and we do histogram, global histogram equalization, we're going to get this. Sometimes it looks a bit better, but there is a compromise. I only have 256 pixel values to distribute for my whole image. So, I may not be able to do a very strong contrast at the same time here and here. So, the way to solve this is to basically consider regions and say, let just do histogram equalization for example of this region and let's do histogram equalization of this region. They can actually be overlapping and we are going to treat each region independently. And maybe by doing that, I can basically improve the contrast in every region in the image, when there is a large variability, like in this particular image. This is the result of that and I want you for example to concentrate on this region. How it's very hard to see the details here. They don't look much better here, but they do look much better here. Now, in this region, there's not a lot of details. The global did manage to do a good job but not in this region. Global histogram equalization or global histogram matching is a compromise between everything that we see in the image. We can try to overcome, at least partially, that compromise by doing this local operation. And now we see that you can get details here, we can also get details here. So, in the different regions of the image, they have very different pixel values and we can still do a basically a histogram stretching and get much better contrast in the different regions. By this, we basically conclude our unit, our portion on histogram equalization and histogram matching, a very simple operation, and very useful. See you in the next video. Thank you.