unravel.image_tools.math module#

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

Inputs:
  • Any of the following formats: .czi, .nii.gz, .ome.tif series, .tif series, .h5, .zarr

Outputs:
  • .nii.gz, .tif series, or .zarr depending on the output path extension

Supported Operations:
  • Use with the -n or --operation flag:

  • Arithmetic: +, -, <asterisk>, /, //, %, <asterisk><asterisk>

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

  • Logical: and, or, xor, not

  • Other: abs_diff (absolute difference)

Thresholding:

Optionally apply a threshold (lower and/or upper) to the result using: - –threshold : Lower threshold - –upper_thres : Upper threshold - –True_val : Value to assign if the condition is met (default: 1) - –False_val : Value to assign if the condition is not met (default: 0)

Usage to add two images and binarize the result:#

img_math -i A.nii.gz B.nii.gz -n ‘+’ -t 0.5 -o result.nii.gz -r A.nii.gz -d uint8

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 a single image and set to 8 bit:#

img_math -i A.nii.gz -t 0.5 -o binarized.nii.gz -r A.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.threshold_image(image, lower_thr=None, upper_thr=None, true_val=1, false_val=0)[source]#

Apply lower and/or upper thresholding to an image.

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