unravel.image_tools.math module#

Use img_math (math) from UNRAVEL to perform mathematical operations on 3D images.

Inputs:
  • Supported formats: .czi, .nii.gz, .ome.tif series, .tif series, .h5, .zarr

Outputs:
  • Automatically determined by output extension (.nii.gz, .tif, or .zarr)

Operations:
  • Arithmetic: +, -, *, /, //, %, **

  • Comparison: ==, !=, >, >=, <, <=

  • Logical: and, or, xor, not

  • Other: abs_diff (absolute difference)

Thresholding and Masking:
  • Apply thresholding with --threshold (exclude values below) and/or --upper_thres (exclude values above)

  • Apply mask(s) with --masks

  • Use --bin to binarize (assign --True_val and --False_val); otherwise retain intensities within the mask or threshold range

Usage to add two .nii.gz images [and apply a mask]:#

img_math -i A.nii.gz B.nii.gz -n ‘+’ -o result.nii.gz [-mas mask.nii.gz]

Usage multiply three images and save as Zarr:#

img_math -i A.nii.gz B.nii.gz C.nii.gz -n <asterisk> -o result.zarr

Usage to binarize an image and set to 8 bit:#

img_math -i A.nii.gz -t 0.5 -b -o binarized.nii.gz -d uint8

unravel.image_tools.math.parse_args()[source]#
unravel.image_tools.math.apply_operation(image1, image2, operation)[source]#

Apply a mathematical operation to two ndarrays (images).

Supported operations include addition, subtraction, multiplication, division, and more.

Parameters:
  • image1 (np.ndarray) – First image.

  • image2 (np.ndarray) – Second image.

  • operation (str) – The operation to perform. Supported operations are: +, -, *, /, //, %, **, ==, !=, >, >=, <, <=, and, or, xor, not, abs_diff.

Notes

  • Element-wise comparison operations (==, !=, >, >=, <, <=) return a boolean array.

  • Logical operations (and, or, xor, not) also return a boolean array.

  • The abs_diff operation computes the absolute difference between the two images.

Returns:

Resulting image after applying the operation.

Return type:

np.ndarray

unravel.image_tools.math.apply_mask(image, mask, binarize=False, true_val=1, false_val=0)[source]#

Apply a binary mask to an image.

unravel.image_tools.math.threshold_image(image, lower_thr=None, upper_thr=None, binarize=False, true_val=1, false_val=0)[source]#

Apply lower and/or upper thresholding to an image.

unravel.image_tools.math.main()[source]#