<jupyter_start><jupyter_text>Images SimpleITK conventions:Image access is in x,y,z order, image.GetPixel(x,y,z) or image[x,y,z], with zero based indexing.If the output of an ITK filter has non-zero starting index, then the index will be set to 0, and the origin adjusted accordingly.The unique feature of SimpleITK (derived from ITK) as a toolkit for image manipulation and analysis is that it views images as physical objects occupying a bounded region in physical space. In addition images can have different spacing between pixels along each axis, and the axes are not necessarily orthogonal. The following figure illustrates these concepts.  Pixel TypesThe pixel type is represented as an enumerated type. The following is a table of the enumerated list.  sitkUInt8Unsigned 8 bit integer  sitkInt8Signed 8 bit integer  sitkUInt16Unsigned 16 bit integer  sitkInt16Signed 16 bit integer  sitkUInt32Unsigned 32 bit integer  sitkInt32Signed 32 bit integer  sitkUInt64Unsigned 64 bit integer  sitkInt64Signed 64 bit integer  sitkFloat3232 bit float  sitkFloat6464 bit float  sitkComplexFloat32complex number of 32 bit float  sitkComplexFloat64complex number of 64 bit float  sitkVectorUInt8Multi-component of unsigned 8 bit integer  sitkVectorInt8Multi-component of signed 8 bit integer  sitkVectorUInt16Multi-component of unsigned 16 bit integer  sitkVectorInt16Multi-component of signed 16 bit integer  sitkVectorUInt32Multi-component of unsigned 32 bit integer  sitkVectorInt32Multi-component of signed 32 bit integer  sitkVectorUInt64Multi-component of unsigned 64 bit integer  sitkVectorInt64Multi-component of signed 64 bit integer  sitkVectorFloat32Multi-component of 32 bit float  sitkVectorFloat64Multi-component of 64 bit float  sitkLabelUInt8RLE label of unsigned 8 bit integers  sitkLabelUInt16RLE label of unsigned 16 bit integers  sitkLabelUInt32RLE label of unsigned 32 bit integers  sitkLabelUInt64RLE label of unsigned 64 bit integersThere is also `sitkUnknown`, which is used for undefined or erroneous pixel ID's. It has a value of -1.The 64-bit integer types are not available on all distributions. When not available the value is `sitkUnknown`.<jupyter_code>import SimpleITK as sitk

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

from ipywidgets import interact, fixed
import os

OUTPUT_DIR = 'Output'

# Utility method that either downloads data from the Girder repository or
# if already downloaded returns the file name for reading from disk (cached data).
%run update_path_to_download_script
from downloaddata import fetch_data as fdata<jupyter_output><empty_output><jupyter_text>Load your first image and display it<jupyter_code>logo = sitk.ReadImage(fdata('SimpleITK.jpg'))

plt.imshow(sitk.GetArrayViewFromImage(logo))
plt.axis('off');<jupyter_output><empty_output><jupyter_text>Image ConstructionThere are a variety of ways to create an image. The following components are required for a complete definition of an image:Pixel type [fixed on creation, no default]: unsigned 32 bit integer, sitkVectorUInt8, etc., see list above. Sizes [fixed on creation, no default]: number of pixels/voxels in each dimension. This quantity implicitly defines the image dimension. Origin [default is zero]: coordinates of the pixel/voxel with index (0,0,0) in physical units (i.e. mm). Spacing [default is one]: Distance between adjacent pixels/voxels in each dimension given in physical units. Direction matrix [default is identity]: mapping, rotation, between direction of the pixel/voxel axes and physical directions.Initial pixel/voxel values are set to zero.<jupyter_code>image_3D = sitk.Image(256, 128, 64, sitk.sitkInt16)
image_2D = sitk.Image(64, 64, sitk.sitkFloat32)
image_2D = sitk.Image([32,32], sitk.sitkUInt32)
image_RGB = sitk.Image([128,64], sitk.sitkVectorUInt8, 3)<jupyter_output><empty_output><jupyter_text>Basic Image AttributesYou can change the image origin, spacing and direction. Making such changes to an image already containing data should be done cautiously.<jupyter_code>image_3D.SetOrigin((78.0, 76.0, 77.0))
image_3D.SetSpacing([0.5,0.5,3.0])

