Log transformation of an image using Python and OpenCV

09 Mar, 2020
Logarithm value of a number is a number that raises power to a base number which gives the same number as in input. Simply, the logarithm is the inversion of exponential value of a number.
log(exp(x)) = x
Let’s see an example,
By applying logarithm in both sides,
log(2^3) = log(8)
3 * log(2) = log(8)
3 = log(8) / log(2)
Log(8) = 3 (base is 2)
We know, value of a number with power 0 is equal to 1. So,
log1 = 0 
and
log0 = infinity
We can find the log value of a number using Python as follow:
import numpy as np  # input a number as integera = int(input())  print("Natural log value of the input number is",       np.log(a))  # If you want base of log to be set to 2print("Log value of the number with base 2 is",       np.log2(a))  # If you want base of log to be set to 10print("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: You can see log function in Python by visiting here.
Logarithmic transformation of an image is one of the gray level image transformations. Log transformation of an image means replacing all pixel values, present in the image, with its logarithmic values. Log transformation is used for image enhancement as it expands dark pixels of the image as compared to higher pixel values.
The formula for applying log transformation in an image is,
S = c * log (1 + r)

where,
R = input pixel value,
C = scaling constant and
S = output pixel value
The value of ‘c’ is chosen such that we get the maximum output value corresponding to the bit size used. So, the formula for calculating ‘c’ is as follows:
c = 255 / (log (1 + max_input_pixel_value))
When we apply log transformation in an image and any pixel value is ‘0’ then its log value will become infinite. That’s why we are adding ‘1’ to each pixel value at the time of log transformation so that if any pixel value is ‘0’, it will become ‘1’ and its log value will be ‘0’.
Let’s apply log transformation in an image using Python.
Input File –
import cv2import numpy as npimport matplotlib.pyplot as plt   # Read an imageimage = cv2.imread('GFG.png')   # Apply log transformation methodc = 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 intlog_image = np.array(log_image, dtype = np.uint8)   # Display both imagesplt.imshow(image)plt.show()plt.imshow(log_image)plt.show()
Output :
Log transformation of gives actual information by enhancing the image. If we apply this method in an image having higher pixel values then it will enhance the image more and actual information of the image will be lost. So, this method can’t be applied everywhere. It can be applied in images where low pixel values are more than higher ones.
Image-Processing
Python-OpenCV
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Iterate over a list in Python
Convert integer to string in Python