 Welcome to this session in which we discuss a C plus plus program which performs histogram equalization. Last time we had discussed the concept of associative arrays and saw how it could be used to calculate a histogram value more efficiently. Today we will use the various formulae for histogram equalization which we had studied in earlier sessions. Remember our discussion based on this Wikipedia article. So, we will use those formulae and write a program to improve the image contrast. We remember that we are doing this in the context. The explanations are in the context of an artificial image which is 8 by 8 image with these kind of pixel values. The original pixel values were these. Using these we know that the histogram values will be of this kind. We had also seen the cumulative distribution function values and in fact the formulae was to be used for doing the contrast improvement. Here are the formulae from the Wikipedia article. The equalization formula permits us to calculate a new value for every pixel value v. So, v is an existing pixel value in the current image. There is a formula which maps this v into H of v given by rounded value of v-th element of cdf minus the cdf min divided by the size of the image m by n minus cdf min and the whole thing multiplied by l minus 1. The equalization formula for our example image where l is 256 and m and n are 8. The minimum cdf value is 1 reduces to this simple formulae. Those of you are interested in understanding how this formulae came about are most welcome to study many more details available in both open source material and other books on image processing. The point I want to make here is that people who have worked out these details now permit us to go ahead and implement this formula or such algorithms to easily build our programs which will perform the necessary image enhancement. It is useful, but not essential to understand how these formulae have come. We only note that once we calculate the histogram, once we calculate the cumulative distribution function, then we simply have to apply this formula to get a new value for any image pixel value that exists in the current image. This is the formula that we need to implement in our program. Let us now look at the program which enhances the contrast. Here are a series of comments written at the beginning. You will notice that I have used a new notation not slash slash which has to be put in front of every line, but a comment which starts with slash star and ends with star slash. The advantage is I can write as many lines as I want as a part of the comment and the whole thing becomes a readable text. This is the beginning of my program after the standard include statements and I include the CMath library because I will require some mathematical computations here. These are the variables that I have declared. Notice two arrays image 500 by 500 will store the original image, new image 500 by 500 will store the new image after changing the pixel values to get my contrast enhancement. These three arrays which I use to store histogram, to store the cumulative distribution function values and to store what I call the equalizer. This equalizer is nothing but the value which we obtain after applying the formula. So, if a pixel value is say 58, I will just go to the 58 entry into this equalizer array and that value will be the new pixel value for the new image. I now read the image data, first I collect the size. Notice here that in see out statement I have used another interesting string back slash t. This is a special character which represents a tab character. You will notice in the program listing given in the courseware that the output contains values separated neatly by a tab. I now read the entire image I just need a nested iteration i equal to 0 to m and i equal to 0 to m minus 1 and 0 to n minus 1. The original image is also produced as an output just for you to verify that your data is correct. Next, I initialize all my table array elements of histogram CDFN equalizer to 0 because in these arrays now I will be accumulating different values through computations. So, a single iteration for i equal to 0 to 255 will set histogram CDFN equalizer arrays to 0 value. Next I calculate the histogram table entry. You will remember the method that we decided to use exploiting the associative properties of the image pixel values and the array elements that I need to compute new values for. So, I set up an array iteration 0 to m minus 1 0 to n minus 1. I scan all the elements of the picture array and I simply use the i comma jth element of image as an index in the histogram array and increment it. So, notice the simplest computation in a single nested iteration will get me the entire histogram value. Once I do that, I proceed to compute the cumulative distribution function. The cumulative distribution function is essentially the sum of the successive histogram elements up to that point. So, I start with the 0th value being the same as histogram 0 and then I keep adding every element of histogram into the previous value of CDF to get the new element for CDFN. This completes the calculations of cumulative distribution function. Now, I find the minimum non-zero value in the CDF table. You remember that this is required in the formula for calculating the mapping of a old value to a new value. I start with a minimum value of 255 which is actually the maximum value. Scan the entire array of CDF and find out its minimum. Notice that I need to find out minimum for non-zero value and therefore, the condition here cannot simply be i th element of CDF is less than min. I must also logically end it with i th element not equal to 0. If this condition is satisfied by a pixel, I put that value as the new minimum. This will get me the correct minimum value computed. Having done this, I am now ready to calculate entries into the equalizer table. Remember the formula, rounded value of CDF i minus min divided by M into N minus min into 256 minus 1. Please note that I convert this value into float to ensure that I get a floating point number which then is rounded off and this rounded value is assigned to the equalizer. So, what does equalizer i contain now? If a pixel value in the original image is i, then the equalizer i will give me the new image value. So, I again use the associative property of the equalizer array itself to calculate values of my new image. I run a scan for 0 to M minus 1, 0 to N minus 1. I look at every original image value i comma j th element. Use that as an index in the equalizer array to calculate the i comma j th element of the new image. Notice again how beautifully simple the computations are. In a single scan of image array, I convert every element of the image array into a new image array. Finally, I produce an output. Notice that here again I am using a backslash t to separate out values by a tap character. Notice that after printing every row there is a end of line. So, I will get a decent print out. So, you will see that this program will calculate the enhancement of contrast operation and the new image that we so get. In summary, we now wrote a program to enhance the contrast of a black and white image. This image program enhancement program is available in the courseware on your site. Thank you.