spymicmac.image

spymicmac.image is a collection of tools for working with images.

spymicmac.image.balance_image(img)[source]

Apply contrast-limited adaptive histogram equalization (CLAHE) on an image, then apply a de-noising filter.

Parameters:

img (array-like) – the image to balance.

Returns:

img_filt (array-like) – the balanced, filtered image.

spymicmac.image.contrast_enhance(fn_img, mask_value=None, qmin=0.02, qmax=0.98, gamma=1.25, disksize=3, imgmin=0)[source]

Enhance image contrast in a three-step process. First, the image is processed with a median filter to reduce noise. Next, a linear contrast stretch is applied, and finally, a gamma adjustment is applied.

Parameters:
  • fn_img (str) – the image filename.

  • mask_value (int|float) – a mask value to use when filtering the image.

  • qmin (float) – the minimum quantile to use for the linear contrast stretch (default: 0.02)

  • qmax (float) – the maximum quantile to use for the linear contrast stretch (default: 0.98)

  • gamma (float) – the value to use for the gamma adjustment

  • disksize (int) – the filter disk size (input to skimage.morphology.disk; default: 3)

  • imgmin (int|float) – the minimum value in the output image (default: 0)

Returns:

enhanced (array-like) – the contrast-enhanced image.

spymicmac.image.get_parts_list(im_pattern)[source]

Find all of the parts of a scanned image that match a given filename pattern

Parameters:

im_pattern (str) – the image pattern to match

Returns:

parts_list (list) – a list of all parts of the image that match the pattern.

spymicmac.image.get_rough_frame(img, fact=10)[source]

Find the rough location of an image frame/border.

Parameters:
  • img (array-like) – the image to find a border for

  • fact (int) – the scaling factor for the low-resolution image (default: 10)

Returns:

xmin, xmax, ymin, ymax (float) – the left, right, top, and bottom indices for the rough border.

spymicmac.image.highpass_filter(img)[source]

Subtract a low-pass from an image, to return a highpass filter.

Parameters:

img (array-like) – the image to filter.

Returns:

highpass (array-like) – the highpass-filtered image

spymicmac.image.join_halves(left, right, overlap, block_size=None, blend=True, trim=None)[source]

Join two halves of a scanned image together.

Parameters:
  • left (array-like) – the left half of the image

  • right (array-like) – the right half of the image

  • overlap (int) – the amount of overlap, in pixels, between the two halves.

  • block_size (int) – the number of rows each sub-block should cover. Defaults to overlap.

  • blend (bool) – apply a linear blend between the two scanned halves (default: True).

  • trim (int) – the amount to trim the right side of the image by. (default: None).

spymicmac.image.join_hexagon(im_pattern, overlap=2000, block_size=None, blend=True, is_reversed=False)[source]

Join multiple parts of a scanned image.

Parameters:
  • im_pattern (str) – the base name of the image to use (e.g., DZB1216-500280L002001).

  • overlap (int) – the overlap, in pixels, between the image parts.

  • block_size (int) – the number of rows each sub-block should cover. Defaults to overlap.

  • blend (bool) – apply a linear blend between the two scanned halves (default: True).

  • is_reversed (bool) – parts are in reversed order (i.e., part b is the left part, part a is the right part)

spymicmac.image.make_binary_mask(img, mult_value=255, erode=0, mask_value=0)[source]

Create a binary mask for an image based on a given mask value. Values equal to mask_value will be given a value of 0 in the mask, while all other values will be set equal to mult_value.

Parameters:
  • img (array-like) – the image to create a mask for.

  • mult_value (int|float) – the value indicating a non-masked value (default: 255).

  • erode (int) – the size of the erosion operation to apply (default: 0).

  • mask_value (int) – the value to mask in the image (default: 0).

Returns:

mask (array-like) the binary mask.

spymicmac.image.nanmedian_filter(img, **kwargs)[source]

Calculate a multi-dimensional median filter that respects NaN values and masked arrays.

Parameters:
  • img (array-like) – image on which to calculate the median filter

  • kwargs – additional arguments to ndimage.generic_filter Note that either size or footprint must be defined. size gives the shape that is taken from the input array, at every element position, to define the input to the filter function. footprint is a boolean array that specifies (implicitly) a shape, but also which of the elements within this shape will get passed to the filter function. Thus size=(n,m) is equivalent to footprint=np.ones((n,m)). We adjust size to the number of dimensions of the input array, so that, if the input array is shape (10,10,10), and size is 2, then the actual size used is (2,2,2).

Returns filtered:

Filtered array of same shape as input.

spymicmac.image.remove_scanner_stripes(img, dtype=<class 'numpy.uint8'>, scan_axis=1)[source]

Remove horizontal (or vertical) stripes from an image.

Parameters:
  • img (array-like) – the image to remove stripes from.

  • dtype (numpy.dtype) – the original datatype of the image.

  • scan_axis (int) – the axis corresponding to the direction of the stripes. A scan_axis of 1 corresponds to horizontal stripes (the default), while a scan_axis of 0 corresponds to vertical stripes.

Returns:

destriped: the original image with the stripes (mostly) removed.

spymicmac.image.splitter(img, nblocks, overlap=0)[source]

Split an image into (m, n) blocks with a given overlap.

Parameters:
  • img (array-like) – the image to split

  • nblocks (tuple) – the number of blocks to create along each axis (m, n)

  • overlap (int) – the number of pixels to overlap each block. (default: 0)

Returns:

  • blocks (list) – a list of the image blocks created

  • top_inds (list) – a list of the original row index of the top edge of each block

  • left_inds (list) – a list of the original column index of the left edge of each block

spymicmac.image.stretch_image(img, scale=(0.0, 1.0), mult=255, imgmin=0, outtype=<class 'numpy.uint8'>, mask=None)[source]

Apply a linear stretch to an image by clipping and stretching to quantiles.

Parameters:
  • img (array-like) – the image to stretch.

  • scale (tuple) – the minimum and maximum quantile to stretch to. (default: (0, 1) - the minimum/maximum values of the image)

  • mult (int|float) – a multiplier to scale the result to. (default: 255)

  • imgmin (int|float) – the minimum value in the output image (default: 0)

  • outtype (numpy.dtype) – the numpy datatype to return the stretched image as. (default: np.uint8)

  • mask (array-like) – a mask of pixels to ignore when calculating quantiles.

Returns:

stretched (array-like) – the stretched image.