print(image_3D.GetOrigin())
print(image_3D.GetSize())
print(image_3D.GetSpacing())
print(image_3D.GetDirection())<jupyter_output><empty_output><jupyter_text>Image dimension queries:<jupyter_code>print(image_3D.GetDimension())
print(image_3D.GetWidth())
print(image_3D.GetHeight())
print(image_3D.GetDepth())<jupyter_output><empty_output><jupyter_text>What is the depth of a 2D image?<jupyter_code>print(image_2D.GetSize())
print(image_2D.GetDepth())<jupyter_output><empty_output><jupyter_text>Pixel/voxel type queries:<jupyter_code>print(image_3D.GetPixelIDValue())
print(image_3D.GetPixelIDTypeAsString())
print(image_3D.GetNumberOfComponentsPerPixel())<jupyter_output><empty_output><jupyter_text>What is the dimension and size of a Vector image and its data?<jupyter_code>print(image_RGB.GetDimension())
print(image_RGB.GetSize())
print(image_RGB.GetNumberOfComponentsPerPixel())<jupyter_output><empty_output><jupyter_text>Accessing Pixels and SlicingThe Image class's member functions ``GetPixel`` and ``SetPixel`` provide an ITK-like interface for pixel access.<jupyter_code>help(image_3D.GetPixel)
print(image_3D.GetPixel(0, 0, 0))
image_3D.SetPixel(0, 0, 0, 1)
print(image_3D.GetPixel(0, 0, 0))

# This can also be done using Pythonic notation.
print(image_3D[0,0,1])
image_3D[0,0,1] = 2
print(image_3D[0,0,1])<jupyter_output><empty_output><jupyter_text>Slicing of SimpleITK images returns a copy of the image data. This is similar to slicing Python lists and differs from the "view" returned by slicing numpy arrays.<jupyter_code># Brute force sub-sampling 
logo_subsampled = logo[::2,::2]

# Get the sub-image containing the word Simple
simple = logo[0:115,:]

# Get the sub-image containing the word Simple and flip it
simple_flipped = logo[115:0:-1,:]

n = 4

plt.subplot(n,1,1)
plt.imshow(sitk.GetArrayViewFromImage(logo))
plt.axis('off');

plt.subplot(n,1,2)
plt.imshow(sitk.GetArrayViewFromImage(logo_subsampled))
plt.axis('off');

plt.subplot(n,1,3)
plt.imshow(sitk.GetArrayViewFromImage(simple))
plt.axis('off')

plt.subplot(n,1,4)
plt.imshow(sitk.GetArrayViewFromImage(simple_flipped))
plt.axis('off');<jupyter_output><empty_output><jupyter_text>Draw a square on top of the logo image:After running this cell, uncomment "Version 3" and see its effect.<jupyter_code># Version 0: get the numpy array and assign the value via broadcast - later on you will need to construct 
# a new image from the array 
logo_pixels = sitk.GetArrayFromImage(logo)
logo_pixels[0:10,0:10] = [0,255,0]

# Version 1: generates an error, the image slicing returns a new image and you cannot assign a value to an image 
#logo[0:10,0:10] = [255,0,0]

# Version 2: image slicing returns a new image, so all assignments here will not have any effect on the original
# 'logo' image
logo_subimage = logo[0:10, 0:10]
for x in range(0,10):
    for y in range(0,10):
        logo_subimage[x,y] = [255,0,0]

# Version 3: modify the original image, iterate and assign a value to each pixel
#for x in range(0,10):
#    for y in range(0,10):
#        logo[x,y] = [255,0,0]

        
plt.subplot(2,1,1)
plt.imshow(sitk.GetArrayViewFromImage(logo))
plt.axis('off')

plt.subplot(2,1,2)
plt.imshow(logo_pixels)
plt.axis('off');<jupyter_output><empty_output><jupyter_text>We can also paste one image into the other, either using the [PasteImageFilter](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1PasteImageFilter.html) with its procedural interface or using a more Pythonic approach with image slicing. Note that for these operations SimpleITK treats the images as arrays of pixels and not as spatial objects. In the example below the fact that the images have different spacings is ignored.<jupyter_code>logo = sitk.ReadImage(fdata('SimpleITK.jpg'))

