The document discusses various image enhancement techniques including gamma correction, log transformation, and histogram equalization. It provides Python code examples for each method, illustrating how to improve image contrast and visibility of details. Additionally, it explains the mathematical principles behind these transformations and their applications in image processing.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
1 views
02_chapter 5 Image Enhancement Part2
The document discusses various image enhancement techniques including gamma correction, log transformation, and histogram equalization. It provides Python code examples for each method, illustrating how to improve image contrast and visibility of details. Additionally, it explains the mathematical principles behind these transformations and their applications in image processing.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6
Image Enhancement 101
import cv2 import matplotlib.pyplot as plt import numpy as np
# Opening the image.
a = cv2.imread('../Figures/angiogram1.png') # gamma is initialized. gamma = 0.5 # b is converted to type float. b1 = a.astype(float) # Maximum value in b1 is determined. b3 = np.max(b1) # b1 is normalized b2 = b1/b3 # gamma-correction exponent is computed. b4 = np.log(b2)*gamma # gamma-correction is performed. c = np.exp(b4)*255.0 # c is converted to type int. c1 = c.astype(int) # Displaying c1 plt.imshow(c1)
Figure 5.5(a) is an image of the angiogram of blood vessels. The
image is too bright and it is quite difficult to distinguish the blood vessels from background. Figure 5.5(b) is the image after gamma cor- rection with γ = 0.5; the image is brighter compared to the original image. Figure 5.5(c) is the image after gamma correction with γ = 5; this image is darker and the blood vessels are visible. 102 Image Processing and Acquisition using Python
(a) Input image. (b) Gamma corrected image with
γ = 0.5.
(c) Gamma-corrected image with
γ = 5.
FIGURE 5.5: An example of power law transformation.
5.5 Log Transformation
Log transformation is used to enhance pixel intensities that are otherwise missed due to a wide range of intensity values or lost at the expense of high-intensity values. If the intensities in the image range from [0, L − 1] then the log transformation at (i, j) is given by
t(i, j) = k log(1 + I(i, j)) (5.7)
Image Enhancement 103 L−1 where k = and Imax is the maximum magnitude value log(1 + |Imax |) and I(i, j) is the intensity value of the pixel in the input image at (i, j). If both I(i, j) and Imax are equal to L − 1, then t(i, j) = L − 1. When I(i, j) = 0, since log(1) = 0 will give t(i, j) = 0. While the end points of the range get mapped to themselves, other input values will be transformed by the above equation. The log can be of any base; however, the common log (log base 10) or natural log (log base e) are widely used. The inverse of the above log transformation when the x base is e is given by t−1 (x) = e k − 1, which does the opposite of the log transformation. Similar to the power law transformation with γ < 1, the log trans- formation also maps a small range of dark or low-intensity pixel val- ues in the input image to a wide range of intensities in the output image, while a wide range of bright or high-intensity pixel values in the input image get mapped to narrow range of high intensities in the output image. Considering the intensity range is between [0, 1], Figure 5.6 illustrates the log and inverse log transformations. The Python code for log transformation is given below.
import cv2 import numpy, math
# Opening the image.
a = cv2.imread('../Figures/bse.png') # a is converted to type float. b1 = a.astype(float) # Maximum value in b1 is determined. b2 = numpy.max(b1) # Performing the log transformation. c = (255.0*numpy.log(1+b1))/numpy.log(1+b2) # c is converted to type int. c1 = c.astype(int) # Saving c1 as logtransform_output.png. cv2.imwrite('../Figures/logtransform_output.png', c1) 104 Image Processing and Acquisition using Python
FIGURE 5.6: Graph of log and inverse log transformations.
Figure 5.7(a) is a backscattered electron microscope image. Notice
that the image is very dark and the details are not clearly visible. Log transformation is performed to improve the contrast, to obtain the output image shown in Figure 5.7(b).
5.6 Histogram Equalization
The histogram of an image was discussed in Chapter 3, “Image and its Properties.” The histogram of an image is a discrete function, its input is the gray-level value and the output is the number of pixels with that gray-level value and can be given as h(xn ) = yn . In a grayscale image, the intensities of the image take values between [0, L − 1]. As discussed earlier, low gray-level values in the image (the left side of the Image Enhancement 105
(a) Input (b) Output
FIGURE 5.7: Example of log transformation. Original image reprinted
with permission from Mr. Karthik Bharathwaj.
histogram) correspond to dark regions and high gray-level values in the
image (the right side of the histogram) correspond to bright regions. In a low-contrast image, the histogram is narrow, whereas in an image with better contrast, the histogram is spread out. In histogram equalization, the goal is to improve the contrast of an image by rescaling the histogram so that the histogram of the new image is spread out and the pixel intensities range over all possible gray-level values. The rescaling of the histogram will be performed by using a transformation. To ensure that for every gray-level value in the input image there is a corresponding output, a one-to-one transformation is required; that is, every input has a unique output. This means the transformation should be a monotonic function. This will ensure that the transformation is invertible. Before histogram equalization transformation is defined, the follow- ing should be computed:
• The histogram of the input image is normalized so that the range
of the normalized histogram is [0, 1].
• Since the image is discrete, the probability of a gray-level value,
denoted by px (i), is the ratio of the number of pixels with a gray 106 Image Processing and Acquisition using Python
value i to the total number of pixels in the image. This is generally
called the probability distribution function (PDF).
• The cumulative distribution function (CDF) is defined as C(i) =
Xi px (j), where 0 ≤ i ≤ L − 1 and where L is the total number j=0 of gray-level values in the image. The C(i) is the sum of all the probabilities of the pixel gray-level values from 0 to i. Note that C is an increasing function.
The histogram equalization transformation can be defined as fol-
lows:
C(u) − Cmin h(u) = round ∗ (L − 1) (5.8) 1 − Cmin where Cmin is the minimum value in the cumulative distribution. For a grayscale image with range between [0, 255], if C(u) = Cmin then h(u) = 0. If C(u) = 1 then h(u) = 255. The integer value for the output image is obtained by rounding Equation 5.8. Let us consider an example to illustrate the probability, CDF, and histogram equalization. Figure 5.8 is an image of size 5 by 5. Let us assume that the gray levels of the image range from [0, 255]. The probabilities, CDF as C for each gray-level value along with the output of histogram equalization transformation, are given in Figure 5.9. The Python code for histogram equalization is given below. The image is read and a flattened image is calculated. The histogram and the CDF of the flattened image are then computed. The histogram equalization is then performed according to Equation 5.8. The flattened image is then passed through the CDF function and then reshaped to the original image shape.