spymicmac.image#

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

radiometric adjustment#

spymicmac.image.balance_image(img, clip_limit=0.01)[source]#

Apply contrast-limited adaptive histogram equalization (CLAHE) on an image.

Parameters:
  • img (ndarray[tuple[Any, ...], dtype[_ScalarT]]) – the image to balance.

  • clip_limit (float) – Clipping limit, normalized between 0 and 1 (higher values give more contrast).

Returns:

img_filt – the balanced, filtered image.

Return type:

ndarray[tuple[Any, …], dtype[_ScalarT]]

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 | None) – a mask value to use when filtering the image.

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

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

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

  • disksize (int) – the filter disk size (input to skimage.morphology.disk)

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

Returns:

enhanced – the contrast-enhanced image.

Return type:

ndarray[tuple[Any, …], dtype[_ScalarT]]

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

Remove horizontal (or vertical) stripes from an image.

Parameters:
  • img (ndarray[tuple[Any, ...], dtype[_ScalarT]]) – the image to remove stripes from.

  • dtype (DTypeLike) – 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.

Return type:

ndarray[tuple[Any, …], dtype[_ScalarT]]

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 (ndarray[tuple[Any, ...], dtype[_ScalarT]]) – the image to stretch.

  • scale (tuple[float, float]) – the minimum and maximum quantile to stretch to. Defaults to the minimum/maximum values of the image.

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

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

  • outtype (DTypeLike) – the numpy datatype to return the stretched image as.

  • mask (ndarray[tuple[Any, ...], dtype[_ScalarT]] | None) – a mask of pixels to ignore when calculating quantiles.

Returns:

the stretched image.

Return type:

ndarray[tuple[Any, …], dtype[_ScalarT]]

combining/mosaicking#

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)

Return type:

None

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 (ndarray[tuple[Any, ...], dtype[_ScalarT]]) – the left half of the image

  • right (ndarray[tuple[Any, ...], dtype[_ScalarT]]) – the right half of the image

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

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

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

  • trim (int | None) – the amount to trim the right side of the image by. Default is no trimming.

Returns:

the joined image.

Return type:

ndarray[tuple[Any, …], dtype[_ScalarT]]

miscellaneous#

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

Find the rough location of an image frame/border.

Parameters:
  • img (ndarray[tuple[Any, ...], dtype[_ScalarT]]) – the image to find a border for

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

Returns:

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

Return type:

tuple[float, float, float, float]

spymicmac.image.highpass_filter(img)[source]#

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

Parameters:

img (ndarray[tuple[Any, ...], dtype[_ScalarT]]) – the image to filter.

Returns:

the highpass-filtered image

Return type:

ndarray[tuple[Any, …], dtype[_ScalarT]]

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 (ndarray[tuple[Any, ...], dtype[_ScalarT]]) – the image to create a mask for.

  • mult_value (int | float) – the value indicating a non-masked value

  • erode (int) – the size of the erosion operation to apply

  • mask_value (int) – the value to mask in the image

Returns:

the binary mask.

Return type:

ndarray[tuple[Any, …], dtype[_ScalarT]]

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

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

Parameters:
  • img (ndarray[tuple[Any, ...], dtype[_ScalarT]]) – the image to split

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

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

Returns:

  • blocks – a list of the image blocks created

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

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

Return type:

tuple[list, list, list]