sz_x = 10
sz_y = 10
color_channels = [sitk.Image([sz_x,sz_y], sitk.sitkUInt8),
                  sitk.Image([sz_x,sz_y], sitk.sitkUInt8)+255,
                  sitk.Image([sz_x,sz_y], sitk.sitkUInt8)]
color_image = sitk.Compose(color_channels)
color_image.SetSpacing([0.5,0.5])

print(logo.GetSpacing())
print(color_image.GetSpacing())

#Set sub image using the Paste function
logo = sitk.Paste(destinationImage=logo, 
                 sourceImage=color_image,
                 sourceSize=color_image.GetSize(),
                 sourceIndex=[0,0],
                 destinationIndex=[0,0])
#Set sub image using slicing.
logo[20:20+sz_x, 20:20+sz_y] = color_image

sitk.Show(logo)<jupyter_output><empty_output><jupyter_text>Conversion between numpy and SimpleITKSimpleITK and numpy indexing access is in opposite order! SimpleITK: image[x,y,z]numpy: image_numpy_array[z,y,x]  From SimpleITK to numpyWe have two options for converting from SimpleITK to numpy:* GetArrayFromImage(): returns a copy of the image data. You can then freely modify the data as it has no effect on the original SimpleITK image.* GetArrayViewFromImage(): returns a view on the image data which is useful for display in a memory efficient manner. You cannot modify the data and __the view will be invalid if the original SimpleITK image is deleted__.<jupyter_code>nda = sitk.GetArrayFromImage(image_3D)
print(image_3D.GetSize())
print(nda.shape)

nda = sitk.GetArrayFromImage(image_RGB)
print(image_RGB.GetSize())
print(nda.shape)
gabor_image = sitk.GaborSource(size=[64,64], frequency=.03)
# Getting a numpy array view on the image data doesn't copy the data
nda_view = sitk.GetArrayViewFromImage(gabor_image)
plt.imshow(nda_view, cmap=plt.cm.Greys_r)
plt.axis('off');

# Trying to assign a value to the array view will throw an exception
nda_view[0,0] = 255<jupyter_output><empty_output><jupyter_text>From numpy to SimpleITKRemember to to set the image's origin, spacing, and possibly direction cosine matrix. The default values may not match the physical dimensions of your image.<jupyter_code>nda = np.zeros((10,20,3))

        #if this is supposed to be a 3D gray scale image [x=3, y=20, z=10]
img = sitk.GetImageFromArray(nda)
print(img.GetSize())

      #if this is supposed to be a 2D color image [x=20,y=10]
img = sitk.GetImageFromArray(nda, isVector=True)
print(img.GetSize())<jupyter_output><empty_output><jupyter_text>There and back againThe following code cell illustrates a situation where your code is a combination of SimpleITK methods and custom Python code which works with intensity values or labels outside of SimpleITK. This is a reasonable approach when you implement an algorithm in Python and don't care about the physical spacing of things (you are actually assuming the volume is isotropic).<jupyter_code>def my_algorithm(image_as_numpy_array):
    # res is the image result of your algorithm, has the same grid size as the original image
    res = image_as_numpy_array
    return res

# Starting with SimpleITK
img = sitk.ReadImage(fdata('training_001_mr_T1.mha'))

# Custom Python code working on a numpy array.
npa_res = my_algorithm(sitk.GetArrayFromImage(img))

# Converting back to SimpleITK (assumes we didn't move the image in space as we copy the information from the original)
res_img = sitk.GetImageFromArray(npa_res)
res_img.CopyInformation(img)

# Continuing to work with SimpleITK images
res_img - img<jupyter_output><empty_output><jupyter_text>Image operationsSimpleITK supports basic arithmetic operations between images, taking into account their physical space.Repeatedly run this cell. Fix the error (comment out the SetDirection, then SetSpacing). Why doesn't the SetOrigin line cause a problem? How close do two physical attributes need to be in order to be considered equivalent?<jupyter_code>img1 = sitk.Image(24,24, sitk.sitkUInt8)
img1[0,0] = 0

