ITK (Insight Toolkit)

5.4.5 · active · verified Fri Apr 17

ITK (Insight Toolkit) is an actively developed, open-source C++ library with Python bindings for N-dimensional scientific image analysis, processing, segmentation, and registration. It's widely used in medical imaging and computer vision research. The current stable version is 5.4.5, with frequent maintenance releases and major versions (e.g., 6.0) released periodically after beta testing.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to read an image, apply a basic image filter (Gamma correction), and save the result. It highlights ITK's strong typing with `itk.Image[pixel_type, dimension]` and the common `itk.imread`/`itk.imwrite` functions. A test image path is used for a runnable example.

import itk
import os

# Load a test image (ensure itk.test_data is available or provide your own path)
# Example: itk.test_data.dir() + '/BrainProtonDensitySlice.png'
# Or download one, e.g., from https://itk.org/Doxygen/html/itkTestingData_2itkTestingData_8h-example.html

# For demonstration, we'll try to find a suitable test image
try:
    input_image_path = itk.test_data.dir() + '/BrainProtonDensitySlice.png'
except AttributeError:
    print("itk.test_data not found. Please provide a path to a test image.")
    # Fallback to a placeholder, won't run without a real image
    input_image_path = "path/to/your/image.png" 

if not os.path.exists(input_image_path): # Ensure the path actually exists
    print(f"Warning: Test image not found at {input_image_path}. Please provide a valid image path to run this example.")
    # Create a dummy image for the rest of the code to parse if test data isn't there
    # This part won't execute if os.path.exists is true or real path given
    image = itk.Image[itk.F, 2].New()
    image.SetRegions(itk.ImageRegion[2]([0,0], [10,10]))
    image.Allocate()
    print("Using a dummy image for processing.")
else:
    # Read the image, specifying the pixel type (itk.F for float)
    image = itk.imread(input_image_path, itk.F)
    print(f"Read image with dimensions: {image.GetLargestPossibleRegion().GetSize()} and pixel type: {image.GetPixelID()}')")

# Define the image type (float, 2D) for the filter
pixel_type = itk.F
image_type = itk.Image[pixel_type, 2]

# Create a Gaussian filter instance
gamma_filter = itk.GammaImageAdaptor[image_type, pixel_type].New()

# Set the input image
gamma_filter.SetInputImage(image)

# Apply a gamma correction (e.g., gamma = 0.5)
gamma_filter.SetGamma(0.5)

# Update the filter to execute
gamma_filter.Update()

output_image = gamma_filter.GetOutput()

# Write the output image
output_file_name = "gamma_corrected_output.png"
# Ensure a real image was processed before trying to write
if os.path.exists(input_image_path):
    itk.imwrite(output_image, output_file_name)
    print(f"Processed image and saved to {output_file_name}")
else:
    print("Skipped writing output because a real input image was not processed.")

view raw JSON →