Inverse log transformation in image processing python code


In this video we will continue with point operations - Log and Inverse Log transformation on images. The log transformation can be defined by this formula = c*log(1+r) where s and r are the pixel values of the output and the input image and c is a constant.

Log and Inverse Log transformation on Image in Python


I think you defined c to normalize the resulting image to a valid (visible) range. Then a rational value for c could be:

c = (L - 1)/log(L) 

where L is the number of gray levels. So s would be:

s = log(r+1) .* ((L – 1)/log(L)) 

or

s = log(r+1) .* c

Then the inverted transformation would be:

s2 = (exp(r) .^ (log(L) / (L-1))) – 1

or

s2 = (exp(r) .^ (1/c)) – 1

This is the transformation output for L=256:

Inverse log transformation in image processing python code

To apply this transformation to an image we need to do some typecasting:

figure;
L = 256;
I = imread('cameraman.tif');
log_I = uint8(log(double(I)+1) .* ((L - 1)/log(L)));
exp_I = uint8((exp(double(I)) .^ (log(L) / (L-1))) - 1);
subplot(2, 2, [1 2]); imshow(I); title('Input');
subplot(2, 2, 3); imshow(log_I); title('\itlog(I)');
subplot(2, 2, 4); imshow(exp_I); title('\itexp(I)');

Inverse log transformation in image processing python code

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,

Inverse log transformation in image processing python code

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 –

Inverse log transformation in image processing python code

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 :

Inverse log transformation in image processing python code

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.


How do you log transform an image in Python?

Log transformation s = c log(r + 1). Where s and r are the pixel values of the output and the input image and c is a constant. The value 1 is added to each of the pixel value of the input image because if there is a pixel intensity of 0 in the image, then log (0) is equal to infinity.

What is the effect of applying inverse log transformation to an image?

Log transformation is used for image enhancement as it expands dark pixels of the image as compared to higher pixel values. When we apply log transformation in an image and any pixel value is '0' then its log value will become infinite.

How do you reverse a log transformation?

1 Answer.
reciprocal. In this case the inverse of log(x) is 1/log(x).
inverse function. In this case it refers to solving the equation log(y) = x for y in which case the inverse transformation is exp(x) assuming the log is base e . (In general, the solution is b^x if the log is of base b ..

What is log transformation formula?

Recall the general form of a logarithmic function is: f ( x ) = k + a log b ⁡ where a, b, k, and h are real numbers such that b is a positive number ≠ 1, and x - h > 0. A logarithmic function is transformed into the equation: f ( x ) = 4 + 3 log ⁡ .