img2 = sitk.Image(img1.GetSize(), sitk.sitkUInt8)
img2.SetDirection([0,1,0.5,0.5])
img2.SetSpacing([0.5,0.8])
img2.SetOrigin([0.000001,0.000001])
img2[0,0] = 255

img3 = img1 + img2
print(img3[0,0])<jupyter_output><empty_output><jupyter_text>Reading and WritingSimpleITK can read and write images stored in a single file, or a set of files (e.g. DICOM series).Images stored in the DICOM format have a meta-data dictionary associated with them, which is populated with the DICOM tags. When a DICOM series is read as a single image, the meta-data information is not available since DICOM tags are specific to each file. If you need the meta-data, you have three options:1. Using the object oriented interface's [ImageSeriesReader](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1ImageSeriesReader.html) class, configure it to load the tags using the `MetaDataDictionaryArrayUpdateOn` method and possibly the `LoadPrivateTagsOn` method if you need the private tags. Once the series is read you can access the meta-data from the series reader using the `GetMetaDataKeys`, `HasMetaDataKey`, and `GetMetaData`.2. Using the object oriented interface's [ImageFileReader](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1ImageFileReader.html), set a specific slice's file name and only read it's meta-data using the `ReadImageInformation` method which only reads the meta-data but not the bulk pixel information. Once the meta-data is read you can access it from the file reader using the `GetMetaDataKeys`, `HasMetaDataKey`, and `GetMetaData`.3. Using the object oriented interface's [ImageFileReader](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1ImageFileReader.html), set a specific slice's file name and read it. Or using the procedural interface's, [ReadImage](https://simpleitk.org/doxygen/latest/html/namespaceitk_1_1simple.htmlae3b678b5b043c5a8c93aa616d5ee574c) function, read a specific file. You can then access the meta-data directly from the [Image](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1Image.html) using the `GetMetaDataKeys`, `HasMetaDataKey`, and `GetMetaData`. In the following cell, we read an image in JPEG format, and write it as PNG and BMP. File formats are deduced from the file extension. Appropriate pixel type is also set - you can override this and force a pixel type of your choice.<jupyter_code>img = sitk.ReadImage(fdata('SimpleITK.jpg'))
print(img.GetPixelIDTypeAsString())

# write as PNG and BMP
sitk.WriteImage(img, os.path.join(OUTPUT_DIR, 'SimpleITK.png'))
sitk.WriteImage(img, os.path.join(OUTPUT_DIR, 'SimpleITK.bmp'))<jupyter_output><empty_output><jupyter_text>Read an image in JPEG format and cast the pixel type according to user selection.<jupyter_code># Several pixel types, some make sense in this case (vector types) and some are just show
# that the user's choice will force the pixel type even when it doesn't make sense
# (e.g. sitkVectorUInt16 or sitkUInt8).
pixel_types = { 'sitkUInt8': sitk.sitkUInt8,
                'sitkUInt16' : sitk.sitkUInt16,
                'sitkFloat64' : sitk.sitkFloat64,
                'sitkVectorUInt8' : sitk.sitkVectorUInt8,
                'sitkVectorUInt16' : sitk.sitkVectorUInt16,
                'sitkVectorFloat64' : sitk.sitkVectorFloat64}

def pixel_type_dropdown_callback(pixel_type, pixel_types_dict):
    #specify the file location and the pixel type we want
    img = sitk.ReadImage(fdata('SimpleITK.jpg'), pixel_types_dict[pixel_type])
    
    print(img.GetPixelIDTypeAsString())
    print(img[0,0])
    plt.imshow(sitk.GetArrayViewFromImage(img))
    plt.axis('off')
 
interact(pixel_type_dropdown_callback, pixel_type=list(pixel_types.keys()), pixel_types_dict=fixed(pixel_types));<jupyter_output><empty_output><jupyter_text>Read a DICOM series and write it as a single mha file<jupyter_code>data_directory = os.path.dirname(fdata("CIRS057A_MR_CT_DICOM/readme.txt"))
series_ID = '1.2.840.113619.2.290.3.3233817346.783.1399004564.515'

