Create Module

The create module provides functions for creating images with various initial values such as zeros, ones, random values, etc.

Image creation functions.

This module provides functions for creating images with various initial values such as zeros, ones, random values, etc. All functions return numpy arrays with uint8 data type suitable for image processing.

Functions:

zeros, zeros_like: Create arrays filled with zeros. ones, ones_like: Create arrays filled with ones. full, full_like: Create arrays filled with a specified value. empty, empty_like: Create uninitialized arrays. white, white_like: Create arrays filled with white pixels (255). random, rand, randn, randint: Create arrays filled with random values.

cv3.create.zeros(*args)[source]

Create an array filled with zeros.

Parameters:

*args – Dimensions of the array. Can be passed as separate arguments (e.g., zeros(100, 100, 3)) or as a tuple (e.g., zeros((100, 100, 3))).

Returns:

Array filled with zeros of type uint8.

Return type:

numpy.ndarray

Example

>>> import cv3
>>> img = cv3.zeros(100, 100, 3)  # Create a 100x100x3 image filled with zeros
>>> print(img.shape)
(100, 100, 3)
>>> print(img.dtype)
uint8
cv3.create.zeros_like(img)[source]

Create an array of zeros with the same shape and type as the input array.

Parameters:

img (numpy.ndarray) – Input array whose shape and type will be used to create the new array.

Returns:

Array filled with zeros having the same shape and type

as the input array.

Return type:

numpy.ndarray

Example

>>> import cv3
>>> original = cv3.ones(50, 50, 3)
>>> new_img = cv3.zeros_like(original)  # Create array with same shape as original
>>> print(new_img.shape)
(50, 50, 3)
cv3.create.ones(*args)[source]

Create an array filled with ones.

Parameters:

*args – Dimensions of the array. Can be passed as separate arguments (e.g., ones(100, 100, 3)) or as a tuple (e.g., ones((100, 100, 3))).

Returns:

Array filled with ones of type uint8.

Return type:

numpy.ndarray

Example

>>> import cv3
>>> img = cv3.ones(100, 100, 3)  # Create a 100x100x3 image filled with ones
>>> print(img.shape)
(100, 100, 3)
cv3.create.ones_like(img)[source]

Create an array of ones with the same shape and type as the input array.

Parameters:

img (numpy.ndarray) – Input array whose shape and type will be used to create the new array.

Returns:

Array filled with ones having the same shape and type

as the input array.

Return type:

numpy.ndarray

Example

>>> import cv3
>>> original = cv3.zeros(50, 50, 3)
>>> new_img = cv3.ones_like(original)  # Create array with same shape as original
>>> print(new_img.shape)
(50, 50, 3)
cv3.create.full(*args, value)[source]

Create an array filled with a specified value.

Parameters:
  • *args – Dimensions of the array. Can be passed as separate arguments (e.g., full(100, 100, 3, value=128)) or as a tuple.

  • value – The value to fill the array with.

Returns:

Array filled with the specified value of type uint8.

Return type:

numpy.ndarray

Example

>>> import cv3
>>> img = cv3.full(100, 100, 3, value=128)  # Create image filled with 128
>>> print(img[0, 0])  # All pixels have value 128
[128 128 128]
cv3.create.full_like(img, value)[source]

Create an array filled with a specified value, with the same shape and type as the input array.

Parameters:
  • img (numpy.ndarray) – Input array whose shape and type will be used to create the new array.

  • value – The value to fill the array with.

Returns:

Array filled with the specified value having the same shape and type

as the input array.

Return type:

numpy.ndarray

Example

>>> import cv3
>>> original = cv3.zeros(50, 50, 3)
>>> new_img = cv3.full_like(original, value=255)  # Create array with same shape, filled with 255
>>> print(new_img.shape)
(50, 50, 3)
cv3.create.empty(*args)[source]

Create an uninitialized array.

Parameters:

*args – Dimensions of the array. Can be passed as separate arguments (e.g., empty(100, 100, 3)) or as a tuple.

Returns:

Uninitialized array of type uint8.

Return type:

numpy.ndarray

Note

The values in the returned array are uninitialized and may contain arbitrary data. For performance reasons, the array is not initialized to zero.

Example

>>> import cv3
>>> img = cv3.empty(100, 100, 3)  # Create uninitialized 100x100x3 image
>>> print(img.shape)
(100, 100, 3)
cv3.create.empty_like(img)[source]

Create an uninitialized array with the same shape and type as the input array.

Parameters:

img (numpy.ndarray) – Input array whose shape and type will be used to create the new array.

Returns:

Uninitialized array having the same shape and type

as the input array.

Return type:

numpy.ndarray

Note

The values in the returned array are uninitialized and may contain arbitrary data. For performance reasons, the array is not initialized to zero.

Example

>>> import cv3
>>> original = cv3.zeros(50, 50, 3)
>>> new_img = cv3.empty_like(original)  # Create uninitialized array with same shape
>>> print(new_img.shape)
(50, 50, 3)
cv3.create.white(*args)[source]

Create an array filled with white pixels (255).

Parameters:

*args – Dimensions of the array. Can be passed as separate arguments (e.g., white(100, 100, 3)) or as a tuple.

Returns:

Array filled with 255 (white) of type uint8.

Return type:

numpy.ndarray

Example

>>> import cv3
>>> img = cv3.white(100, 100, 3)  # Create a white 100x100x3 image
>>> print(img[0, 0])  # All pixels are white
[255 255 255]
cv3.create.white_like(img)[source]

Create an array filled with white pixels (255), with the same shape and type as the input array.

Parameters:

img (numpy.ndarray) – Input array whose shape and type will be used to create the new array.

Returns:

Array filled with 255 (white) having the same shape and type

as the input array.

Return type:

numpy.ndarray

Example

>>> import cv3
>>> original = cv3.zeros(50, 50, 3)
>>> white_img = cv3.white_like(original)  # Create white image with same shape
>>> print(white_img.shape)
(50, 50, 3)
cv3.create.black(*args)

Alias for zeros().

cv3.create.black_like(img)

Alias for zeros_like().

cv3.create.random(*args)[source]

Create an array filled with random values between 0 and 255.

Parameters:

*args – Dimensions of the array. Can be passed as separate arguments (e.g., random(100, 100, 3)) or as a tuple.

Returns:

Array filled with random integers [0, 255] of type uint8.

Return type:

numpy.ndarray

Example

>>> import cv3
>>> img = cv3.random(100, 100, 3)  # Create 100x100x3 image with random values
>>> print(img.shape)
(100, 100, 3)
>>> print(0 <= img.min() <= img.max() <= 255)  # All values in valid range
True
cv3.create.rand(*args)

Alias for random().

cv3.create.randn(*args)

Alias for random().

cv3.create.randint(*args)

Alias for random().

zeros(*args)

Create an array filled with zeros.

zeros_like(img)

Create an array of zeros with the same shape and type as the input array.

ones(*args)

Create an array filled with ones.

ones_like(img)

Create an array of ones with the same shape and type as the input array.

full(*args, value)

Create an array filled with a specified value.

full_like(img, value)

Create an array filled with a specified value, with the same shape and type as the input array.

empty(*args)

Create an uninitialized array.

empty_like(img)

Create an uninitialized array with the same shape and type as the input array.

white(*args)

Create an array filled with white pixels (255).

white_like(img)

Create an array filled with white pixels (255), with the same shape and type as the input array.

black(*args)

Alias for zeros().

black_like(img)

Alias for zeros_like().

random(*args)

Create an array filled with random values between 0 and 255.

rand(*args)

Alias for random().

randn(*args)

Alias for random().

randint(*args)

Alias for random().