kwimage.im_cv2

Wrappers around cv2 functions

Note: all functions in kwimage work with RGB input by default instead of BGR.

Module Contents

kwimage.im_cv2._CV2_INTERPOLATION_TYPES
kwimage.im_cv2._rectify_interpolation(interpolation, default=cv2.INTER_LANCZOS4, grow_default=cv2.INTER_LANCZOS4, shrink_default=cv2.INTER_AREA, scale=None)

Converts interpolation into flags suitable cv2 functions

Parameters:
  • interpolation (int or str) – string or cv2-style interpolation type
  • default (int) – cv2 flag to use if interpolation is None and scale is None.
  • grow_default (int) – cv2 flag to use if interpolation is None and scale is greater than or equal to 1.
  • shrink_default (int) – cv2 flag to use if interpolation is None and scale is less than 1.
  • scale (float) – indicate if the interpolation will be used to scale the image.
Returns:

flag specifying interpolation type that can be passed to

functions like cv2.resize, cv2.warpAffine, etc…

Return type:

int

kwimage.im_cv2.imscale(img, scale, interpolation=None, return_scale=False)

Resizes an image by a scale factor.

Because the result image must have an integer number of pixels, the scale factor is rounded, and the rounded scale factor is optionaly returned.

Parameters:
  • img (ndarray) – image to resize
  • scale (float or Tuple[float, float]) – desired floating point scale factor. If a tuple, the dimension ordering is x,y.
  • interpolation (str | int) – interpolation key or code (e.g. linear lanczos)
  • return_scale (bool, default=False) – if True returns both the new image and the actual scale factor used to achive the new integer image size.
SeeAlso:
imresize

Example

>>> import kwimage
>>> import numpy as np
>>> img = np.zeros((10, 10, 3), dtype=np.uint8)
>>> new_img, new_scale = kwimage.imscale(img, scale=.85,
>>>                                      interpolation='nearest',
>>>                                      return_scale=True)
>>> assert new_scale == (.8, .8)
>>> assert new_img.shape == (8, 8, 3)
kwimage.im_cv2.imresize(img, scale=None, dsize=None, max_dim=None, min_dim=None, interpolation=None, letterbox=False, return_info=False)

Resize an image based on a scale factor, final size, or size and aspect ratio.

Slightly more general than cv2.resize and kwimage.imscale, allows for specification of either a scale factor, a final size, or the final size for a particular dimension.

Parameters:
  • img (ndarray) – image to resize
  • scale (float or Tuple[float, float]) – desired floating point scale factor. If a tuple, the dimension ordering is x,y. Mutually exclusive with dsize, max_dim, and min_dim.
  • dsize (Tuple[None | int, None | int]) – the desired with and height of the new image. If a dimension is None, then it is automatically computed to preserve aspect ratio. Mutually exclusive with size, max_dim, and min_dim.
  • max_dim (int) – new size of the maximum dimension, the other dimension is scaled to maintain aspect ratio. Mutually exclusive with size, dsize, and min_dim.
  • min_dim (int) – new size of the minimum dimension, the other dimension is scaled to maintain aspect ratio.Mutually exclusive with size, dsize, and max_dim.
  • interpolation (str | int) – interpolation key or code (e.g. linear lanczos)
  • letterbox (bool, default=False) – if used in conjunction with dsize, then the image is scaled and translated to fit in the center of the new image while maintaining aspect ratio. Black padding is added if necessary.
  • return_info (bool, default=False) – if True returns information about the final transformation in a dictionary.
Returns:

the new image and optionally an info dictionary

Return type:

ndarray | Tuple[ndarray, Dict]

Example

>>> import kwimage
>>> import numpy as np
>>> # Test scale
>>> img = np.zeros((16, 10, 3), dtype=np.uint8)
>>> new_img, info = kwimage.imresize(img, scale=.85,
>>>                                  interpolation='area',
>>>                                  return_info=True)
>>> print('info = {!r}'.format(info))
>>> assert info['scale'].tolist() == [.8, 0.875]
>>> # Test dsize without None
>>> new_img, info = kwimage.imresize(img, dsize=(5, 12),
>>>                                  interpolation='area',
>>>                                  return_info=True)
>>> print('info = {!r}'.format(info))
>>> assert info['scale'].tolist() == [0.5 , 0.75]
>>> # Test dsize with None
>>> new_img, info = kwimage.imresize(img, dsize=(6, None),
>>>                                  interpolation='area',
>>>                                  return_info=True)
>>> print('info = {!r}'.format(info))
>>> assert info['scale'].tolist() == [0.6, 0.625]
>>> # Test max_dim
>>> new_img, info = kwimage.imresize(img, max_dim=6,
>>>                                  interpolation='area',
>>>                                  return_info=True)
>>> print('info = {!r}'.format(info))
>>> assert info['scale'].tolist() == [0.4  , 0.375]
>>> # Test min_dim
>>> new_img, info = kwimage.imresize(img, min_dim=6,
>>>                                  interpolation='area',
>>>                                  return_info=True)
>>> print('info = {!r}'.format(info))
>>> assert info['scale'].tolist() == [0.6  , 0.625]

Example

