kwimage.im_core

Not sure how to best classify these functions

Module Contents

kwimage.im_core.num_channels(img)

Returns the number of color channels in an image

Parameters:img (ndarray) – an image with 2 or 3 dimensions.
Returns:the number of color channels (1, 3, or 4)
Return type:int

Example

>>> H = W = 3
>>> assert num_channels(np.empty((W, H))) == 1
>>> assert num_channels(np.empty((W, H, 1))) == 1
>>> assert num_channels(np.empty((W, H, 3))) == 3
>>> assert num_channels(np.empty((W, H, 4))) == 4
>>> # xdoctest: +REQUIRES(module:pytest)
>>> import pytest
>>> with pytest.raises(ValueError):
...     num_channels(np.empty((W, H, 2)))
kwimage.im_core.ensure_float01(img, dtype=np.float32, copy=True)

Ensure that an image is encoded using a float32 properly

Parameters:
  • img (ndarray) – an image in uint255 or float01 format. Other formats will raise errors.
  • dtype (type, default=np.float32) – a numpy floating type
  • copy (bool, default=False) – always copy if True, else copy if needed.
Returns:

an array of floats in the range 0-1

Return type:

ndarray

Raises:

ValueError – if the image type is integer and not in [0-255]

Example

>>> ensure_float01(np.array([[0, .5, 1.0]]))
array([[0. , 0.5, 1. ]], dtype=float32)
>>> ensure_float01(np.array([[0, 1, 200]]))
array([[0..., 0.0039..., 0.784...]], dtype=float32)
kwimage.im_core.ensure_uint255(img, copy=True)

Ensure that an image is encoded using a uint8 properly. Either

Parameters:
  • img (ndarray) – an image in uint255 or float01 format. Other formats will raise errors.
  • copy (bool, default=False) – always copy if True, else copy if needed.
Returns:

an array of bytes in the range 0-255

Return type:

ndarray

Raises:
  • ValueError – if the image type is float and not in [0-1]
  • ValueError – if the image type is integer and not in [0-255]

Example

>>> ensure_uint255(np.array([[0, .5, 1.0]]))
array([[  0, 127, 255]], dtype=uint8)
>>> ensure_uint255(np.array([[0, 1, 200]]))
array([[  0,   1, 200]], dtype=uint8)
kwimage.im_core.make_channels_comparable(img1, img2, atleast3d=False)

Broadcasts image arrays so they can have elementwise operations applied

Parameters:
  • img1 (ndarray) – first image
  • img2 (ndarray) – second image
  • atleast3d (bool, default=False) – if true we ensure that the channel dimension exists (only relevant for 1-channel images)

Example

>>> import itertools as it
>>> wh_basis = [(5, 5), (3, 5), (5, 3), (1, 1), (1, 3), (3, 1)]
>>> for w, h in wh_basis:
>>>     shape_basis = [(w, h), (w, h, 1), (w, h, 3)]
>>>     # Test all permutations of shap inputs
>>>     for shape1, shape2 in it.product(shape_basis, shape_basis):
>>>         print('*    input shapes: %r, %r' % (shape1, shape2))
>>>         img1 = np.empty(shape1)
>>>         img2 = np.empty(shape2)
>>>         img1, img2 = make_channels_comparable(img1, img2)
>>>         print('... output shapes: %r, %r' % (img1.shape, img2.shape))
>>>         elem = (img1 + img2)
>>>         print('... elem(+) shape: %r' % (elem.shape,))
>>>         assert elem.size == img1.size, 'outputs should have same size'
>>>         assert img1.size == img2.size, 'new imgs should have same size'
>>>         print('--------')
kwimage.im_core._alpha_fill_for(img)

helper for make_channels_comparable

kwimage.im_core.atleast_3channels(arr, copy=True)

Ensures that there are 3 channels in the image

Parameters:
  • arr (ndarray[N, M, …]) – the image
  • copy (bool) – Always copies if True, if False, then copies only when the size of the array must change.
Returns:

with shape (N, M, C), where C in {3, 4}

Return type:

ndarray

Doctest:
>>> assert atleast_3channels(np.zeros((10, 10))).shape[-1] == 3
>>> assert atleast_3channels(np.zeros((10, 10, 1))).shape[-1] == 3
>>> assert atleast_3channels(np.zeros((10, 10, 3))).shape[-1] == 3
>>> assert atleast_3channels(np.zeros((10, 10, 4))).shape[-1] == 4