Transform Module

The transform module provides functions for transforming images including flipping, rotating, scaling, shifting, resizing, cropping, and padding.

Image transformation operations.

This module provides functions for transforming images including flipping, rotating, scaling, shifting, resizing, cropping, and padding.

cv3.transform.vflip(img)[source]

Flip image vertically (around x-axis).

Parameters:

img (numpy.ndarray) – Input image.

Returns:

Vertically flipped image.

Return type:

numpy.ndarray

Example

>>> import cv3
>>> import numpy as np
>>> # Create a simple image
>>> img = np.zeros((100, 100, 3), dtype=np.uint8)
>>> img[25:75, 25:75] = [255, 255, 255]  # White square
>>> # Flip vertically
>>> flipped = cv3.vflip(img)
cv3.transform.hflip(img)[source]

Flip image horizontally (around y-axis).

Parameters:

img (numpy.ndarray) – Input image.

Returns:

Horizontally flipped image.

Return type:

numpy.ndarray

Example

>>> import cv3
>>> import numpy as np
>>> # Create a simple image
>>> img = np.zeros((100, 100, 3), dtype=np.uint8)
>>> img[25:75, 25:75] = [255, 255, 255]  # White square
>>> # Flip horizontally
>>> flipped = cv3.hflip(img)
cv3.transform.dflip(img)[source]

Flip image diagonally (around both axes).

Parameters:

img (numpy.ndarray) – Input image.

Returns:

Diagonally flipped image.

Return type:

numpy.ndarray

Example

>>> import cv3
>>> import numpy as np
>>> # Create a simple image
>>> img = np.zeros((100, 100, 3), dtype=np.uint8)
>>> img[25:75, 25:75] = [255, 255, 255]  # White square
>>> # Flip diagonally
>>> flipped = cv3.dflip(img)
cv3.transform.transform(img, angle, scale, inter=1, border=0, value=None)[source]

Apply affine transformation to image.

Parameters:
  • img (numpy.ndarray) – Input image.

  • angle (float) – Rotation angle in degrees.

  • scale (float) – Scaling factor.

  • inter (int or str, optional) – Interpolation method. Can be one of: ‘nearest’, ‘linear’, ‘area’, ‘cubic’, ‘lanczos4’ or OpenCV flags. Defaults to cv2.INTER_LINEAR.

  • border (int or str, optional) – Border type. Can be one of: ‘constant’, ‘replicate’, ‘reflect’, ‘wrap’, ‘default’ or OpenCV flags. Defaults to cv2.BORDER_CONSTANT.

  • value – Border color value for constant border type. Defaults to None.

Returns:

Transformed image.

Return type:

numpy.ndarray

Example

>>> import cv3
>>> import numpy as np
>>> # Create a simple image
>>> img = np.zeros((100, 100, 3), dtype=np.uint8)
>>> img[25:75, 25:75] = [255, 255, 255]  # White square
>>> # Rotate 45 degrees and scale by 1.5
>>> transformed = cv3.transform(img, 45, 1.5)
>>> # Rotate with custom border color
>>> transformed = cv3.transform(img, 45, 1.5, value='red')
cv3.transform.rotate(img, angle, inter=1, border=0, value=None)[source]

Rotate image by specified angle.

Parameters:
  • img (numpy.ndarray) – Input image.

  • angle (float) – Rotation angle in degrees.

  • inter (int or str, optional) – Interpolation method. Can be one of: ‘nearest’, ‘linear’, ‘area’, ‘cubic’, ‘lanczos4’ or OpenCV flags. Defaults to cv2.INTER_LINEAR.

  • border (int or str, optional) – Border type. Can be one of: ‘constant’, ‘replicate’, ‘reflect’, ‘wrap’, ‘default’ or OpenCV flags. Defaults to cv2.BORDER_CONSTANT.

  • value – Border color value for constant border type. Defaults to None.

Returns:

Rotated image.

Return type:

numpy.ndarray

Example

>>> import cv3
>>> import numpy as np
>>> # Create a simple image
>>> img = np.zeros((100, 100, 3), dtype=np.uint8)
>>> img[25:75, 25:75] = [255, 255, 255]  # White square
>>> # Rotate 45 degrees
>>> rotated = cv3.rotate(img, 45)
>>> # Rotate with custom border color
>>> rotated = cv3.rotate(img, 45, value=[0, 128, 128])
cv3.transform.rotate90(img, inter=1, border=0, value=None)[source]

