Images (deepdish.image)

Basic functions for working with images.

deepdish.image.asgray(im)[source]

Takes an image and returns its grayscale version by averaging the color channels. if an alpha channel is present, it will simply be ignored. If a grayscale image is given, the original image is returned.

Parameters:image (ndarray, ndim 2 or 3) – RGB or grayscale image.
Returns:gray_image – Grayscale version of image.
Return type:ndarray, ndim 2
deepdish.image.bounding_box(alpha, threshold=0.1)[source]

Returns a bounding box of the support.

Parameters:
  • alpha (ndarray, ndim=2) – Any one-channel image where the background has zero or low intensity.
  • threshold (float) – The threshold that divides background from foreground.
Returns:

bounding_box – The bounding box describing the smallest rectangle containing the foreground object, as defined by the threshold.

Return type:

(top, left, bottom, right)

deepdish.image.bounding_box_as_binary_map(alpha, threshold=0.1)[source]

Similar to bounding_box, except returns the bounding box as a binary map the same size as the input.

Same parameters as bounding_box.

Returns:binary_map – Binary map with True if object and False if background.
Return type:ndarray, ndim=2, dtype=np.bool_
deepdish.image.crop(im, size)[source]

Crops an image in the center.

Parameters:size (tuple, (height, width)) – Finally size after cropping.
deepdish.image.crop_or_pad(im, size, value=0)[source]

Crops an image in the center.

Parameters:size (tuple, (height, width)) – Finally size after cropping.
deepdish.image.crop_to_bounding_box(im, bb)[source]

Crops according to a bounding box.

Parameters:bounding_box (tuple, (top, left, bottom, right)) – Crops inclusively for top/left and exclusively for bottom/right.
deepdish.image.extract_patches(images, patch_shape, samples_per_image=40, seed=0, cycle=True)[source]

Takes a set of images and yields randomly chosen patches of specified size.

Parameters:
  • images (iterable) – The images have to be iterable, and each element must be a Numpy array with at least two spatial 2 dimensions as the first and second axis.
  • patch_shape (tuple, length 2) – The spatial shape of the patches that should be extracted. If the images have further dimensions beyond the spatial, the patches will copy these too.
  • samples_per_image (int) – Samples to extract before moving on to the next image.
  • seed (int) – Seed with which to select the patches.
  • cycle (bool) – If True, then the function will produce patches indefinitely, by going back to the first image when all are done. If False, the iteration will stop when there are no more images.
Returns:

This function returns a generator that will produce patches.

Return type:

patch_generator

Examples

>>> import deepdish as dd
>>> import matplotlib.pylab as plt
>>> import itertools
>>> images = ag.io.load_example('mnist')

Now, let us say we want to exact patches from the these, where each patch has at least some activity.

>>> gen = dd.image.extract_patches(images, (5, 5))
>>> gen = (x for x in gen if x.mean() > 0.1)
>>> patches = np.array(list(itertools.islice(gen, 25)))
>>> patches.shape
(25, 5, 5)
>>> dd.plot.images(patches)
>>> plt.show()
deepdish.image.integrate(ii, r0, c0, r1, c1)[source]

Use an integral image to integrate over a given window.

Parameters:
  • ii (ndarray) – Integral image.
  • c0 (r0,) – Top-left corner of block to be summed.
  • c1 (r1,) – Bottom-right corner of block to be summed.
Returns:

S – Integral (sum) over the given window.

Return type:

int

deepdish.image.load(path, dtype=<Mock id='140454313906448'>)[source]

Loads an image from file.

Parameters:
  • path (str) – Path to image file.
  • dtype (np.dtype) – Defaults to np.float64, which means the image will be returned as a float with values between 0 and 1. If np.uint8 is specified, the values will be between 0 and 255 and no conversion cost will be incurred.
deepdish.image.offset(img, offset, fill_value=0)[source]

Moves the contents of image without changing the image size. The missing values are given a specified fill value.

Parameters:
  • img (array) – Image.
  • offset ((vertical_offset, horizontal_offset)) – Tuple of length 2, specifying the offset along the two axes.
  • fill_value (dtype of img) – Fill value. Defaults to 0.
deepdish.image.resize_by_factor(im, factor)[source]

Resizes the image according to a factor. The image is pre-filtered with a Gaussian and then resampled with bilinear interpolation.

This function uses scikit-image and essentially combines its pyramid_reduce with pyramid_expand into one function.

Returns the same object if factor is 1, not a copy.

Parameters:
  • im (ndarray, ndim=2 or 3) – Image. Either 2D or 3D with 3 or 4 channels.
  • factor (float) – Resize factor, e.g. a factor of 0.5 will halve both sides.
deepdish.image.save(path, im)[source]

Saves an image to file.

If the image is type float, it will assume to have values in [0, 1].

Parameters:
  • path (str) – Path to which the image will be saved.
  • im (ndarray (image)) – Image.