c = 255/log[1+Maximum pixel value from the input image].
#----- Example Python program for logarithmic transformation of a Digital Image -----
# import Pillow modules
from PIL import Image
from PIL import ImageFilter
import math
# Compute log
def logTransform[c, f]:
g = c * math.log[float[1 + f],10];
return g;
# Apply logarithmic transformation for an image
def logTransformImage[image, outputMax = 255, inputMax=255]:
c = outputMax/math.log[inputMax+1,10];
# Read pixels and apply logarithmic transformation
for i in range[0, img.size[0]-1]:
for j in range[0, img.size[1]-1]:
# Get pixel value at [x,y] position of the image
f = img.getpixel[[i,j]];
# Do log transformation of the pixel
redPixel = round[logTransform[c, f[0]]];
greenPixel = round[logTransform[c, f[1]]];
bluePixel = round[logTransform[c, f[2]]];
# Modify the image with the transformed pixel values
img.putpixel[[i,j],[redPixel, greenPixel, bluePixel]];
return image;
# Display the original image
imageFileName = "forest.jpg";
img = Image.open[imageFileName];
img.show[];
# Display the image after applying the logarithmic transformation
logTransformedImage = logTransformImage[img];
logTransformedImage.show[];
In the original image the trees are darker and a small region of a stream or a path is not visible. Many trees as well are not clearly visibile.
The logarithmically transformed image has the most of the trees distinctly visible. However the background snow corresponding to the brightest pixels[not the foreground snow] is mostly the same in both the images.
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,
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
a
=
int
[
input
[]]
print
[
"Natural log value of the input number is"
,
np.log[a]]
print
[
"Log value of the number with base 2 is"
,
np.log2[a]]
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 –
import
cv2
import
numpy as np
import
matplotlib.pyplot as plt
image
=
cv2.imread[
'GFG.png'
]
c
=
255
/
np.log[
1
+
np.
max
[image]]
log_image
=
c
*
[np.log[image
+
1
]]
log_image
=
np.array[log_image, dtype
=
np.uint8]
plt.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.