Miscellaneous Module
nDTomo Utility Tools
This module contains a broad collection of general-purpose functions and tools used across the nDTomo package. These include image and volume processing utilities, geometric transformations, interpolation routines, masking, thresholding, and auxiliary tools for rebinning, visualization, and user-guided input.
Key functionalities: - Circular and binary masking for 2D/3D data - Coordinate transformations (Cartesian <-> Polar) - Cropping, normalization, and padding of images and volumes - Interpolation in 1D, 2D, and 3D - Image segmentation, thresholding, and morphological filling - Matrix size harmonization (even sizing, padding) - Spectral registration and rebinning - Interactive shape fitting (circle/ellipse) - Miscellaneous utility functions (e.g., RGB to grayscale)
These tools serve as foundational operations for pre-processing, simulation, and reconstruction workflows in X-ray chemical tomography.
@author: Antony Vamvakeros
- nDTomo.methods.misc.calculate_center_of_mass(spectrum)[source]
Calculate the center of mass of a spectrum with subpixel resolution.
- nDTomo.methods.misc.cart2pol(x, y)[source]
Convert Cartesian (x,y) coordinates to polar coordinates (rho, phi).
- nDTomo.methods.misc.cart2polim(im, thpix=1024, rpix=1024, ofs=0)[source]
Convert an image from Cartesian to polar coordinates.
- Parameters:
- Returns:
Image in polar coordinates.
- Return type:
numpy.ndarray
- nDTomo.methods.misc.cirmask(im, npx=0)[source]
Apply a circular mask to the image/volume.
- Parameters:
im (numpy.ndarray) – Input image or volume.
npx (int) – Number of pixels to exclude from the circular mask (default: 0).
- Returns:
Image or volume with a circular mask applied.
- Return type:
numpy.ndarray
- nDTomo.methods.misc.compare_spectra(reference_spectrum, translated_spectrum, pixel_range, resolution)[source]
Compare two spectra by translating the second spectrum with subpixel resolution using interpolation.
- Parameters:
reference_spectrum (list or numpy.ndarray) – A list or numpy array representing the reference spectrum.
translated_spectrum (list or numpy.ndarray) – A list or numpy array representing the spectrum to be translated.
pixel_range (tuple) – The range of subpixel translation in pixels (e.g., (-0.5, 0.5)).
resolution (float) – The resolution of subpixel translation (e.g., 0.1).
- Returns:
The translated spectrum aligned with the reference spectrum.
- Return type:
numpy.ndarray
- nDTomo.methods.misc.create_circle(npix_im=512, r0=128)[source]
Create a circular mask for a squared image.
- nDTomo.methods.misc.create_cirmask(npix_im=512, npx=0)[source]
Create a circular mask for a squared image.
- nDTomo.methods.misc.crop_ctimage(im, plot=False)[source]
Crop a CT image using a square inside the reconstruction circle. :param im: CT image. :type im: numpy.ndarray :param plot: Flag to display a plot of the cropped image (default: False). :type plot: bool
- Returns:
Cropped CT image.
- Return type:
numpy.ndarray
- nDTomo.methods.misc.crop_ctvolume(vol, plot=False)[source]
Crop a CT volume using a square inside the reconstruction circle.
- Parameters:
vol (numpy.ndarray) – Input CT volume.
plot (bool) – Flag to display a plot of the cropped volume (default: False).
- Returns:
Cropped CT volume.
- Return type:
numpy.ndarray
- nDTomo.methods.misc.crop_image(im, thr=None, norm=False, plot=False, inds=None)[source]
Crop an image.
- Parameters:
im (numpy.ndarray) – Input image.
thr (float) – Threshold value to apply for cropping (default: None).
norm (bool) – Flag to normalize the image (default: False).
plot (bool) – Flag to display plots of the original and cropped images (default: False).
inds (list) – List of indices for cropping (default: None).
- Returns:
Cropped image. list: List of indices used for cropping.
- Return type:
numpy.ndarray
- nDTomo.methods.misc.crop_volume(vol, thr=None, plot=False, dtype='float32', inds=None)[source]
Crop a data volume using the average image along the third dimension.
- Parameters:
vol (numpy.ndarray) – Input data volume.
thr (float) – Threshold value to apply for cropping (default: None).
plot (bool) – Flag to display a plot of the cropped volume (default: False).
dtype (str) – Data type of the cropped volume (default: ‘float32’).
inds (list) – List of indices for cropping (default: None).
- Returns:
Cropped data volume.
- Return type:
numpy.ndarray
- nDTomo.methods.misc.crop_volume_getinds(vol, thr=None)[source]
Crop a data volume using the average image along the third dimension and return the indices.
- nDTomo.methods.misc.even_idx(a)[source]
Return all even elements from a matrix. :param a: Input matrix. :type a: numpy.ndarray
- Returns:
Array containing only the even elements.
- Return type:
numpy.ndarray
- nDTomo.methods.misc.find_first_neighbors_2D(mat, r, c)[source]
Find the first neighbor elements that are non-zero in a binary 2D matrix.
- nDTomo.methods.misc.fit_shape_to_points(image, shape='circle')[source]
Fits a circle or ellipse to five user-selected points on an image and overlays it on the original image.
- Parameters:
image (numpy.ndarray) – 2D numpy array representing the image.
shape (str) – Shape to fit. Options: ‘circle’ (default) or ‘ellipse’.
- Returns:
None
- nDTomo.methods.misc.image_segm(im, thr, norm=False, plot=False)[source]
Perform simple threshold-based image segmentation.
- nDTomo.methods.misc.interpvol(vol, xold, xnew)[source]
Perform linear interpolation on a 3D matrix along the spectral/height dimension.
- Parameters:
vol (numpy.ndarray) – Input 3D matrix.
xold (numpy.ndarray) – Original x-coordinates along the spectral/height dimension.
xnew (numpy.ndarray) – New x-coordinates for interpolation.
- Returns:
Interpolated 3D matrix.
- Return type:
numpy.ndarray
- nDTomo.methods.misc.make_matrix_even(mat)[source]
Make the dimensions of a 1D, 2D, or 3D array have even sizes.
- Parameters:
mat (numpy.ndarray) – Input array.
- Returns:
Array with even dimensions.
- Return type:
numpy.ndarray
- nDTomo.methods.misc.mask_thr(vol, thr, roi=None, fignum=1)[source]
Apply a threshold-based mask to a 2D or 3D volume.
- Parameters:
- Returns:
Binary mask based on the threshold.
- Return type:
numpy.ndarray
- nDTomo.methods.misc.maskvolume(vol, msk)[source]
Apply a mask to a 3D array.
- Parameters:
vol (numpy.ndarray) – Input 3D array.
msk (numpy.ndarray) – Mask array.
- Returns:
Resulting masked 3D array.
- Return type:
numpy.ndarray
- nDTomo.methods.misc.nan_to_number(array, val=0)[source]
Replace NaN values in an array with a specified number.
- Parameters:
array (numpy.ndarray) – Input array.
val (float) – Value to replace NaN with. Default is 0.
- Returns:
Array with NaN values replaced by the specified number.
- Return type:
numpy.ndarray
- nDTomo.methods.misc.ndtomopath()[source]
Returns the absolute path to the installed nDTomo package directory.
This is useful for locating internal resources such as ‘examples/’ that now live inside the package.
- Returns:
Absolute path to the nDTomo package.
- Return type:
- nDTomo.methods.misc.normvol(vol)[source]
Normalize a 3D matrix along the spectral/height dimension.
- Parameters:
vol (numpy.ndarray) – Input 3D matrix.
- Returns:
Normalized 3D matrix.
- Return type:
numpy.ndarray
- nDTomo.methods.misc.number_to_nan(array, val=0)[source]
Replace a specified number in an array with NaN values.
- Parameters:
array (numpy.ndarray) – Input array.
val (float) – Value to replace with NaN. Default is 0.
- Returns:
Array with the specified number replaced by NaN values.
- Return type:
numpy.ndarray
- nDTomo.methods.misc.odd_idx(a)[source]
Return all odd elements from a matrix. :param a: Input matrix. :type a: numpy.ndarray
- Returns:
Array containing only the odd elements.
- Return type:
numpy.ndarray
- nDTomo.methods.misc.pad_zeros(im1, im2, dtype)[source]
Pad two images with zeros to match their sizes.
- nDTomo.methods.misc.pad_zeros_svol(vol, dims, dtype)[source]
Pad a volume with zeros to match a specific size.
- nDTomo.methods.misc.pad_zeros_vol(vol1, vol2, dtype)[source]
Pad two volumes with zeros to match their sizes.
- Parameters:
vol1 (numpy.ndarray) – First volume.
vol2 (numpy.ndarray) – Second volume.
dtype (str) – Data type for the padded volumes.
- Returns:
Padded vol1. numpy.ndarray: Padded vol2.
- Return type:
numpy.ndarray
- nDTomo.methods.misc.pol2cart(phi, rho)[source]
Convert polar (rho, phi) coordinates to Cartesian coordinates (x, y).
- nDTomo.methods.misc.pol2cartim(imp, im_size=None, thpix=1024, rpix=1024, ofs=0)[source]
Convert an image from polar to Cartesian coordinates.
- Parameters:
imp (numpy.ndarray) – 2D array corresponding to the polar-transformed image.
im_size (list) – List containing the two dimensions of the image with Cartesian coordinates (default: None).
thpix (int) – Number of bins for the azimuthal range (default: 1024).
rpix (int) – Number of bins for the r distance range (default: 1024).
ofs (float) – Angular offset (default: 0).
- Returns:
Image in Cartesian coordinates.
- Return type:
numpy.ndarray
- nDTomo.methods.misc.rebin1d(x, y, x_new)[source]
Rebin 1D data from original x-axis x to a new x-axis x_new using linear interpolation with weighted averaging.
This function redistributes the values in y (defined on axis x) onto a new set of points x_new by performing distance-weighted averaging to the nearest two neighbors in x_new.
For points in x that lie outside the bounds of x_new, the value of y is assigned directly to the nearest boundary bin.
- Parameters:
x (array-like of shape (N,)) – Original x-axis coordinates.
y (array-like of shape (N,)) – Data values corresponding to x.
x_new (array-like of shape (M,)) – New x-axis grid to which y values will be rebinned.
- Returns:
rebin_data – Rebinned data values corresponding to x_new.
- Return type:
ndarray of shape (M,)
Notes
The method conserves total weight by proportionally distributing each y[i] value between the two closest points in x_new.
This approach is useful for spectral regridding or when combining datasets sampled on different 1D grids.