How to perform matrix transformation in OpenCV Python?

The cv2.transform() function performs matrix transformation of each element in an input array. Since images in OpenCV are NumPy arrays, we can apply transformations directly to modify pixel values using a transformation matrix.

Syntax

cv2.transform(src, m, dst=None)

Parameters

  • src ? Input array (image)
  • m ? Transformation matrix of size (output_channels, input_channels)
  • dst ? Output array (optional)

Steps to Perform Matrix Transformation

Follow these steps to apply matrix transformation to an image ?

  • Import the required libraries OpenCV and NumPy

  • Read the input image using cv2.imread()

  • Define a transformation matrix of appropriate dimensions

  • Apply cv2.transform() with the transformation matrix

  • Display or save the transformed image

Example 1: Random Matrix Transformation

This example applies a random 3×3 transformation matrix to create color effects ?

import cv2
import numpy as np

# Create a sample colored image
height, width = 200, 200
img = np.zeros((height, width, 3), dtype=np.uint8)
img[:, :width//3] = [255, 0, 0]  # Blue region
img[:, width//3:2*width//3] = [0, 255, 0]  # Green region  
img[:, 2*width//3:] = [0, 0, 255]  # Red region

print("Original image shape:", img.shape)
print("Sample pixel values:", img[100, 50])

# Define random transformation matrix
m = np.random.randn(3, 3) * 0.5
print("Transformation matrix:")
print(m)

# Apply matrix transformation
img_transformed = cv2.transform(img, m)

print("Transformed image shape:", img_transformed.shape)
print("Sample transformed pixel:", img_transformed[100, 50])
Original image shape: (200, 200, 3)
Sample pixel values: [255   0   0]
Transformation matrix:
[[ 0.21  -0.85   0.43]
 [-0.12   0.67  -0.33]
 [ 0.89  -0.21   0.55]]
Transformed image shape: (200, 200, 3)
Sample transformed pixel: [ 53.7  191.25  226.5]

Example 2: Identity Matrix (No Change)

Using an identity matrix preserves the original image ?

import cv2
import numpy as np

# Create a sample image
img = np.array([[[100, 150, 200]]], dtype=np.uint8)
print("Original pixel:", img[0, 0])

# Identity transformation matrix
m = np.eye(3, dtype=np.float32)
print("Identity matrix:")
print(m)

# Apply transformation
img_identity = cv2.transform(img, m)
print("Transformed pixel:", img_identity[0, 0])
Original pixel: [100 150 200]
Identity matrix:
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
Transformed pixel: [100. 150. 200.]

Example 3: Color Channel Mixing

This example swaps and mixes color channels ?

import cv2
import numpy as np

# Create a sample BGR image
img = np.array([[[255, 0, 0], [0, 255, 0], [0, 0, 255]]], dtype=np.uint8)
print("Original pixels (BGR):", img[0])

# Matrix to swap B and R channels
m = np.array([[0, 0, 1],  # Red = Blue
              [0, 1, 0],  # Green = Green  
              [1, 0, 0]], dtype=np.float32)  # Blue = Red

# Apply transformation
img_swapped = cv2.transform(img, m)
print("Swapped pixels (BGR):", img_swapped[0])
Original pixels (BGR): [[255   0   0] [  0 255   0] [  0   0 255]]
Swapped pixels (BGR): [[  0.   0. 255.] [  0. 255.   0.] [255.   0.   0.]]

Key Points

  • The transformation matrix must have dimensions (output_channels, input_channels)

  • Each pixel is transformed using matrix multiplication: output = matrix × input

  • Values may be clipped to valid ranges (0-255 for uint8 images)

  • Random matrices create unpredictable color effects

Conclusion

The cv2.transform() function enables pixel-level transformations using matrix operations. Use identity matrices to preserve images, or custom matrices to create color effects, channel swaps, and artistic transformations.

Updated on: 2026-03-26T22:58:36+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements