Usage Examples

FastGeodis package includes a number of examples that demonstrate its usage for 2D and 3D data use-cases. Here we provide details of these examples and how to setup and run them.

Simplest Examples

For a given 2D image, FastGeodis can be used to get Geodesic distance transform as:

device = "cpu"
image = np.asarray(Image.open("data/img2d.png"), np.float32)

image_pt = torch.from_numpy(image).unsqueeze_(0).unsqueeze_(0)
image_pt = image_pt.to(device)
mask_pt = torch.ones_like(image_pt)
mask_pt[..., 100, 100] = 0

v = 1e10
# lamb = 0.0 (Euclidean) or 1.0 (Geodesic) or (0.0, 1.0) (mixture)
lamb = 1.0
iterations = 2
geodesic_dist = FastGeodis.generalised_geodesic2d(
    image_pt, mask_pt, v, lamb, iterations
)

For a given 3D image volume, FastGeodis can be used to get Geodesic distance transform as:

device = "cuda" if torch.cuda.is_available() else "cpu"

image_sitk = sitk.ReadImage("data/img3d.nii.gz")
image = sitk.GetArrayFromImage(image_sitk)
spacing_raw = image_sitk.GetSpacing()[::-1]

image = np.asarray(image, np.float32)
mask = np.zeros_like(image, np.float32)
mask[30, 120, 160] = 1

image_pt = torch.from_numpy(image).unsqueeze_(0).unsqueeze_(0)
mask_pt = torch.from_numpy(1 - mask.astype(np.float32)).unsqueeze_(0).unsqueeze_(0)

image_pt = image_pt.to(device)
mask_pt = mask_pt.to(device)

v = 1e10
iterations = 2
# lamb = 0.0 (Euclidean) or 1.0 (Geodesic) or (0.0, 1.0) (mixture)
lamb = 1.0
geodesic_dist = FastGeodis.generalised_geodesic3d(
    image_pt, mask_pt, spacing, v, lamb, iterations
)

To access complete examples, refer to Simple 2D/3D examples in table below.

Note: the above example execute using CPU with device = "cpu". To change execution device to GPU use device="cuda".

Available Examples

A number of examples are provided, which can be found in ./samples folder. The examples are provided as local python (.py) files, local Jupyter (.ipynb) notebooks and cloud Colab (.ipynb) notebooks.

Running Examples Locally

To run locally, clone github repository and navigate to ./samples directory as:

git clone https://github.com/masadcv/FastGeodis
cd FastGeodis/samples

and run an example as:

python demo2d.py

Running Examples in Colab

To setup and run Colab notebooks in the cloud, follow the Colab links below.

List of Example Scripts

The table below gives an exhaustive list of all available demo examples. It includes relevant links to Python as well as Colab versions of the same demo.

Description

Python Link

Colab Link

Simple 2D Geodesic & Euclidean Distance

SimpleDemo2dPy

SimpleDemo2dColab

Simple Signed 2D Geodesic & Euclidean Distance

SimpleDemo2dSignedPy

SimpleDemo2dSignedColab

Simple 3D Geodesic & Euclidean Distance

SimpleDemo3dPy

SimpleDemo3dColab

Simple Signed 3D Geodesic & Euclidean Distance

SimpleDemo3dSignedPy

SimpleDemo3dSignedColab

2D Geodesic & Euclidean Distance

Demo2dPy

Demo2dColab

2D Signed Geodesic & Euclidean Distance

Demo2dSignedPy

Demo2dSignedColab

3D Geodesic & Euclidean Distance

Demo3dPy

Demo3dColab

3D Signed Geodesic & Euclidean Distance

Demo3dSignedPy

Demo3dSignedColab

2D 2D GSF Segmentation Smoothing

DemoGSF2dPy

DemoGSF2dColab