Python Intensity Transformation Operations on Images (original) (raw)

Last Updated : 8 Jun, 2026

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.

Types of Intensity Transformations

The following grayscale image will be used throughout the article to demonstrate different intensity transformation techniques.

samplecamera

Input Image

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.

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:

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.

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.

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:

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 Formula:

\text{Contrast} = \frac{I_{\max} - I_{\min}}{I_{\max} + I_{\min}}

Python 1== `

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: