Intensity transformations are image processing techniques that modify pixel intensity values to enhance image appearance, improve contrast, or highlight specific features. These operations are performed directly on image pixels in the spatial domain.
- Enhance image brightness, contrast, and visibility.
- Support tasks such as image enhancement, thresholding, and feature extraction.
Types of Intensity Transformations
The following grayscale image will be used throughout the article to demonstrate different intensity transformation techniques.

1. Image Negatives
Image negative transformation inverts the intensity values of an image, producing a photographic negative effect. It is commonly used to enhance details that are difficult to observe in dark regions of an image.
- Each pixel intensity is transformed using
s=L−1−r , wherer is the input intensity ands is the output intensity. - Dark regions become bright and bright regions become dark, helping reveal hidden details in low-intensity areas.
import cv2
import numpy as np
img = cv2.imread('image.jpg', 0)
negative_img = 255 - img
cv2.imshow('Original Image', img)
cv2.imshow('Negative Image', negative_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:

2. Log Transformation
Log transformation expands low-intensity pixel values while compressing higher-intensity values, making details in darker regions of an image more visible.
- Log transformation follows the relation
s = c \log(1 + r) , wherer is the input intensity,s is the output intensity, andc is a scaling constant. - It maps a narrow range of dark pixel values to a wider range, improving visibility in low-light areas.
import cv2
import numpy as np
img = cv2.imread('sample.jpg')
c = 255 / (np.log(1 + np.max(img)))
log_transformed = c * np.log(1 + img)
log_transformed = np.array(log_transformed, dtype=np.uint8)
cv2.imwrite('log_transformed.jpg', log_transformed)
Output:

3. Power-Law (Gamma) Transformation
Power-law transformation, also known as gamma transformation, adjusts image brightness using a power-law relationship between input and output pixel intensities. It is widely used for image enhancement and display correction.
- Gamma transformation follows the relation
s = cr^{\gamma} , wherec is a constant and\gamma controls the brightness of the output image. - Values of
\gamma < 1 brighten the image, while values of\gamma > 1 darken the image.
import cv2
import numpy as np
# Open the image.
img = cv2.imread('sample.jpg')
# Trying 4 gamma values.
for gamma in [0.1, 0.5, 1.2, 2.2]:
# Apply gamma correction.
gamma_corrected = np.array(255*(img / 255) ** gamma, dtype = 'uint8')
# Save edited images.
cv2.imwrite('gamma_transformed'+str(gamma)+'.jpg', gamma_corrected)
Output:
Gamma = 0.1:

Gamma = 0.5:

Gamma = 1.2:

Gamma = 2.2:

Note:
- Gamma = 0.1, 0.5: Produces brighter images by increasing low-intensity pixel values.
- Gamma = 1.2, 2.2: Produces darker images by reducing pixel intensities.
4. Piecewise-Linear Transformation Functions
Contrast stretching is a piecewise-linear transformation that improves image contrast by expanding the range of intensity values. It enhances the visibility of details in images with poor contrast.
- Contrast stretching increases the difference between dark and bright regions, making image features more distinguishable.
- The transformation preserves the relative ordering of pixel intensities while expanding their range.
Contrast Formula:
\text{Contrast} = \frac{I_{\max} - I_{\min}}{I_{\max} + I_{\min}}
import cv2
import numpy as np
# Function to map each intensity level to output intensity level.
def pixelVal(pix, r1, s1, r2, s2):
if (0 <= pix and pix <= r1):
return (s1 / r1)*pix
elif (r1 < pix and pix <= r2):
return ((s2 - s1)/(r2 - r1)) * (pix - r1) + s1
else:
return ((255 - s2)/(255 - r2)) * (pix - r2) + s2
# Open the image.
img = cv2.imread('sample.jpg')
# Define parameters.
r1 = 70
s1 = 0
r2 = 140
s2 = 255
# Vectorize the function to apply it to each value in the Numpy array.
pixelVal_vec = np.vectorize(pixelVal)
# Apply contrast stretching.
contrast_stretched = pixelVal_vec(img, r1, s1, r2, s2)
# Save edited image.
cv2.imwrite('contrast_stretch.jpg', contrast_stretched)
Output:
