Introduction to Image Processing and Python: Brightness and Contrast Brightness and Contrast Steven L. Tanimoto Intro. to Image Proc. & Python © S. Tanimoto Saturday, August 24, 13 Brightness and Contrast 1 Outline Brightness Contrast Pixel Arithmetic and Truncation Histograms Thresholding Intro. to Image Proc. & Python © S. Tanimoto Saturday, August 24, 13 Brightness and Contrast 2 Brightness Brightness is the relative light intensity of some part of the image, typically of a single pixel. Some people make a distinction between actual physical light intensity (as measured by a light meter) and the perception of intensity, which can be more subjective. Whereas light intensity may be measured in physical units such as “foot candles”, lumens, etc., brightness in a digital image is simply in “brightness units.” The actual light intensity of a pixel of a digital image depends on factors such as what kind of computer monitor is displaying the image, whether the intensity control is turned all the way up, or in the case of a printout, how much illumination there is on the picture. Intro. to Image Proc. & Python © S. Tanimoto Saturday, August 24, 13 Brightness and Contrast 3 Brightness (continued) Brightness for a color pixel may be considered to be the average value of the color components: (R + G + B)/3. Thus brightness in PixelMath ranges from 0 to 255, just as the color components do. Intro. to Image Proc. & Python © S. Tanimoto Saturday, August 24, 13 Brightness and Contrast 4 Adjusting Brightness Here are two simple ways to adjust the brightness of an image: 1. Add a number to every pixel value in the image. P is replaced by P + d, where d represents the difference. If d > 0, then the image will generally get brighter. (We say “generally,” because if the image is already as bright as possible, then it can’t get any brighter.) If d < 0, then the image will generally get darker. 2. Multiply every pixel value by a factor. P is replaced by P * f, where f is the factor. If f > 1, then the image will generally get brighter. If f < 1, then the image will generally get darker. Intro. to Image Proc. & Python © S. Tanimoto Saturday, August 24, 13 Brightness and Contrast 5 Adjusting Brightness (cont.) Multiplying also generally changes the contrast of the image. If f > 1, then the pixel values typically end up more widely spread (but not always, due to the truncation property of “Pixel Arithmetic”). If f < 1, then the pixel values end up less widely spread. Intro. to Image Proc. & Python © S. Tanimoto Saturday, August 24, 13 Brightness and Contrast 6 Pixel Arithmetic The PixelMath system computes values with great accuracy, using “double precision” computer representations of each number. However, when the numbers are ready to be put into the destination image’s pixels, they may be changed. First, they are “truncated.” This means that any negative number is changed to 0.0. Also, any number greater than 255.0 is changed to 255.0. Next, they are converted to integers. This means that a number such as 37.886 gets changed to 37. Any number that is equal to an integer (201.00) is not changed in value but is changed in form to an integer (201). The name “floor” is given to the function that gives back the integer, but we don’t have to ask for it; it’s done automatically in PixelMath. Intro. to Image Proc. & Python © S. Tanimoto Saturday, August 24, 13 Brightness and Contrast 7 Pixel Arithmetic (continued) In the PixelMath system, what will be the result (in the destination image), for the following operations? 1+1 255 + 1 1.5 + 1.6 100 * 3.5 9 / 10 100.0 * - 2.5 Intro. to Image Proc. & Python © S. Tanimoto Saturday, August 24, 13 Brightness and Contrast 8 Coordinating Brightness and Contrast To control both brightness and contrast it is typical to multiply and add at the same time. Replace p by (f * p) + d. For example, with f = 2.0 and d = -100, we have pnew = (2.0 * pold) - 100 This this would theoretically be a linear function, (and its graph would be a line, with slope f, and intercept d), but PixelMath pixels cannot be less than 0 or greater than 255, so the actual graph is a piecewise linear function… Intro. to Image Proc. & Python © S. Tanimoto Saturday, August 24, 13 Brightness and Contrast 9 Coordinating Brightness and Contrast (cont.) pnew = (2.0 * pold) - 100 255 pnew 0 0 Intro. to Image Proc. & Python © S. Tanimoto Saturday, August 24, 13 pold 255 Brightness and Contrast 10 Histograms A histogram is a graph showing the frequency of occurrence of each of a set of values. Here is a non-image-processing example: Musical note sequence*: Histogram: A B “EEFGGFEDCCDEEDD” X X X X X X X X X X X X X X X C D E F G * Theme from Beethoven’s 9th symphony. Intro. to Image Proc. & Python © S. Tanimoto Saturday, August 24, 13 Brightness and Contrast 11 Histogram “Bins” Each column of a histogram is called a bin. A B X X X X X X X X X X X X X X X C D E F G Intro. to Image Proc. & Python © S. Tanimoto Saturday, August 24, 13 Brightness and Contrast 12 Pixel Value Histograms A pixel value histogram is a graph showing the frequency of occurrence of each of the possible pixel values in an image. Intro. to Image Proc. & Python © S. Tanimoto Saturday, August 24, 13 Brightness and Contrast 13 Histogram Equalization To “equalize” a histogram, we change the original item values in such a way that the new histogram will have a more even distribution of values. (In the following example all Cs will become As, and some other notes will be changed, too.) For this to work, there must be some spread already – if all the values are in one bin, they will stay in one bin. Original histogram: A B X X C X X X X D X X X X X E Let’s use numbers instead of X’s X X F X X G Intro. to Image Proc. & Python © S. Tanimoto Saturday, August 24, 13 0 A 0 B 2 C 4 D 5 E 2 F 2 G Brightness and Contrast 14 Histogram Equalization (cont.) Ideal cumulative histogram: Original histogram: 0 A 0 B 2 C 4 D 5 E 2 F 2 G Cumulative histogram: 0 0 2 6 11 13 15 A B C D E F G There should be about 2 items per bin, on average. Intro. to Image Proc. & Python © S. Tanimoto Saturday, August 24, 13 2 A 4 B 6 C 8 10 12 15 D E F G Equalized histogram: 2 A 0 B 4 C 0 D 5 E 2 F 2 G Cumulative equalized histogram: 2 2 6 6 11 13 15 A B C D E F G Brightness and Contrast 15 Purpose of Equalization The main reason to perform histogram equalization is to better spread out the pixels values within the available range (0 to 255), so that the differences in brightness among the pixels can be more readily seen, or so that automatic algorithms can take advantage of greater differences among the pixel values. Equalization can be used for contrast enhancement. It tends to accentuate differences in brightness without throwing away any of those differences. As we’ll see, thresholding does a great job of increasing contrast, but at a cost: although some differences are amplified, many of the differences among pixel values are lost. Intro. to Image Proc. & Python © S. Tanimoto Saturday, August 24, 13 Brightness and Contrast 16 Making a (Gray) Monochrome PixelMath formula: (Red1(x,y)+Green1(x,y)+Blue1(x,y))/3 This is an average of the R, G, and B components. In the monochrome image, each of the color components will be given this average value. After doing this, the red, green, and blue values within a pixel will be equal. Then the histograms for red, green and blue will be identical, and any one of them can serve to represent the others. Intro. to Image Proc. & Python © S. Tanimoto Saturday, August 24, 13 Brightness and Contrast 17 “Equalized Mono Mona” Intro. to Image Proc. & Python © S. Tanimoto Saturday, August 24, 13 Brightness and Contrast 18 “Equalized Histogram for Mona” Intro. to Image Proc. & Python © S. Tanimoto Saturday, August 24, 13 Brightness and Contrast 19 Thresholding A “threshold” is a value used to separate “low values” from “high values”. What would be a good threshold? Intro. to Image Proc. & Python © S. Tanimoto Saturday, August 24, 13 Brightness and Contrast 20 Thresholding A “threshold” is a value used to separate “low values” from “high values”. Often a “valley” leads to a good threshold. p Valley at p = 113 Intro. to Image Proc. & Python © S. Tanimoto Saturday, August 24, 13 Brightness and Contrast 21 Thresholding: PixelMath Formula Condition then value else value if Source1(x,y) < 113 then 0 else 255 Source image pixel comparison Intro. to Image Proc. & Python © S. Tanimoto Saturday, August 24, 13 threshold black Brightness and Contrast white 22 Thresholding Resulting image has a histogram with only two non-empty bins (0 and 255). # black pixels (p=0) is now 81384 # white pixels (p = 255) is now 96699 Total number of pixels is (always) 178083. Intro. to Image Proc. & Python © S. Tanimoto Saturday, August 24, 13 Brightness and Contrast 23 Thresholding (Result) Intro. to Image Proc. & Python © S. Tanimoto Saturday, August 24, 13 Brightness and Contrast 24 Comparing Thresholds Threshold = 175 Threshold = 113 Intro. to Image Proc. & Python © S. Tanimoto Saturday, August 24, 13 Threshold = 90 Brightness and Contrast 25