# Get the list of files belonging to a specific series ID.
reader = sitk.ImageSeriesReader()
# Use the functional interface to read the image series.
original_image = sitk.ReadImage(reader.GetGDCMSeriesFileNames(data_directory, series_ID))

# Write the image.
output_file_name_3D = os.path.join(OUTPUT_DIR, '3DImage.mha')
sitk.WriteImage(original_image, output_file_name_3D)

# Read it back again.
written_image = sitk.ReadImage(output_file_name_3D)

# Check that the original and written image are the same.
statistics_image_filter = sitk.StatisticsImageFilter()
statistics_image_filter.Execute(original_image - written_image)

# Check that the original and written files are the same
print(f'Max, Min differences are : {statistics_image_filter.GetMaximum()}, {statistics_image_filter.GetMinimum()}')<jupyter_output><empty_output><jupyter_text>Write an image series as JPEG. The WriteImage function receives a volume and a list of images names and writes the volume according to the z axis. For a displayable result we need to rescale the image intensities (default is [0,255]) since the JPEG format requires a cast to the UInt8 pixel type.<jupyter_code>sitk.WriteImage(sitk.Cast(sitk.RescaleIntensity(written_image), sitk.sitkUInt8), 
                [os.path.join(OUTPUT_DIR, f'slice{i:03d}.jpg') for i in range(written_image.GetSize()[2])])<jupyter_output><empty_output><jupyter_text>Select a specific DICOM series from a directory and only then load user selection.<jupyter_code>data_directory = os.path.dirname(fdata("CIRS057A_MR_CT_DICOM/readme.txt"))
# Global variable 'selected_series' is updated by the interact function
selected_series = ''
file_reader = sitk.ImageFileReader()
def DICOM_series_dropdown_callback(series_to_load, series_dictionary):
    global selected_series
               # Print some information about the series from the meta-data dictionary
               # DICOM standard part 6, Data Dictionary: http://medical.nema.org/medical/dicom/current/output/pdf/part06.pdf
    file_reader.SetFileName(series_dictionary[series_to_load][0])
    file_reader.ReadImageInformation()
    tags_to_print = {'0010|0010': 'Patient name: ', 
                     '0008|0060' : 'Modality: ',
                     '0008|0021' : 'Series date: ',
                     '0008|0080' : 'Institution name: ',
                     '0008|1050' : 'Performing physician\'s name: '}
    for tag in tags_to_print:
        try:
            print(tags_to_print[tag] + file_reader.GetMetaData(tag))
        except: # Ignore if the tag isn't in the dictionary
            pass
    selected_series = series_to_load                    

# Directory contains multiple DICOM studies/series, store
             # in dictionary with key being the series ID
reader = sitk.ImageSeriesReader()
series_file_names = {}
series_IDs = reader.GetGDCMSeriesIDs(data_directory)
            # Check that we have at least one series
if series_IDs:
    for series in series_IDs:
        series_file_names[series] = reader.GetGDCMSeriesFileNames(data_directory, series)
    
    interact(DICOM_series_dropdown_callback, series_to_load=list(series_IDs), series_dictionary=fixed(series_file_names)); 
else:
    print('Data directory does not contain any DICOM series.')
reader.SetFileNames(series_file_names[selected_series])
img = reader.Execute()
# Display the image slice from the middle of the stack, z axis
z = int(img.GetDepth()/2)
plt.imshow(sitk.GetArrayViewFromImage(img)[z,:,:], cmap=plt.cm.Greys_r)
plt.axis('off');<jupyter_output><empty_output><jupyter_text>DICOM photometric interpretation Generally speaking, SimpleITK represents color images as multi-channel images independent of a [color space](https://en.wikipedia.org/wiki/Color_space). It is up to you to interpret the channels correctly based on additional color space knowledge prior to using them for display or any other purpose.The following cells illustrate reading and interpretation of interesting images in DICOM format. The first is a photograph of an X-ray on a light box (yes, there are some strange things in the wild). The second is a digital X-ray. While both of these are chest X-rays they differ in image modality (0008|0060) and in Photometric Interpretation (0028|0004), color space in DICOM speak.Things to note:1. When using SimpleITK to read a color DICOM image, the channel values will be transformed to the RGB color space.2. When using SimpleITK to read a scalar image, it is assumed that the lowest intensity value is black and highest white. If the photometric interpretation tag is MONOCHROME2 (lowest value displayed as black) nothing is done. If it is MONOCHROME1 (lowest value displayed as white), the pixel values are inverted.<jupyter_code>xrays = [sitk.ReadImage(fdata('photo.dcm')),
         sitk.ReadImage(fdata('cxr.dcm'))]
