intro4.pptx
Document Details

Uploaded by OpulentCopper
Full Transcript
Histograms Introduction Before discussing the use of Histograms in image processing, we will first look at what histogram is, and how it is used. Histograms A histogram is a graph. A graph that shows the frequency of anything. Usually, histograms have bars that represent the frequency of occurrence...
Histograms Introduction Before discussing the use of Histograms in image processing, we will first look at what histogram is, and how it is used. Histograms A histogram is a graph. A graph that shows the frequency of anything. Usually, histograms have bars that represent the frequency of occurrence of A histogram has two data in the whole data set. axes the x-axis and the y-axis. The x-axis contains events whose frequency you have to count. The y-axis contains frequency. The different heights of the bar show different Example Consider a class of programming students and you are teaching Python to them. At the end of the semester, you got this result that is shown in the table. But it is very messy and does not show your overall result in class. So, you make a histogram of your result, showing the overall frequency of occurrence of grades in your class. A histogram of an image, like other histograms also shows frequency. However, an image histogram shows the frequency of pixel intensity values. In an image histogram, the x-axis shows the gray level intensities and the y-axis shows the frequency of these intensities. The histogram of the picture of Einstein would be something like this. The x-axis of the histogram shows the range of pixel values. Since it’s an 8-bpp image, that means it has 256 levels of gray or shades of gray in it. That’s why the range of the x-axis starts from 0 and ends at 255 with a gap of 50. Whereas on the y axis, is the count of these intensities. As you can see from the graph, most of the bars that have high frequency lie in the first half portion which is the darker portion. That means that the image is darker. Applications of Histograms Histograms have many uses in image processing. The first use as it has also been discussed above is the analysis of the image. We can predict about an image by just looking at its histogram. It is like looking at an x-ray of a bone of a body. The second use of histogram is for brightness purposes. The histograms have wide applications in image brightness. Not only in brightness but histograms are also used in adjusting the contrast of an image. Another important use of histogram is to equalize an image. The histogram has also a wide use in thresholding. Brightness and Contrast Brightness Brightness is a relative term. It depends on your visual perception. Since brightness is a relative term, brightness can be defined as the amount of energy output by a source of light relative to the source we are comparing it to. In some cases, we can easily say that the image is bright, and in some cases, it’s not easy to perceive. For example Just have a look at both of these images and compare which one is brighter. We can easily see, that the image on the right side is brighter as compared to the image on the left. But if the image on the right is darker than the first one, then we can say that the image on the left is brighter than the right. How to make an image brighter. Brightness can be simply increased or decreased by simple addition or subtraction, to the image matrix. Consider this black image of 5 rows and 5 Since we already columns know, that each image has a matrix at it is behind it that contains the pixel values. Since the whole matrix is filled with zero, the image is very much darker. Now we would compare it with another black image to see if this image got brighter or not. Still, both the images are the same, performing some operations on image 1, due to which it becomes brighter than the second one. Simply add a value of 5 to each of the matrix values of image 1. After adding image 1 would be something like this. Now we will again compare it with image 2 and see any difference. We see, that still, we cannot tell which image is brighter as both images look the same. Now what we will do, is we will add 50 to each of the matrix values of image 1 and see what the image has become. The output is given below. Now again, we will compare it with image 2 You can see that Image 1 is slightly brighter than Image 2. We go on and add another 45 values to the matrix of image 1, and this time we compare again both images. Here, Image 1 is brighter than Image 2. Even it is brighter than the old image. At this point the matrix of image1 contains 100 at each index as first add 5, then 50, then 45. So, 5 + 50 + 45 = 100. Contrast Contrast can be simply explained as the difference between maximum and minimum pixel intensity in an image. For example. Consider the final image1 in brightness. The matrix of this image is: The maximum and minimum values in this matrix are 100. Contrast = maximum pixel intensity - minimum pixel intensity = 100 (subtracted by) 100 = 0 Histogram Sliding Histogram sliding In histogram sliding, we just simply shift a complete histogram rightwards or leftwards. Due to the shifting or sliding of the histogram towards the right or left, a clear change can be seen in the image. In these slides, we are going to use histogram sliding for manipulating brightness. The term i.e.: Brightness has been discussed in the introduction to brightness and contrast. But we are going to briefly define it here. Brightness Brightness is a relative term. Brightness can be defined as the intensity of light emitted by a particular light source. Contrast Contrast can be defined as the difference between maximum and minimum pixel intensity in an image. Sliding Histograms Increasing brightness using histogram sliding A histogram of this image is shown. On the y-axis of this histogram is the frequency or count. On the x-axis, we have gray level values. Those gray level intensities whose count is more than 700, lie in the first half portion, which means towards the blacker portion. That’s why we got an image that is a bit darker. To brighten it, we will slide its histogram towards the right, or the whiter portion. To do we need to add at least a value of 50 to this image. As we can see from the histogram above, this image also has 0-pixel intensities, which are pure black. So, if we add 0 to 50, we will shift all the values at 0 intensity to 50 intensity and all the rest of the values will be shifted accordingly. Here is what we got after adding 50 to each pixel intensity. Let’s compar e these two images and their histogr ams to see what change s have to be made. Conclusion As we can see from the new histogram all the pixel values have been shifted towards the right and its effect can be seen in the new Decreasing brightness using histogram sliding Now if we were to decrease the brightness of this new image to such an extent that the old image looks brighter, we subtract some value from all the matrix of the new image. The value which we are going to subtract is 80. Because we already added 50 to the original image and we got a new brighter image, now if we want to make it darker, we have to subtract at least more than 50 from it. And this is what we got after subtracting 80 from the new image. Conclusion It is clear from the histogram of the new image, that all the pixel values have been shifted towards the right and thus, it can be validated from the image that the new image is darker and now the original image looks brighter as compared to Histogram stretching One of the other advantages of histograms is the introduction of contrast enhancement. There are two methods of enhancing contrast. The first one is called histogram stretching which increases contrast. The second one is called Histogram equalization which enhances contrast, and it has been discussed in our slides of histogram equalization. Before we will discuss the histogram stretching to increase contrast, we will briefly define contrast. Consider this image. The histogram of this image is shown belo Now we calculate contrast from this image. Contrast = 225. Increasing the contrast of the image The formula for stretching the histogram of the image to increase the contrast is The formula requires finding the minimum and maximum pixel intensity multiplied by levels of gray. In our case the image is 8bpp, so levels of gray are 256 1. The minimum value is 0 and the maximum value is 225. So, the formula in our case is where f(x, y) denotes the value of each pixel intensity. For each f(x, y) in an image, we will calculate this formula. After doing this, we will be able to enhance our contrast. The stretched histogram of this image is shown below. Note the shape and symmetry of the histogram. The histogram is now stretched or by other means expanded. Have a look at it. In this case, the contrast of the image can be calculated as Contrast = 240 Hence, we can say that the contrast of the image is increased. Note: this method of increasing contrast does not work always, but it fails in some cases. Failing histogram stretching As we have discussed, the algorithm fails in some cases. Those cases include images when there is pixel intensities 0 and 255 are present in the image. When pixel intensities 0 and 255 are present in an image, then in that case they become the minimum and maximum pixel intensity which ruins the formula like this. Original Formula Putting fail case values in the formula: Simplify that expression gives That means the output image is equal to the processed image. That means there is no effect of histogram stretching has been done in this Introduction to Probability PMF (Probability Mass Function) and CDF (Cumulative Density Function) are both terms that belong to probability and statistics. Now the question that should arise in your mind, is why are we studying probability? This is because these two concepts of PMF and CDF are going to be used in the next slides of Histogram equalization. What is PMF? PMF stands for probability mass function. As its name suggests, it gives the probability of each number in the data set or you can say that it gives the count or frequency of each element. How PMF is calculated We will calculate PMF in two different ways. First from a matrix of an image. Then we will take another example in which we will calculate PMF from the histogram. Consider this matrix. At first, we will take the first value in the matrix, and then we will count, how many times this value appears in the whole matrix. After counting, they can either be represented in a histogram or a table PMF Note that the sum of the count must be equal to the total number of values. Calculating PMF from histogram The histogram shows the frequency of gray level values for an 8 bits per pixel image. Now if we have to calculate its PMF, we will simply look at the count of each bar from the vertical axis and then divide it by the total count. So, the PMF of the histogram is this. Another important thing to note in the above histogram is that it is not monotonically increasing. So, to increase it monotonically, we will calculate its CDF. What is CDF? CDF stands for cumulative distributive function. It is a function that calculates the cumulative sum of all the values that are calculated by PMF. It sums up the previous one. How it is calculated? We will calculate CDF using a histogram. Here is how it is done. Consider the histogram shown earlier which shows PMF. Since this histogram is not increasing monotonically, so will make it grow monotonically. We will simply keep the first value as it is, and then for the 2nd value, we will add the first Here is the CDF of the previous PMF function. Now as you can see from the graph above, the first value of PMF remains as it is. The second value of PMF is added to the first value and placed over 128. The third value of PMF is added to the second value of CDF, which gives 110/110 which is equal to 1. And also now, the function is growing monotonically which is a necessary condition for PMF and CDF usage in histogram equalization Histogram equalization Histogram equalization is discussed in the next slides, but a brief introduction to histogram equalization is given below. Histogram equalization is used to enhance the contrast of the images. In the histogram equalization, the first and the second step are PMF and CDF. Since in histogram equalization, we have to equalize all the pixel values of an image. So PMF helps us calculate the probability of each pixel value in an image. And CDF gives us the cumulative sum of these values. Further on, this CDF is multiplied by levels, to find the new pixel intensities, which are Histogram Equalization We have already seen that contrast can be increased using histogram stretching. In these slides, we will see how histogram equalization can be used to enhance contrast. Before performing histogram equalization, you must know two important concepts used in equalizing histograms. These two concepts are known as PMF and CDF. Histogram Equalization Histogram equalization is used to enhance contrast. Contrast doesn’t need to always be increased in this. There may be some cases where histogram equalization can be worse. In those cases, the contrast is decreased. Histogram of this image The histogram of this image is shown below. Now we will perform histogram equalization to it. PMF First, we have to calculate the PMF (probability mass function) of all the pixels in this image. CDF Our next step involves the calculation of CDF (cumulative distributive function). Calculate CDF according to gray levels Let’s, for instance, consider, that the CDF calculated in the Then in this step, you will multiply the CDF value with (Gray levels (minus) 1). Considering we have a 3 bpp image. The number of levels we have are 8. And 1 subtracts 8 is 7. So, we multiply CDF by 7. Here is what we got after multiplying. Now we have the last step, in which we have to map the new gray level values into several pixels. Let’s assume our old gray level values have this number of pixels. Now if we map our new values, then this is what we got. Now map these new values you are onto the histogram, and you are done. Let’s apply this technique to our original image. After applying we got the following image and its following histogram. Histogram Equalization Image Cumulative Distributive function of this image Histogram Equalization histogram Comparing both the histograms and images Conclusion As you can see from the images the new image contrast has been enhanced and its histogram has also been equalized. There is also one important thing to note here during histogram equalization the overall shape of the histogram changes, whereas in histogram stretching the overall shape of the histogram remains the same.