 Welcome to this session in the course on computer programming. Today, we are going to discuss some very interesting applications of using matrices to represent digital images. We will discuss the concept of an histogram and we will see how digital images can be easily processed using matrices. First technique recap, we have seen several computational problems involving matrices. The main challenges that we have noted are one, how to represent the problem data by matrices. Remember that in case of simultaneous equations, we use matrices to represent the coefficients of the unknown variables. Further, we need to be very careful about manipulation of index values for rows and columns of matrices. We would have noticed some interesting complications when analyzing the magic square for being a true magic square. There are umpteen such occasions. In this session, we will see how images are stored and processed digitally. We are familiar with the photographs, but these days you use digital images and digital photographs. In particular, we will study the basic image representation using a matrix and we will also see how digital images can be processed by simple matrix operations to enhance the contrast of the given image. Please note that the histogram equalization technique which we will be describing here and the digital images used in this session are directly based on a Wikipedia article given by this link. Those of you are interested and can look at more details at this link. So, we now look at digital images. These are actually a collection of pixel values. The pixel is a picture element and the value of a picture element represents the light intensity. Now, these pixel elements are arranged in a matrix form with certain height and width. So, there are h into w pixels in an image. Each pixel value can be represented either by one bit which means it is a monochrome image, 0 or 1 value that is either black or white. It could be represented by 8 bits which is called a gray scale image. 0 would mean black and 255 will mean white. So, you get all shades of black and white. A color image can be represented by 24 bits where you use 8 bits to represent red, other 8 bits to represent blue and 8 more bits to represent green. For each one byte, you actually can represent a pixel. So, one pixel gets represented by 3 bytes or 24 bits. Please note that one can have 16 million colors if you look at all the possible combinations that can be represented. Of course, the capacity of a human eye is limited to a small range. We can actually detect somewhere between 200 to 2000 colors. Now, the information about an image is actually stored in a file because there is a large number of pixels. You cannot put them down on a piece of paper or input them or output them ordinarily. Such a file will contain some spatial header information. For example, that file will tell us what is the height, what is the width, what are the types of color present, etcetera, etcetera. And each file will have values for each pixel in the image for every color present depending upon whether it is a color, monochrome or grayscale image. Fingerprint images typically are monochrome or grayscale images and have small size 500 to 300 bytes. But for large images, we need to compress them. There are interesting compression techniques which keep the file size limited. Otherwise, remember that a 12 megapixel camera, that means in single frame it can capture 12 megapixel. Since each pixel can be 3 bytes in color, a single picture can be 36 megabytes size. In order to reduce this size, but without losing the quality, there are compression techniques that have been discovered. The compression can be either lossy or lossless. We will not go into those details except that we note that there are several file formats which have evolved to represent images of different types. So, we have raw images PNG, BMP, TIFF, JEPH, JPEG, XMP. You can refer to the Wikipedia article on the different file formats under the title image file formats. Many of you would have seen these files because invariably on our computers, we have photographs in one of these file formats. We will concentrate on looking at the pixel values of a digital image, assuming that these pixel values can be read inside a matrix. The matrix elements can then be processed further. Each element of a matrix for a grayscale image, for example, would contain a value between 0 and 255, where we said 0 is black, 255 is white. It does not require more than one short end or one care, which is one byte to store a pixel. We now assume that we can read all the pixel values into a matrix and see how they can be processed. First, we introduce the term histogram. This is another statistical term which is used to denote the frequencies or count of number of times an event or incidence occurs. In case of digital images, a histogram table simply indicates how many times a particular value occurs in the image pixel. So, for each possible value, the number of pixels in the image having that value is the entry into the histogram table. Why is histogram important? Let us look at an example. Here is a sample image of size 8 pixels by 8 pixels. It is obviously an artificial image. A pixel is shown as a very big thing, a square thing. Notice the different shades of grey. This one is the brightest, but this is not pure white. This one is the darkest, but this is not pure black. So, this is an image which has a very low contrast. Let us look at the pixel values of this image. Observe that these are the pixel values stored in an 8 by 8 matrix. Observe that the smallest pixel value is actually 52 and not 0. The largest pixel value is 154 and not 255. This is one of the reasons why the contrast of this image is poor. So, this is how the image looks like. I have simply shown the value of the brightest e-pixel here as 154. Let us look at the histogram values which are shown for non-zero pixels. Of course, at 0, 1, 2, 3, 4, up to 51, there are no pixels. There is one pixel with value 52, three pixels with value 55, two pixels with value 58 and so on. Observe that there is one pixel with value 154, but there is no pixel with value 155, 156 and so on. We notice that all values are between 52 and 154 and this results in inadequate contrast. Imagine if this contrast was to be removed, then what we must do is somehow the histogram which is concentrated in a narrow range between 52 and 154 needs to be stretched because the possible pixel values are 0 to 255. So, if we can stretch the histogram such that it covers all possible values, then we might get a picture with a better contrast. This process is called histogram equalization. What we need to do is given the pixel values in an image, we calculate the histogram, we find out some method of mapping an existing pixel value to some new value. A value v should be mapped by a suitable function h of v. So, original value v should become h of v. For example, we would like 52 to be mapped to 0, 154 to be mapped to 255. Of course, we cannot arbitrarily move values from one end to another. All the values which are so transformed must be transformed in proper harmonious relationship with the entire image and that is the reason why we use histogram. First, we calculate the cumulative distribution function or CDF of a histogram. The CDF is simply defined as a number of pixels below a certain value. So, for example, there is only one pixel below 52. There were three pixels at 55. So, below 55, 3 plus 1, 4 pixels are there. Below 58, there are 6 pixels. Below 59, there are 9 pixels and so on. So, this will tell you the cumulative distribution function. Observe that the largest value is 154. So, below 154, there are 64 pixels. Of course, the entry into a cumulative distribution function array 155, 156, etcetera will all have the number 64 because there are 64 pixels less than the next value. Here is the histogram equalization formula. Looks complex, is not it? H of v, if v is a value of an existing pixel, then H of v is given by rounded value of CDF v minus CDF min divided by m into n minus CDF min multiplied by l minus 1. Where from has this formula come? Well, we do not know. Frankly, it does not matter. Those of us who are interested in image processing can figure this out. The beauty of computer programming is if somebody has discovered such formulation, such algorithm, we can actually implement those formally into computational processes, which is what we propose to do. The equalization formula for our example image, where l is 256, m and n are 8 and the minimum CDF is 1 is simply H v is equal to round of this upon 63 into 254. That means for every value v, we can find out what the new value of H will be. Let us look at this example. The CDF of 78 is 46. You can go back and check the cumulative distribution function at 78 was 46. So, a pixel value 78 will have to be equalized. That means it will have to be mapped to a new value using our formula. This formula is H of 78 is equal to round of 46 minus 1 by 63 into 255, which is nothing but round of 0.714286 into 255, which is nothing but 182. So, look how interesting the life is. We do not know how the formula has come by, but we can implement it into an equation and calculate new value. This gives us a equalized value. Each pixel in the image will have to be transformed in this fashion. Each pixel which has a value 78 will now have a value 182 and we can calculate such new value for each pixel in the image. If we do that, we will get the new pixel values after what we call histogram equalization. Notice that in these new pixel values, the smallest value is actually 0. The largest value is 255, which was 154 earlier. All other values are changed, but they are not arbitrarily changed. They are changed harmoniously in relation with the histogram properties of the image. To see how effective this technique is, let us look at the contrast enhancement that we get. Do not you see that this image look much better in contrast? Let us compare these two images. This was the original image. This is the new image. Observe that we have simply implemented a computational procedure by representing the image in a matrix and by doing the histogram equalization. Here is another interesting example. Here is a scene, another grayscale picture. Here also the contrast is not very good. If we actually plot the histogram and the cumulative distribution function, it will look like that. The red color thing is the histogram. Notice that all pixel values are concentrated in a small range somewhere between 100 and something to 200 and this is the cumulative distribution function. If we apply histogram equalization, the cumulative distribution function will become like this and the histogram will be stretched all the way from 0 to 250. When we transform individual pixels as per this equalization formula, the picture that we will get will be this. So, notice this was the original picture with this histogram and cumulative distribution function. We did the histogram equalization using that formula and mapped all the new pixels. The new pixels will now constitute an image which has a much greater control. In short, we have seen how histogram equalization can be done, how digital images can be represented in the next session. We will actually write a program to implement this technique. I would like to tell you that this programming is not very complex at all. In fact, in many embedded systems such as your digital cameras, such histogram equalization programs are written by people. They are embedded into the computers inside your camera and they can be invoked by press of a button. We of course in this course shall study how such programs can be written in C plus plus and executed. Thank you.