{"id":9841,"library":"itk","title":"ITK (Insight Toolkit)","description":"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.","status":"active","version":"5.4.5","language":"en","source_language":"en","source_url":"https://github.com/InsightSoftwareConsortium/ITK","tags":["image processing","scientific computing","medical imaging","n-dimensional","computer vision","segmentation","registration"],"install":[{"cmd":"pip install itk","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"The primary way to import ITK functionality.","symbol":"itk","correct":"import itk"}],"quickstart":{"code":"import itk\nimport os\n\n# Load a test image (ensure itk.test_data is available or provide your own path)\n# Example: itk.test_data.dir() + '/BrainProtonDensitySlice.png'\n# Or download one, e.g., from https://itk.org/Doxygen/html/itkTestingData_2itkTestingData_8h-example.html\n\n# For demonstration, we'll try to find a suitable test image\ntry:\n    input_image_path = itk.test_data.dir() + '/BrainProtonDensitySlice.png'\nexcept AttributeError:\n    print(\"itk.test_data not found. Please provide a path to a test image.\")\n    # Fallback to a placeholder, won't run without a real image\n    input_image_path = \"path/to/your/image.png\" \n\nif not os.path.exists(input_image_path): # Ensure the path actually exists\n    print(f\"Warning: Test image not found at {input_image_path}. Please provide a valid image path to run this example.\")\n    # Create a dummy image for the rest of the code to parse if test data isn't there\n    # This part won't execute if os.path.exists is true or real path given\n    image = itk.Image[itk.F, 2].New()\n    image.SetRegions(itk.ImageRegion[2]([0,0], [10,10]))\n    image.Allocate()\n    print(\"Using a dummy image for processing.\")\nelse:\n    # Read the image, specifying the pixel type (itk.F for float)\n    image = itk.imread(input_image_path, itk.F)\n    print(f\"Read image with dimensions: {image.GetLargestPossibleRegion().GetSize()} and pixel type: {image.GetPixelID()}')\")\n\n# Define the image type (float, 2D) for the filter\npixel_type = itk.F\nimage_type = itk.Image[pixel_type, 2]\n\n# Create a Gaussian filter instance\ngamma_filter = itk.GammaImageAdaptor[image_type, pixel_type].New()\n\n# Set the input image\ngamma_filter.SetInputImage(image)\n\n# Apply a gamma correction (e.g., gamma = 0.5)\ngamma_filter.SetGamma(0.5)\n\n# Update the filter to execute\ngamma_filter.Update()\n\noutput_image = gamma_filter.GetOutput()\n\n# Write the output image\noutput_file_name = \"gamma_corrected_output.png\"\n# Ensure a real image was processed before trying to write\nif os.path.exists(input_image_path):\n    itk.imwrite(output_image, output_file_name)\n    print(f\"Processed image and saved to {output_file_name}\")\nelse:\n    print(\"Skipped writing output because a real input image was not processed.\")","lang":"python","description":"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."},"warnings":[{"fix":"Review the official ITK 6.0 documentation and migration guides for updated CMake usage and build configurations if you are building ITK or custom C++ modules.","message":"ITK 6.0 introduces major build system changes with modern CMake module targets. While Python users primarily rely on pre-built wheels, this is a breaking change for those building ITK from source or developing custom C++ modules that link ITK.","severity":"breaking","affected_versions":"6.0 and later"},{"fix":"Ensure your C++ development environment is configured to use C++17 or newer. For Python users, this is typically handled by the pre-built `itk` wheels.","message":"ITK 5.4 and later versions (including 6.0) require a C++17 compliant compiler. This primarily affects users building ITK from source or developing custom C++ modules.","severity":"breaking","affected_versions":"5.4 and later"},{"fix":"Explicitly specify the `PixelType` and `Dimension` when creating or reading images (`itk.imread(path, itk.F)`) and ensure compatibility with the filters being used. Use `itk.CastImageFilter` when type conversion is necessary.","message":"ITK utilizes strong typing for image objects (e.g., `itk.Image[itk.F, 3]` for a 3D float image). Incorrect pixel types or dimensions when passing images to filters can lead to `TypeError` or unexpected behavior.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Consider using streaming filters (`itk.StreamingImageFilter`), processing images in chunks, or downsampling images where appropriate. Monitor memory usage and ensure sufficient RAM is available.","message":"Processing very large N-dimensional images (high resolution, many slices, multiple channels) with ITK can be memory-intensive, potentially leading to `MemoryError` or system performance issues.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Run `pip install itk` to install the library.","cause":"The `itk` Python package is not installed in your current environment.","error":"ModuleNotFoundError: No module named 'itk'"},{"fix":"Verify the image file path and filename. Check the file's integrity and format. Ensure ITK has been compiled with support for the specific image format (e.g., DICOM, JPEG2000, TIFF).","cause":"The specified image file path is incorrect, the file does not exist, the file is corrupt, or ITK does not support the image format (or the necessary ImageIO module is not loaded/compiled).","error":"RuntimeError: itk::ExceptionObject: ... Could not read image file ... No ImageIO loading filter is able to read the given file"},{"fix":"Review the ITK documentation for the specific class or function you're using. Pay close attention to the required pixel type and dimension arguments (e.g., `itk.Image[itk.F, 2].New()`).","cause":"You are attempting to create an `itk.Image` or use an ITK function with arguments that do not match any of its overloaded C++ constructors or expected Python types.","error":"TypeError: itk.Image: No constructor matches the arguments given: ..."},{"fix":"Ensure the input image's pixel type and dimension exactly match the filter's template parameters. Use `itk.CastImageFilter` to explicitly convert the image to the required type (e.g., `itk.CastImageFilter[InputImageType, OutputImageType].New()`) before applying the filter.","cause":"This specific `TypeError` (or similar for other filters) indicates an incompatibility between the input image's pixel type/dimension and what the filter expects. Here, `int_itkImage_int_itkImage` implies an integer image, but a float image was provided.","error":"TypeError: in method 'CastImageFilter_New_int_itkImage_int_itkImage', argument 1 of type 'itk::Image<float, 2u> *'"}]}