mp.ImageFrame

A container for storing an image or a video frame, in one of several formats.

Formats supported by ImageFrame are listed in the ImageFormat enum. Pixels are encoded row-major in an interleaved fashion. ImageFrame supports uint8, uint16, and float as its data types.

ImageFrame can be created by copying the data from a numpy ndarray that stores the pixel data continuously. An ImageFrame may realign the input data on its default alignment boundary during creation. The data in an ImageFrame will become immutable after creation.

Creation examples:

import cv2
cv_mat = cv2.imread(input_file)
rgb_frame = mp.ImageFrame(image_format=ImageFormat.SRGB, data=cv_mat)
gray_frame = mp.ImageFrame(
    image_format=ImageFormat.GRAY,
    data=cv2.cvtColor(cv_mat, cv2.COLOR_RGB2GRAY))

from PIL import Image
pil_img = Image.new('RGB', (60, 30), color = 'red')
image_frame = mp.ImageFrame(
    image_format=mp.ImageFormat.SRGB, data=np.asarray(pil_img))

The pixel data in an ImageFrame can be retrieved as a numpy ndarray by calling ImageFrame.numpy_view(). The returned numpy ndarray is a reference to the internal data and itself is unwritable. If the callers want to modify the numpy ndarray, it's required to obtain a copy of it.

Pixel data retrieval examples: for channel in range(num_channel): for col in range(width): for row in range(height): print(image_frame[row, col, channel])

output_ndarray = image_frame.numpy_view() print(output_ndarray[0, 0, 0]) copied_ndarray = np.copy(output_ndarray) copied_ndarray[0,0,0] = 0

byte_depth

channels

height

image_format

width

Methods

is_aligned

Return True if each row of the data is aligned to alignment boundary, which must be 1 or a power of 2.

Args
alignment_boundary An integer.

Returns
A boolean.

Examples
image_frame.is_aligned(16)

is_contiguous

Return True if the pixel data is stored contiguously (without any alignment padding areas).

is_empty

Return True if the pixel data is unallocated.

numpy_view

Return the image frame pixel data as an unwritable numpy ndarray.

Realign the pixel data to be stored contiguously and return a reference to the unwritable numpy ndarray. If the callers want to modify the numpy array data, it's required to obtain a copy of the ndarray.

Returns
An unwritable numpy ndarray.

Examples
output_ndarray = image_frame.numpy_view() copied_ndarray = np.copy(output_ndarray) copied_ndarray[0,0,0] = 0

__getitem__

Use the indexer operators to access pixel data.

Raises
IndexError If the index is invalid or out of bounds.

Examples
for channel in range(num_channel): for col in range(width): for row in range(height): print(image_frame[row, col, channel])