Log transformation means replacing each pixel value with its logarithm. The general form of log transformation function is
s = T(r) = c*log(1+r)
Where, ‘s’ and ‘r’ are the output and input pixel values and c is the scaling constant represented by the following expression (for 8-bit)
c = 255/(log(1 + max_input_pixel_value))
The value of c is chosen such that we get the maximum output value corresponding to the bit size used. e.g for 8 bit image, c is chosen such that we get max value equal to 255.
For an 8-bit image, log transformation looks like this
- Expands the dark pixels in the image while compressing the brighter pixels
- Compresses the dynamic range (display of Fourier transform).
Dynamic range refers to the ratio of max and min intensity values. When the dynamic range of the image is greater than that of displaying device(like in Fourier transform), the lower values are suppressed. To overcome this issue, we use
The code below shows how to apply log transform using OpenCV Python
import numpy as np
# Load the image
img = cv2.imread('D:/downloads/pasta.JPG')
# Apply log transform
img_log = (np.log(img+1)/(np.log(1+np.max(img))))*255
# Specify the data type
img_log = np.array(img_log,dtype=np.uint8)
# Display the image
Thus, a logarithmic transform is appropriate when we want to enhance the low pixel values at the expense of loss of information in the high pixel values.
Be careful, if most of the details are present in the high pixel values, then applying the log transform results in the loss of information as shown below
In the next blog, we will discuss Power law or Gamma transformation. Hope you enjoy reading.
If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.