 Welcome to this session in the course of computer programming. Today when I am recording this session, it is an important day for us. It is the Independence Day of India. I take this opportunity to offer my greetings and best wishes to all the participants of this course. In this session, we shall study the notion of an associative array to perform fast computations of an histogram in the context of doing image analysis and enhancement of image by improving the contrast. To quickly recapitulate what we have seen in the last session, we looked at the digital representation of images and we also saw how matrices can be used to represent images. In particular, we discussed the histogram and the cumulative distribution function and then we saw how histogram equalization technique can be used to improve the contrast. In this session, in the context of histogram calculations, we will look at what kind of computations are required to be performed. In particular, we will discuss an important concept called associative array. We will use this concept in our program which we will write for contrast enhancement. Once again, I request all to note that this histogram equalization technique is directly based on a Wikipedia article which is given here. You will remember that we have seen a large image, black and white image, seen how its histogram was limited to a very small portion of the total area and this was the cumulative distribution function. When we applied the histogram equalization technique and stretched the histogram, this is how the histogram looked. This is how the cumulative distribution function looked. More importantly, when we recalculated the values of individual pixels, we got an image which had a much better contrast than the original image. Of course, for the purposes of computations that we will discuss in this session, we will use the simpler picture. You will remember this was an artificial picture consisting of 8 by 8 pixels, that is 64 pixels. These were the original pixels and if we performed the contrast enhancement operation, this would be the new image that we will get. You will remember the pixel values for the image were shown to be like this. We had noticed that the smallest value was 52 and largest value was 154, indicating that the entire image is concentrated in a small range of pixels. We had also seen the histogram values which result from this image were not shown in 0 pixels. I mean pixels which did not exist at some pixel values like 0, 1, 2, 3, 4, but this was the list at which at pixel value 52, there was 1 pixel, 55 there were 3, 62 there was 1, 68 there were 5, 69 there were 3 and so on. So, this is nothing but the count how many times a particular pixel value occurs in the image. Now, in order to perform these histogram calculations, what we need to do is find out how many pixels exist in the image which have the same value. The best way to represent such a count is to actually accumulate these counts in various array elements of a single array. Since there are possibly 256 values, the best way to represent the histogram is to use a one-dimensional array of 256 elements because a pixel can only be between 0 to 255 for a gray scale of black and white image. All that we need to do is to accumulate the count of each pixel value found in the image in the corresponding element of this histogram array. So, let us look at the possible procedure. Here I have shown the pixel values in the image and here I have shown the histogram array. This would be the index 0, 1, 2, 3, 4 up to 255. We will start with an initial value of 0 for all these elements. The calculation that we will perform could be as follows. I will start scanning the image array from here. Let us say I am looking for an element 25. I scan the entire array. I do not find any element to 25 here. So, the count for 25 will remain 0. Consider an element 68 which I am looking for. I look for pixels with value 68. The procedure will be, I will again scan this array and as I get my first 68, the count will be increased by 1. When I continue my scanning, I locate the second 68. I increase the count to 2. I locate the third 68. I will increase the count to 3. I locate the fourth one. I increase it to 4 and I locate the fifth one. I increase it to 5. After completing one full scan of the image pixels, I will have the total count for the pixel which has the value 68 as 5 here. I could do the same thing for the pixel value 69. The count count will be 3 because there are exactly 3 pixel values here. This apparently works properly. If I repeat this process for each pixel value from 0 to 255, I will have the entire count correctly calculated. However, one should be concerned about how much time it is going to take, how many computing operations that we will have to perform. If we use this logic, let us consider the number of operations that the computer has to perform. Assume that we have an array called I am 8 by 8 for an image and h 256 for the history. Now for each pixel value p, we scan all 64 pixel values in I am and compare each with p. We assign the total count of those pixel values which match to the appropriate element of h. This is exactly what we saw happening in the previous slide. We have to repeat this for each of the 256 possible values. Now if we have a very large image of the size 500 by 300, there will be 150,000 pixels or as some people refer to it 150,000 pixels and I will have to repeatedly scan these 150,000 pixels 256 times. There is a lot of computation. Is there any other way? Yes, there is. But to look at that way, let us look at another example which has nothing to do with pictures. Consider that we have a list of students who have taken a MOOC course and they belong to different cities. So, somebody has prepared a list. I have a roll number and the city to which the student belongs to. Then I have another roll number, which the student belongs to. So, we have this list. There could be thousands of students taking the MOOC course. Imagine now, I just want to know how many students are taking this MOOC course from the city of Mumbai, from city of Delhi, from city of Tokyo, etcetera, etcetera. One simple way of doing this is suppose I want to find out the number of students studying from Delhi, I will look at this list, scan this list. I find Delhi, I count one, I find another Delhi, I count two, I find third Delhi, I count three, fourth Delhi, I count four and keep on doing this for the entire list. At the end, I will get the count of students who are studying this course from Delhi. I can do the same thing for each city and I will then get this kind of count. Delhi, 4024, Dhaka, 1729, Dubai, so many Karachi, so many Kathmandu, etcetera, etcetera. Observe that this city statistics that I am counting has an uncanny resemblance to the histogram that I was counting, but we will come to that in a moment. Now, if we use our approach to do such counting, which we used in histogram counting, we take a city, scan the complete list and put the count value against that city in our count table and we repeat this for each city. Now, look at the operations involved. If there are 50,000 students taking a MOOC course and if they belong to 200 different cities, we need to look at each of the 50,000 entries 200 times and that is 1 crore comparison or as some people would refer to this number, 10 million comparisons. This is clearly a very time consuming exercise. In fact, this is not how statisticians do their counting, but we will not consider that. We will consider a simpler way of doing these computations and this is where we look at the notion of an associative average. It will be useful if the name of a city, which is a value in our list, itself can be used as a key or an index to directly access the associated count, which itself happens to be a value in our table for city and the count. An associative array is in fact a set of key value pairs. The organization is such that key is the index of the array. Assume that we have such an associative array for our example, then if the key Mumbai is given using that as an index of the array, we should directly get 5,102. In fact, what it means is we should be directly able to access the element by simply giving the key. If the key is Kathmandu, we should directly access the element which contains value 431 and we will like to use the value of the name of the city in our original list of students directly as a key for this table to access the count in the city. So, what it means is that something which is a value in one list should be usable as an index or a key in another list or another table. Fortunately, we have pixel values that we are dealing with because we note that in C++ the arrays can be indexed only by integer numbers not by strings and these have to be in the range of 0 to n minus 1 if there are n elements in the array. For the histogram array, the key ranges 0 to 255. We observe that pixel values in the image array are also integer numbers and are precisely in this range. Therefore, the pixel value itself can be used as a key or index of the histogram array and this array then can be treated as an associative array. Look at how it simplifies the computation. Let us use this concept of associative array to find our histogram values. Let us imagine that the image array is stored in im as before and the histogram itself is stored in another array h256. The counting process now will proceed as follows. We will scan the entire image matrix for each pixel value p that we look at. We know which element of the array needs to be updated. The count will have to be updated precisely for the pth element. That is because there is an association between the pixel value p and the index of array h where the count needs to be updated. So, now the count of let us say one image array element im i comma j which has a value p can be incremented by simply the following assignments. p is equal to im i comma j and h of p is equal to h of p plus 1. Observe that the pixel value in the image array is acting as the index of the h array. Or more simply, we can simply use im ij as the index of h and update it by one. Please note that multiple scans of the image array are completely avoided. In a single scan, I can update all the histogram counts. This indeed is the advantage of using such an associative array. In summary, we have seen that in the context of contrast enhancement of digital images, we need to compute the histograms and we discuss the concept of an associative array and found it to be a very useful way to calculate histogram without doing too many computations. In the next session, we will actually write a C plus plus program to implement this technique. Thank you.