Overview
Image transformations allow you to modify the geometry of images. Common operations include:- Resizing images to different dimensions
- Rotating images by any angle
- Flipping images horizontally or vertically
- Cropping regions of interest
- Applying affine and perspective transformations
Resizing Images
Resize images to specific dimensions or by a scaling factor.- Python
- C++
Interpolation Methods
| Method | Description | Use Case |
|---|---|---|
INTER_NEAREST | Nearest neighbor | Fastest, lowest quality |
INTER_LINEAR | Bilinear interpolation | Good balance (default) |
INTER_CUBIC | Bicubic interpolation | Slower, higher quality |
INTER_AREA | Resampling using pixel area | Best for downsampling |
INTER_LANCZOS4 | Lanczos interpolation | Highest quality, slowest |
Rotating Images
Rotate images by 90-degree increments or arbitrary angles.Simple 90-Degree Rotations
- Python
- C++
Arbitrary Angle Rotation
- Python
- C++
When rotating by arbitrary angles, parts of the image may be cropped. To preserve the entire rotated image, calculate new dimensions and adjust the rotation matrix accordingly.
Flipping Images
Flip images horizontally, vertically, or both.- Python
- C++
Cropping Images
Crop a region of interest from an image using array slicing.- Python
- C++
Affine Transformations
Affine transformations preserve parallel lines and include translation, rotation, scaling, and shearing.- Python
- C++
Perspective Transformations
Perspective transformations correct for camera angle and viewpoint changes.- Python
- C++
Key Functions
| Function | Description |
|---|---|
resize() | Resize image to specific dimensions or scale |
rotate() | Rotate image by 90, 180, or 270 degrees |
flip() | Flip image horizontally, vertically, or both |
getRotationMatrix2D() | Get rotation matrix for arbitrary angles |
getAffineTransform() | Get affine transformation matrix from 3 point pairs |
getPerspectiveTransform() | Get perspective transformation matrix from 4 point pairs |
warpAffine() | Apply affine transformation |
warpPerspective() | Apply perspective transformation |
When applying transformations, pixels that fall outside the destination image are cropped. Use appropriate border modes or adjust output dimensions to preserve all data.