Rotate image by 90 degrees clockwise.

Parameters:
  • img (numpy.ndarray) – Input image.

  • inter (int or str, optional) – Interpolation method. Can be one of: ‘nearest’, ‘linear’, ‘area’, ‘cubic’, ‘lanczos4’ or OpenCV flags. Defaults to cv2.INTER_LINEAR.

  • border (int or str, optional) – Border type. Can be one of: ‘constant’, ‘replicate’, ‘reflect’, ‘wrap’, ‘default’ or OpenCV flags. Defaults to cv2.BORDER_CONSTANT.

  • value – Border color value for constant border type. Defaults to None.

Returns:

Rotated image.

Return type:

numpy.ndarray

Example

>>> import cv3
>>> import numpy as np
>>> # Create a simple image
>>> img = np.zeros((100, 100, 3), dtype=np.uint8)
>>> img[25:75, 25:75] = [255, 255, 255]  # White square
>>> # Rotate by 90 degrees
>>> rotated = cv3.rotate90(img)
cv3.transform.rotate180(img, inter=1, border=0, value=None)[source]

Rotate image by 180 degrees.

Parameters:
  • img (numpy.ndarray) – Input image.

  • inter (int or str, optional) – Interpolation method. Can be one of: ‘nearest’, ‘linear’, ‘area’, ‘cubic’, ‘lanczos4’ or OpenCV flags. Defaults to cv2.INTER_LINEAR.

  • border (int or str, optional) – Border type. Can be one of: ‘constant’, ‘replicate’, ‘reflect’, ‘wrap’, ‘default’ or OpenCV flags. Defaults to cv2.BORDER_CONSTANT.

  • value – Border color value for constant border type. Defaults to None.

Returns:

Rotated image.

Return type:

numpy.ndarray

Example

>>> import cv3
>>> import numpy as np
>>> # Create a simple image
>>> img = np.zeros((100, 100, 3), dtype=np.uint8)
>>> img[25:75, 25:75] = [255, 255, 255]  # White square
>>> # Rotate by 180 degrees
>>> rotated = cv3.rotate180(img)
cv3.transform.rotate270(img, inter=1, border=0, value=None)[source]

Rotate image by 270 degrees clockwise (or 90 degrees counter-clockwise).

Parameters:
  • img (numpy.ndarray) – Input image.

  • inter (int or str, optional) – Interpolation method. Can be one of: ‘nearest’, ‘linear’, ‘area’, ‘cubic’, ‘lanczos4’ or OpenCV flags. Defaults to cv2.INTER_LINEAR.

  • border (int or str, optional) – Border type. Can be one of: ‘constant’, ‘replicate’, ‘reflect’, ‘wrap’, ‘default’ or OpenCV flags. Defaults to cv2.BORDER_CONSTANT.

  • value – Border color value for constant border type. Defaults to None.

Returns:

Rotated image.

Return type:

numpy.ndarray

Example

>>> import cv3
>>> import numpy as np
>>> # Create a simple image
>>> img = np.zeros((100, 100, 3), dtype=np.uint8)
>>> img[25:75, 25:75] = [255, 255, 255]  # White square
>>> # Rotate by 270 degrees
>>> rotated = cv3.rotate270(img)
cv3.transform.scale(img, factor, inter=1, border=0, value=None)[source]

Scale image by specified factor.

Parameters:
  • img (numpy.ndarray) – Input image.

  • factor (float) – Scaling factor.

  • inter (int or str, optional) – Interpolation method. Can be one of: ‘nearest’, ‘linear’, ‘area’, ‘cubic’, ‘lanczos4’ or OpenCV flags. Defaults to cv2.INTER_LINEAR.

  • border (int or str, optional) – Border type. Can be one of: ‘constant’, ‘replicate’, ‘reflect’, ‘wrap’, ‘default’ or OpenCV flags. Defaults to cv2.BORDER_CONSTANT.

  • value – Border color value for constant border type. Defaults to None.

Returns:

Scaled image.

Return type:

numpy.ndarray

Example

