Last modified: Jan 17, 2025 By Alexander Williams

Python OpenCV cv2.warpAffine() Guide

In image processing, transformations like rotation, scaling, and translation are essential. OpenCV provides the cv2.warpAffine() function to perform these tasks efficiently.

What is cv2.warpAffine()?

The cv2.warpAffine() function applies an affine transformation to an image. An affine transformation includes rotation, scaling, and translation. It preserves lines and parallelism.

Syntax of cv2.warpAffine()

The syntax for cv2.warpAffine() is straightforward. It takes three main arguments: the source image, the transformation matrix, and the size of the output image.


    cv2.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]])
    

src is the input image. M is the 2x3 transformation matrix. dsize is the size of the output image.

Creating a Transformation Matrix

To use cv2.warpAffine(), you need a transformation matrix. OpenCV provides functions like cv2.getRotationMatrix2D() for rotation and scaling.


    import cv2
    import numpy as np

    # Load an image
    image = cv2.imread('image.jpg')

    # Get the image dimensions
    (h, w) = image.shape[:2]

    # Define the center of the image
    center = (w // 2, h // 2)

    # Create a rotation matrix
    M = cv2.getRotationMatrix2D(center, 45, 1.0)

    # Apply the affine transformation
    rotated = cv2.warpAffine(image, M, (w, h))

    # Display the result
    cv2.imshow("Rotated Image", rotated)
    cv2.waitKey(0)
    

This code rotates the image by 45 degrees around its center. The cv2.getRotationMatrix2D() function creates the transformation matrix.

Translation Using cv2.warpAffine()

Translation shifts an image by a specified number of pixels in the x and y directions. You can create a translation matrix manually.


    import cv2
    import numpy as np

    # Load an image
    image = cv2.imread('image.jpg')

    # Define the translation matrix
    M = np.float32([[1, 0, 100], [0, 1, 50]])

    # Apply the affine transformation
    translated = cv2.warpAffine(image, M, (w, h))

    # Display the result
    cv2.imshow("Translated Image", translated)
    cv2.waitKey(0)
    

This code translates the image 100 pixels to the right and 50 pixels down. The matrix M defines the translation.

Scaling with cv2.warpAffine()

Scaling changes the size of an image. You can combine scaling with rotation in the transformation matrix.


    import cv2
    import numpy as np

    # Load an image
    image = cv2.imread('image.jpg')

    # Define the scaling factor
    scale = 0.5

    # Create a scaling matrix
    M = np.float32([[scale, 0, 0], [0, scale, 0]])

    # Apply the affine transformation
    scaled = cv2.warpAffine(image, M, (int(w * scale), int(h * scale)))

    # Display the result
    cv2.imshow("Scaled Image", scaled)
    cv2.waitKey(0)
    

This code scales the image to half its original size. The matrix M defines the scaling transformation.

Combining Transformations

You can combine multiple transformations into a single matrix. This is useful for complex image manipulations.


    import cv2
    import numpy as np

    # Load an image
    image = cv2.imread('image.jpg')

    # Define the rotation and scaling
    M_rotate = cv2.getRotationMatrix2D(center, 45, 1.0)
    M_scale = np.float32([[0.5, 0, 0], [0, 0.5, 0]])

    # Combine the matrices
    M_combined = np.dot(M_rotate, M_scale)

    # Apply the combined transformation
    transformed = cv2.warpAffine(image, M_combined, (w, h))

    # Display the result
    cv2.imshow("Combined Transformation", transformed)
    cv2.waitKey(0)
    

This code combines rotation and scaling into a single transformation. The np.dot() function multiplies the matrices.

Conclusion

The cv2.warpAffine() function is a powerful tool for image transformations. It supports rotation, scaling, and translation. By mastering this function, you can perform complex image manipulations with ease.

For more advanced image processing techniques, check out our guides on cv2.morphologyEx() and cv2.threshold().