Log transformation of an image using Python and OpenCV

log(exp(x)) = x

How log value of a number is calculated?

log(2^3) = log(8)
3 * log(2) = log(8)
3 = log(8) / log(2)
Log(8) = 3 (base is 2)
log1 = 0 
log0 = infinity
import numpy as np
  
# input a number as integer
a = int(input())
  
print("Natural log value of the input number is"
      np.log(a))
  
# If you want base of log to be set to 2
print("Log value of the number with base 2 is"
      np.log2(a))
  
# If you want base of log to be set to 10
print("Log value of the number with base 10 is",
      np.log10(a))

                    
Examples:
Input : 8
Output : 
Natural log value of the input number is 2.0794415416798357
Log value of the number with base 2 is 3.0
Log value of the number with base 10 is 0.9030899869919435

Input : 255
Output : 
Natural log value of the input number is 5.541263545158426
Log value of the number with base 2 is 7.994353436858858
Log value of the number with base 10 is 2.406540180433955
Note:

Log transformation

S = c * log (1 + r)

where,
R = input pixel value,
C = scaling constant and
S = output pixel value
c = 255 / (log (1 + max_input_pixel_value))
Input File –
import cv2
import numpy as np
import matplotlib.pyplot as plt
   
# Read an image
image = cv2.imread('GFG.png')
   
# Apply log transformation method
c = 255 / np.log(1 + np.max(image))
log_image = c * (np.log(image + 1))
   
# Specify the data type so that
# float value will be converted to int
log_image = np.array(log_image, dtype = np.uint8)
   
# Display both images
plt.imshow(image)
plt.show()
plt.imshow(log_image)
plt.show()

                    
Output :


Contact Us