coerce#
- deeptrack.image.coerce(images: List[Image | ndarray]) List[Image] #
Coerce a list of images to a consistent type.
This function ensures that all images in the input list are instances of the Image class. Additionally, if any image contains a CuPy array, all images are converted to CuPy arrays to ensure consistency.
Parameters#
- imagesList[Union[Image, np.ndarray]]
A list of images to be coerced. Each image can be an Image instance or a NumPy array.
Returns#
- List[Image]
A list of Image instances where all elements are coerced to the same type (CuPy if CuPy arrays are present in any image).
Example#
>>> import numpy as np >>> from deeptrack.image import coerce, Image
Create a list of images:
>>> img1 = Image(np.array([1, 2, 3])) >>> img2 = np.array([4, 5, 6])
Coerce the images to ensure consistency:
>>> result = coerce([img1, img2]) >>> print([type(img._value) for img in result]) [<class 'numpy.ndarray'>, <class 'numpy.ndarray'>]
If one image is a CuPy array, all are converted:
>>> import cupy >>> img3 = Image(cupy.array([7, 8, 9])) >>> result = coerce([img1, img3]) >>> print([type(img._value) for img in result]) [<class 'cupy.ndarray'>, <class 'cupy.ndarray'>]