>>> import cv3
>>> import numpy as np
>>> # Create a simple image
>>> img = np.zeros((100, 100, 3), dtype=np.uint8)
>>> img[25:75, 25:75] = [255, 255, 255]  # White square
>>> # Scale by 1.5
>>> scaled = cv3.scale(img, 1.5)
>>> # Scale with custom border color
>>> scaled = cv3.scale(img, 0.5, value='green')
cv3.transform.shift(img, x, y, border=0, value=None, rel=None)[source]

Shift image by x and y pixels.

Parameters:
  • img (numpy.ndarray) – Input image.

  • x (int or float) – Shift in x direction.

  • y (int or float) – Shift in y direction.

  • border (int or str, optional) – Border type. Can be one of: ‘constant’, ‘replicate’, ‘reflect’, ‘wrap’, ‘default’ or OpenCV flags. Defaults to cv2.BORDER_CONSTANT.

  • value – Border color value for constant border type. Defaults to None.

  • rel (bool, optional) – Whether to interpret x and y as relative values. Defaults to None.

Returns:

Shifted image.

Return type:

numpy.ndarray

Example

>>> import cv3
>>> import numpy as np
>>> # Create a simple image
>>> img = np.zeros((100, 100, 3), dtype=np.uint8)
>>> img[25:75, 25:75] = [255, 255, 255]  # White square
>>> # Shift by 10 pixels in x and 20 pixels in y
>>> shifted = cv3.shift(img, 10, 20)
cv3.transform.translate(img, x, y, border=0, value=None, rel=None)

Alias for shift().

cv3.transform.xshift(img, x, border=0, value=None, rel=None)[source]

Shift image horizontally by x pixels.

Parameters:
  • img (numpy.ndarray) – Input image.

  • x (int or float) – Shift in x direction.

  • border (int or str, optional) – Border type. Can be one of: ‘constant’, ‘replicate’, ‘reflect’, ‘wrap’, ‘default’ or OpenCV flags. Defaults to cv2.BORDER_CONSTANT.

  • value – Border color value for constant border type. Defaults to None.

  • rel (bool, optional) – Whether to interpret x as relative value. Defaults to None.

Returns:

Horizontally shifted image.

Return type:

numpy.ndarray

Example

>>> import cv3
>>> import numpy as np
>>> # Create a simple image
>>> img = np.zeros((100, 100, 3), dtype=np.uint8)
>>> img[25:75, 25:75] = [255, 255, 255]  # White square
>>> # Shift by 10 pixels in x direction
>>> shifted = cv3.xshift(img, 10)
cv3.transform.xtranslate(img, x, border=0, value=None, rel=None)

Alias for xshift().

cv3.transform.yshift(img, y, border=0, value=None, rel=None)[source]

Shift image vertically by y pixels.

Parameters:
  • img (numpy.ndarray) – Input image.

  • y (int or float) – Shift in y direction.

  • border (int or str, optional) – Border type. Can be one of: ‘constant’, ‘replicate’, ‘reflect’, ‘wrap’, ‘default’ or OpenCV flags. Defaults to cv2.BORDER_CONSTANT.

  • value – Border color value for constant border type. Defaults to None.

  • rel (bool, optional) – Whether to interpret y as relative value. Defaults to None.

Returns:

Vertically shifted image.

Return type:

numpy.ndarray

Example

>>> import cv3
>>> import numpy as np
>>> # Create a simple image
>>> img = np.zeros((100, 100, 3), dtype=np.uint8)
>>> img[25:75, 25:75] = [255, 255, 255]  # White square
>>> # Shift by 10 pixels in y direction
>>> shifted = cv3.yshift(img, 10)
cv3.transform.ytranslate(img, y, border=0, value=None, rel=None)

Alias for yshift().

cv3.transform.resize(img, width, height, inter=1, rel=None)[source]

Resize image to specified dimensions.

Parameters:
  • img (numpy.ndarray) – Input image.

  • width (int or float) – Target width.

  • height (int or float) – Target height.

  • inter (int or str, optional) – Interpolation method. Can be one of: ‘nearest’, ‘linear’, ‘area’, ‘cubic’, ‘lanczos4’ or OpenCV flags. Defaults to cv2.INTER_LINEAR.

  • rel (bool, optional) – Whether to interpret width and height as relative values. Defaults to None.

Returns:

Resized image.

Return type:

numpy.ndarray

Example