>>> import kwimage
>>> import numpy as np
>>> # Test letterbox resize
>>> img = np.ones((5, 10, 3), dtype=np.float32)
>>> new_img, info = kwimage.imresize(img, dsize=(19, 19),
>>>                                  letterbox=True,
>>>                                  return_info=True)
>>> print('info = {!r}'.format(info))
>>> assert info['offset'].tolist() == [0, 4]
>>> img = np.ones((10, 5, 3), dtype=np.float32)
>>> new_img, info = kwimage.imresize(img, dsize=(19, 19),
>>>                                  letterbox=True,
>>>                                  return_info=True)
>>> print('info = {!r}'.format(info))
>>> assert info['offset'].tolist() == [4, 0]
>>> import kwimage
>>> import numpy as np
>>> # Test letterbox resize
>>> img = np.random.rand(100, 200)
>>> new_img, info = kwimage.imresize(img, dsize=(300, 300), letterbox=True, return_info=True)
kwimage.im_cv2.convert_colorspace(img, src_space, dst_space, copy=False, implicit=False, dst=None)

Converts colorspace of img. Convinience function around cv2.cvtColor

Parameters:
  • img (ndarray) – image data with float32 or uint8 precision

  • src_space (str) – input image colorspace. (e.g. BGR, GRAY)

  • dst_space (str) – desired output colorspace. (e.g. RGB, HSV, LAB)

  • implicit (bool) –

    if False, the user must correctly specify if the input/output

    colorspaces contain alpha channels.

    If True and the input image has an alpha channel, we modify

    src_space and dst_space to ensure they both end with “A”.

  • dst (ndarray[uint8_t, ndim=2], optional) – inplace-output array.

Returns:

img - image data

Return type:

ndarray

Note

Note the LAB and HSV colorspaces in float do not go into the 0-1 range.

For HSV the floating point range is:
0:360, 0:1, 0:1
For LAB the floating point range is:
0:100, -86.1875:98.234375, -107.859375:94.46875 (Note, that some extreme combinations of a and b are not valid)

Example

>>> import numpy as np
>>> convert_colorspace(np.array([[[0, 0, 1]]], dtype=np.float32), 'RGB', 'LAB')
>>> convert_colorspace(np.array([[[0, 1, 0]]], dtype=np.float32), 'RGB', 'LAB')
>>> convert_colorspace(np.array([[[1, 0, 0]]], dtype=np.float32), 'RGB', 'LAB')
>>> convert_colorspace(np.array([[[1, 1, 1]]], dtype=np.float32), 'RGB', 'LAB')
>>> convert_colorspace(np.array([[[0, 0, 1]]], dtype=np.float32), 'RGB', 'HSV')
Ignore:

# Check LAB output ranges import itertools as it s = 1 _iter = it.product(range(0, 256, s), range(0, 256, s), range(0, 256, s)) minvals = np.full(3, np.inf) maxvals = np.full(3, -np.inf) for r, g, b in ub.ProgIter(_iter, total=(256 // s) ** 3):

img255 = np.array([[[r, g, b]]], dtype=np.uint8) img01 = (img255 / 255.0).astype(np.float32) lab = convert_colorspace(img01, ‘rgb’, ‘lab’) np.minimum(lab[0, 0], minvals, out=minvals) np.maximum(lab[0, 0], maxvals, out=maxvals)

print(‘minvals = {}’.format(ub.repr2(minvals, nl=0))) print(‘maxvals = {}’.format(ub.repr2(maxvals, nl=0)))

kwimage.im_cv2._lookup_cv2_colorspace_conversion_code(src_space, dst_space)
kwimage.im_cv2.gaussian_patch(shape=(7, 7), sigma=None)

Creates a 2D gaussian patch with a specific size and sigma

Parameters:
  • shape (Tuple[int, int]) – patch height and width
  • sigma (float | Tuple[float, float]) – gaussian standard deviation

References

http://docs.opencv.org/modules/imgproc/doc/filtering.html#getgaussiankernel

CommandLine:
xdoctest -m kwimage.im_cv2 gaussian_patch –show

Example

>>> import numpy as np
>>> shape = (88, 24)
>>> sigma = None  # 1.0
>>> gausspatch = gaussian_patch(shape, sigma)
>>> sum_ = gausspatch.sum()
>>> assert np.all(np.isclose(sum_, 1.0))
>>> # xdoc: +REQUIRES(--show)
>>> import kwplot
>>> kwplot.autompl()
>>> norm = (gausspatch - gausspatch.min()) / (gausspatch.max() - gausspatch.min())
>>> kwplot.imshow(norm)
>>> kwplot.show_if_requested()

Example

>>> import numpy as np
>>> shape = (24, 24)
>>> sigma = 3.0
>>> gausspatch = gaussian_patch(shape, sigma)
>>> sum_ = gausspatch.sum()
>>> assert np.all(np.isclose(sum_, 1.0))
>>> # xdoc: +REQUIRES(--show)
>>> import kwplot
>>> kwplot.autompl()
>>> norm = (gausspatch - gausspatch.min()) / (gausspatch.max() - gausspatch.min())
>>> kwplot.imshow(norm)
>>> kwplot.show_if_requested()