OPENCV MATRIX MATH /// CV2.RESIZE /// CV2.WARPAFFINE /// SCALING /// ROTATION /// OPENCV MATRIX MATH /// CV2.RESIZE ///

Image Transformations

Master spatial geometry in OpenCV. Learn to resize matrices and perform rigid affine rotations using Python.

transformations.py
1 / 7
12345
🤖

Tutor:Images are just 2D matrices of pixels. OpenCV allows us to mathematically transform these matrices quickly.


Skill Matrix

UNLOCK NODES BY MASTERING MATRICES.

Concept: Scaling

Scaling resizes the underlying image matrix. Use `cv2.resize()`.

System Check

Which parameter controls the calculation of new pixel values when resizing?


Image Transformations: Scaling & Rotation

In Computer Vision, images are represented as multidimensional NumPy arrays. Transformations like scaling and rotation are essentially matrix multiplications applied to these arrays using Affine Mathematics.

Scaling: cv2.resize()

Scaling involves resizing an image. The spatial resolution of the matrix changes. When resizing, OpenCV must calculate new pixel values through Interpolation.

  • cv2.INTER_AREA: Best for shrinking (decimation). It resamples using pixel area relation, preventing moiré patterns.
  • cv2.INTER_CUBIC: Best for zooming. A slow but high-quality bicubic interpolation over 4x4 pixel neighborhoods.
  • cv2.INTER_LINEAR: The default method, a good balance of speed and quality for zooming.

Rotation: Affine Transformation

Rotation is a rigid affine transformation. To rotate an image safely without cropping, you define a pivot point (usually the center), the angle in degrees, and a scaling factor.

You first generate a 2x3 transformation matrix using cv2.getRotationMatrix2D(center, angle, scale). Then, you apply this matrix to every pixel using cv2.warpAffine(image, M, (width, height)).

Computer Vision FAQs

What is an Affine Transformation?

In affine transformations, all parallel lines in the original image will still be parallel in the output image. Rotation, translation, and scaling are all affine operations.

Why does my rotated image get cropped?

If you rotate an image (e.g., a rectangle by 45 degrees), the new bounding box is larger than the original dimensions. cv2.warpAffine keeps the original dimensions by default. You must mathematically calculate the new bounding box dimensions and adjust the translation part of your rotation matrix to prevent cropping.

OpenCV Definitions

cv2.resize()
Function to scale an image by passing explicit dimensions or scaling factors (fx, fy).
python
Interpolation
The mathematical method used to estimate pixel values when scaling an image up or down.
python
cv2.getRotationMatrix2D()
Calculates an affine matrix of 2D rotation given a center point, angle, and scale.
python
cv2.warpAffine()
Applies an affine transformation to an image based on a provided 2x3 matrix.
python