>>> import cv3
>>> import numpy as np
>>> # Create a simple image
>>> img = np.zeros((100, 100, 3), dtype=np.uint8)
>>> img[25:75, 25:75] = [255, 255, 255]  # White square
>>> # Resize to 200x200
>>> resized = cv3.resize(img, 200, 200)
cv3.transform.crop(img, x0, y0, x1, y1, mode='xyxy', rel=None, copy=True)[source]

Crop image to specified rectangle.

Parameters:
  • img (numpy.ndarray) – Input image.

  • x0 (int or float) – Rectangle coordinates.

  • y0 (int or float) – Rectangle coordinates.

  • x1 (int or float) – Rectangle coordinates.

  • y1 (int or float) – Rectangle coordinates.

  • mode (str, optional) – Coordinate mode. Can be ‘xyxy’, ‘xywh’, ‘ccwh’. Defaults to ‘xyxy’.

  • rel (bool, optional) – Whether to interpret coordinates as relative values. Defaults to None.

  • copy (bool, optional) – Whether to return a copy of the cropped region. Defaults to True.

Returns:

Cropped image.

Return type:

numpy.ndarray

Example

>>> import cv3
>>> import numpy as np
>>> # Create a simple image
>>> img = np.zeros((100, 100, 3), dtype=np.uint8)
>>> img[:, :] = [255, 255, 255]  # White image
>>> # Crop to 50x50 square in the center
>>> cropped = cv3.crop(img, 25, 25, 75, 75)
>>> # Crop without copying the data
>>> cropped_view = cv3.crop(img, 25, 25, 75, 75, copy=False)
cv3.transform.pad(img, y0, y1, x0, x1, border=0, value=None, rel=None)[source]

Pad image with specified borders.

Parameters:
  • img (numpy.ndarray) – Input image.

  • y0 (int or float) – Padding values for each side.

  • y1 (int or float) – Padding values for each side.

  • x0 (int or float) – Padding values for each side.

  • x1 (int or float) – Padding values for each side.

  • border (int or str, optional) – Border type. Can be one of: ‘constant’, ‘replicate’, ‘reflect’, ‘wrap’, ‘default’ or OpenCV flags. Defaults to cv2.BORDER_CONSTANT.

  • value – Border color value for constant border type. Defaults to None.

  • rel (bool, optional) – Whether to interpret padding values as relative. Defaults to None.

Returns:

Padded image.

Return type:

numpy.ndarray

Example

>>> import cv3
>>> import numpy as np
>>> # Create a simple image
>>> img = np.zeros((100, 100, 3), dtype=np.uint8)
>>> img[25:75, 25:75] = [255, 255, 255]  # White square
>>> # Pad with 10 pixels on each side
>>> padded = cv3.pad(img, 10, 10, 10, 10)
>>> # Pad with custom border color
>>> padded = cv3.pad(img, 10, 10, 10, 10, value=[128, 128, 0])
cv3.transform.copyMakeBorder(img, y0, y1, x0, x1, border=0, value=None, rel=None)

Alias for pad().

vflip(img)

Flip image vertically (around x-axis).

hflip(img)

Flip image horizontally (around y-axis).

dflip(img)

Flip image diagonally (around both axes).

transform(img, angle, scale[, inter, ...])

Apply affine transformation to image.

rotate(img, angle[, inter, border, value])

Rotate image by specified angle.

rotate90(img[, inter, border, value])

Rotate image by 90 degrees clockwise.

rotate180(img[, inter, border, value])

Rotate image by 180 degrees.

rotate270(img[, inter, border, value])

Rotate image by 270 degrees clockwise (or 90 degrees counter-clockwise).

scale(img, factor[, inter, border, value])

Scale image by specified factor.

shift(img, x, y[, border, value, rel])

Shift image by x and y pixels.

translate(img, x, y[, border, value, rel])

Alias for shift().

xshift(img, x[, border, value, rel])

Shift image horizontally by x pixels.

xtranslate(img, x[, border, value, rel])

Alias for xshift().

yshift(img, y[, border, value, rel])

Shift image vertically by y pixels.

ytranslate(img, y[, border, value, rel])

Alias for yshift().

resize(img, width, height[, inter, rel])

Resize image to specified dimensions.

crop(img, x0, y0, x1, y1[, mode, rel, copy])

Crop image to specified rectangle.

pad(img, y0, y1, x0, x1[, border, value, rel])

Pad image with specified borders.

copyMakeBorder(img, y0, y1, x0, x1[, ...])

Alias for pad().