for img in xrays:
    print(f'Image Modality: {img.GetMetaData("0008|0060")}')
    print(f'Number of channels: {img.GetNumberOfComponentsPerPixel()}')
    print(f'Photomertic Interpretation: {img.GetMetaData("0028|0004")}')
    # Display the image using Fiji which expects the channels to be in the RGB color space
    sitk.Show(img)<jupyter_output><empty_output><jupyter_text>The first, is a color sRGB image while an x-ray should be a single channel gray scale image. We will [convert sRGB to gray scale](https://en.wikipedia.org/wiki/GrayscaleConverting_color_to_grayscale).<jupyter_code>def srgb2gray(image):
    # Convert sRGB image to gray scale and rescale results to [0,255]    
    channels = [sitk.VectorIndexSelectionCast(image,i, sitk.sitkFloat32) for i in range(image.GetNumberOfComponentsPerPixel())]
    #linear mapping
    I = 1/255.0*(0.2126*channels[0] + 0.7152*channels[1] + 0.0722*channels[2])
    #nonlinear gamma correction
    I = I*sitk.Cast(I<=0.0031308,sitk.sitkFloat32)*12.92 + I**(1/2.4)*sitk.Cast(I>0.0031308,sitk.sitkFloat32)*1.055-0.055
    return sitk.Cast(sitk.RescaleIntensity(I), sitk.sitkUInt8)

sitk.Show(srgb2gray(xrays[0]))<jupyter_output><empty_output><jupyter_text>Finer controlThe ImageFileReader's interface provides finer control for reading, allowing us to require the use of a specific IO and allowing us to stream parts of an image to memory without reading the whole image (supported by a subset of the ImageIO components). Selecting a Specific Image IOSimpleITK relies on the registered ImageIOs to indicate whether they can read a file and then perform the reading. This is done automatically, going over the set of ImageIOs and inquiring whether they can read the given file. The first one that can is selected. If multiple ImageIOs can read a specific format, we do not know which one was used for the task (e.g. TIFFImageIO and LSMImageIO, which is derived from it, can both read tif files). In some cases you may want to use a specific IO, possibly one that reads the file faster, or supports a more complete feature set associated with the file format.The next cell shows how to find out which ImageIOs are registered and specify the one we want.<jupyter_code>file_reader = sitk.ImageFileReader()

# Get a tuple listing all registered ImageIOs
image_ios_tuple = file_reader.GetRegisteredImageIOs()
print("The supported image IOs are: " + str(image_ios_tuple))

# Optionally, just print the reader and see which ImageIOs are registered
print('\n',file_reader)

# Specify the JPEGImageIO and read file
file_reader.SetImageIO('JPEGImageIO')
file_reader.SetFileName(fdata('SimpleITK.jpg'))
logo = file_reader.Execute()

# Unfortunately, now reading a non JPEG image will fail
try:
    file_reader.SetFileName(fdata('cthead1.png'))
    ct_head = file_reader.Execute()
except RuntimeError:
    print('Got a RuntimeError exception.')
    
# We can reset the file reader to its default behaviour so that it automatically 
# selects the ImageIO
file_reader.SetImageIO('')
ct_head = file_reader.Execute()<jupyter_output><empty_output><jupyter_text>Streaming Image IOSome of the ImageIOs supported in SimpleITK allow you to stream in sub-regions of an image without the need to read the whole image into memory. This is very useful when you are memory constrained (either your images are large or your memory is limited). The ImageIOs that support streaming include HDF5ImageIO, VTKImageIO, NiftiImageIO, MetaImageIO...The next cell shows how to read in a sub/cropped image from a larger image. We read the central 1/3 portion of the image [1/3,2/3] of the original image.<jupyter_code>file_reader = sitk.ImageFileReader()
file_reader.SetFileName(fdata('vm_head_rgb.mha'))

file_reader.ReadImageInformation()
image_size = file_reader.GetSize()
start_index, extract_size = zip(*[(int(1.0/3.0*sz), int(1.0/3.0*sz)) for sz in file_reader.GetSize()])

file_reader.SetExtractIndex(start_index)
file_reader.SetExtractSize(extract_size)

sitk.Show(file_reader.Execute())<jupyter_output><empty_output><jupyter_text>The next cells show how to subtract two large images from each other with a smaller memory footprint than the direct approach, though the code is much more complex and slower than the direct approach:```sitk.ReadImage(image1_file_name) - sitk.ReadImage(image2_file_name)```**Note**: The code assume that the two images occupy the same spatial region (origin, spacing, direction cosine matrix).<jupyter_code>def streaming_subtract(image1_file_name, image2_file_name, parts):
    '''
    Subtract image1 from image2 using 'parts' number of sub-regions.
    '''
    file_reader = sitk.ImageFileReader()
    file_reader.SetFileName(image1_file_name)
    file_reader.ReadImageInformation()
    image_size = file_reader.GetSize()

    # Create the result image, initially empty
    result_img = sitk.Image(file_reader.GetSize(), file_reader.GetPixelID(), file_reader.GetNumberOfComponents())
    result_img.SetSpacing(file_reader.GetSpacing())
    result_img.SetOrigin(file_reader.GetOrigin())
    result_img.SetDirection(file_reader.GetDirection())

    extract_size = list(file_reader.GetSize())
    extract_size[-1] = extract_size[-1]//parts
    current_index = [0]*file_reader.GetDimension()
    for i in range(parts):
        if i == (parts-1): # last region may be smaller than the standard extract region
            extract_size[-1] = image_size[-1] - current_index[-1]
        file_reader.SetFileName(image1_file_name)
        file_reader.SetExtractIndex(current_index)
        file_reader.SetExtractSize(extract_size)
        sub_image1 = file_reader.Execute()

        file_reader.SetFileName(image2_file_name)
        file_reader.SetExtractIndex(current_index)
        file_reader.SetExtractSize(extract_size)
        sub_image2 = file_reader.Execute()
        # Paste the result of subtracting the two subregions into their location in the result_img
        result_img = sitk.Paste(result_img, sub_image1 - sub_image2, extract_size, [0]*file_reader.GetDimension(), current_index)
        current_index[-1] += extract_size[-1]        
    return result_img

# If you have the patience and RAM you can try this with the vm_head_rgb.mha image.
image1_file_name = fdata('fib_sem_bacillus_subtilis.mha')
image2_file_name = fdata('fib_sem_bacillus_subtilis.mha')<jupyter_output><empty_output><jupyter_text>A simple way of seeing your system's memory usage is to open the appropriate monitoring program: (Windows) Resource Monitor; (Linux) top; (OS X) Activity Monitor. This will give you a rough idea of the memory used by the streaming vs. non streaming approaches.<jupyter_code>result_img = streaming_subtract(image1_file_name, image2_file_name, parts=5)
del result_img
result_img = sitk.ReadImage(image1_file_name) - sitk.ReadImage(image2_file_name)
del result_img<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08476371338835731, 'fraction_numerical': 0.025965963041414068, 'mean_word_length': 5.315267527675276, 'pattern_counts': {'":': 0, '<': 110, '<?xml version=': 0, '>': 110, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '27', 'dir': 'jupyter-structured-clean-dedup', 'id': '178250', 'license': 'Apache-2.0', 'n_tokens_mistral': 8920, 'n_tokens_neox': 8382, 'n_words': 2735, 'path': 'Python/03_Image_Details.ipynb', 'repo_name': 'NanYoMy/SimpleITK-Notebooks', 'size': '27382'}