This Python script applies 3D Canny edge detection to a NIfTI image with optional Gaussian smoothing, thresholding, and the ability to combine edges from all axes. It also supports auto-completion for command-line arguments.
- Python 3
nibabel
scikit-image
argparse
argcomplete
You can install the required packages using pip:
pip install nibabel scikit-image argparse argcomplete
input_filepath
: Path to the input NIfTI file.output_filepath
: Path to save the output NIfTI file.--sigma
: Sigma value for Canny edge detection (default: 1.0).--gaussian_sigma
: Sigma value for Gaussian filter (default: 1.0).--threshold
: Threshold value to remove data below this value (default: 0.0).--view
: Open the OrthoSlicer3D viewer to visualize the result.
python mri_canny_filter.py path/to/your/input_image.nii.gz path/to/your/output_image.nii.gz --sigma 2.0 --gaussian_sigma 1.5 --threshold 0.1 --view
To enable tab completion for the script's arguments, follow these steps:
-
Install
argcomplete
if you haven't already:pip install argcomplete
-
Add the following line to your
.bashrc
or.bash_profile
:eval "$(register-python-argcomplete mri_canny_filter.py)"
Replace
script_name.py
with the actual name of your script. -
Source your shell configuration file to apply the changes:
source ~/.bashrc # or source ~/.bash_profile
Alternatively, you can enable argcomplete
for the current shell session with:
activate-global-python-argcomplete --dest=- | source /dev/stdin
- Thresholding: The script applies a threshold to the input data, setting all values below the threshold to zero.
- Gaussian Smoothing: A Gaussian filter is applied to smooth the data and reduce noise.
- Canny Edge Detection: Canny edge detection is performed along each axis (sagittal, coronal, axial) of the 3D image.
- Combining Edges: The edges detected along each axis are combined using
np.maximum
to form a single edge-detected volume. - Saving the Result: The combined edges are saved as a new NIfTI image.
- OrthoSlicer3D Viewer: If the
--view
flag is set, the OrthoSlicer3D viewer is opened to visualize the result.