Friday, November 15, 2024
Google search engine
HomeLanguagesLog transformation of an image using Python and OpenCV

Log transformation of an image using Python and OpenCV

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

How log value of a number is calculated?

Let’s see an example,

2^3 = 8

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 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: You can see log function in Python by visiting here.

Log transformation

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 –

python-log-transformation




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 :
python-log-transformation

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.

RELATED ARTICLES

Most Popular

Recent Comments