kwimage.im_stack

Stack images

Module Contents

Functions

stack_images(images, axis=0, resize=None, interpolation=None, overlap=0, return_info=False, bg_value=None) Make a new image with the input images side-by-side
stack_images_grid(images, chunksize=None, axis=0, overlap=0, return_info=False, bg_value=None) Stacks images in a grid. Optionally return transforms of original image
_stack_two_images(img1, img2, axis=0, resize=None, interpolation=None, overlap=0, bg_value=None)
returns:imgB, offset_tup, sf_tup
kwimage.im_stack.stack_images(images, axis=0, resize=None, interpolation=None, overlap=0, return_info=False, bg_value=None)

Make a new image with the input images side-by-side

Parameters:
  • images (Iterable[ndarray[ndim=2]]) – image data
  • axis (int) – axis to stack on (either 0 or 1)
  • resize (int, str, or None) – if None image sizes are not modified, otherwise resize resize can be either 0 or 1. We resize the resize-th image to match the 1 - resize-th image. Can also be strings “larger” or “smaller”.
  • interpolation (int or str) – string or cv2-style interpolation type. only used if resize or overlap > 0
  • overlap (int) – number of pixels to overlap. Using a negative number results in a border.
  • return_info (bool) – if True, returns transforms (scales and translations) to map from original image to its new location.
Returns:

an image of stacked images side by side

OR

Tuple[ndarray, List]: where the first item is the aformentioned stacked

image and the second item is a list of transformations for each input image mapping it to its location in the returned image.

Return type:

ndarray

Example

>>> import kwimage
>>> img1 = kwimage.grab_test_image('carl', space='rgb')
>>> img2 = kwimage.grab_test_image('astro', space='rgb')
>>> images = [img1, img2]
>>> imgB, transforms = stack_images(images, axis=0, resize='larger',
>>>                                 overlap=-10, return_info=True)
>>> print(imgB.shape)
>>> # xdoctest: +REQUIRES(--show)
>>> import kwplot
>>> import kwimage
>>> kwplot.autompl()
>>> kwplot.imshow(imgB, colorspace='rgb')
>>> wh1 = np.multiply(img1.shape[0:2][::-1], transforms[0].scale)
>>> wh2 = np.multiply(img2.shape[0:2][::-1], transforms[1].scale)
>>> xoff1, yoff1 = transforms[0].translation
>>> xoff2, yoff2 = transforms[1].translation
>>> xywh1 = (xoff1, yoff1, wh1[0], wh1[1])
>>> xywh2 = (xoff2, yoff2, wh2[0], wh2[1])
>>> kwplot.draw_boxes(kwimage.Boxes([xywh1], 'xywh'), color=(1.0, 0, 0))
>>> kwplot.draw_boxes(kwimage.Boxes([xywh2], 'xywh'), color=(1.0, 0, 0))
>>> kwplot.show_if_requested()
((662, 512, 3), (0.0, 0.0), (0, 150))
kwimage.im_stack.stack_images_grid(images, chunksize=None, axis=0, overlap=0, return_info=False, bg_value=None)

Stacks images in a grid. Optionally return transforms of original image positions in the output image.

Parameters:
  • images (Iterable[ndarray[ndim=2]]) – image data
  • chunksize (int, default=None) – number of rows per column or columns per row depending on the value of axis. If unspecified, computes this as int(sqrt(len(images))).
  • axis (int, default=0) – If 0, chunksize is columns per row. If 1, chunksize is rows per column.
  • overlap (int) – number of pixels to overlap. Using a negative number results in a border.
  • return_info (bool) – if True, returns transforms (scales and translations) to map from original image to its new location.
Returns:

an image of stacked images in a grid pattern

OR

Tuple[ndarray, List]: where the first item is the aformentioned stacked

image and the second item is a list of transformations for each input image mapping it to its location in the returned image.

Return type:

ndarray

kwimage.im_stack._stack_two_images(img1, img2, axis=0, resize=None, interpolation=None, overlap=0, bg_value=None)
Returns:imgB, offset_tup, sf_tup
Return type:Tuple[ndarray, Tuple, Tuple]
Ignore:
import xinspect globals().update(xinspect.get_func_kwargs(_stack_two_images)) resize = 1 overlap = -10