Exemplo n.º 1
0
#     :cpp:func:`Image::SetSpacing() <itk::simple::Image::SetSpacing>` can be used to get and set spacing respectively.
#
# **Direction Matrix**
#     Mapping/rotation between direction of the pixel/voxel axes and physical directions. Default is identity matrix. The matrix is passed as a 1D array in row-major form.
#
#     :cpp:func:`Image::GetDirection() <itk::simple::Image::GetDirection>` and
#     :cpp:func:`Image::SetDirection() <itk::simple::Image::SetDirection>` can be used to get and set direction matrix respectively.
#
# .. note ::
#     All the transformations like rotation or affine transform are done on the underlying physical space. You can think of image of a view of this physical space.
#
# Let's illustrate this in python:

from downloaddata import fetch_data as fdata
image = sitk.ReadImage(
    fdata("nac-hncma-atlas2013-Slicer4Version/Data/A1_grayT1.nrrd"))

print("Pixel Type    {}".format(image.GetPixelID()))
print("Size          {}".format(image.GetSize()))
print("Origin        {}".format(image.GetOrigin()))
print("Spacing       {}".format(image.GetSpacing()))
print("Direction     {}".format(image.GetDirection()))

##############################################################################
# Transform voxels to physical space
# ----------------------------------
#
# Following equation can be used to convert voxel coordinates/indices to physical coordinates:
#
# .. math::
#
Exemplo n.º 2
0
Laboratory at Brigham and Women's Hospital has a wonderful
`Multi-modality MRI-based Atlas of the Brain <http://www.spl.harvard.edu/publications/item/view/2037>`_ that we can use.

Please note, what is done here is for convenience and is not the common way
images are displayed for radiological work.

Get Images
----------
"""
import matplotlib.pyplot as plt
import SimpleITK as sitk
from downloaddata import fetch_data as fdata
from myshow import myshow

img_T1 = sitk.ReadImage(
    fdata("nac-hncma-atlas2013-Slicer4Version/Data/A1_grayT1.nrrd"))
img_T2 = sitk.ReadImage(
    fdata("nac-hncma-atlas2013-Slicer4Version/Data/A1_grayT2.nrrd"))
img_labels = sitk.ReadImage(
    fdata("nac-hncma-atlas2013-Slicer4Version/Data/hncma-atlas.nrrd"))

myshow(img_T1, title='T1')
myshow(img_T2, title='T2')
myshow(sitk.LabelToRGB(img_labels), title='lables')

##############################################################################
# Visualize another axis.

size = img_T1.GetSize()
myshow(img_T1[:, size[1] // 2, :])
Exemplo n.º 3
0
Visualizing 2D images
=====================

In this example, we will explore using matplotlib to display images in our
notebooks, and work towards developing a reusable function to display 2D,3D,
color for SimpleITK images.

"""

import matplotlib.pyplot as plt
import SimpleITK as sitk
from downloaddata import fetch_data as fdata

##############################################################################
# Fetch and read image
img1 = sitk.ReadImage(fdata("cthead1.png"))
img2 = sitk.ReadImage(fdata("VM1111Shrink-RGB.png"))

##############################################################################
# SimpleITK has a built in `Show` method which saves the image to disk and
# launches a user configurable program ( defaults to ImageJ ), to display the
# image.
#
# .. code-block:: python
#
#     sitk.Show(img1, title="cthead1")
#     sitk.Show(img2, title="Visible Human Head")
#
# Plotting with ``matplotlib``
# ----------------------------
# You can also use matplotlib to show images.
Exemplo n.º 4
0
def test():

    # show the standard fiducial list
    fixed_fiducial_points, moving_fiducial_points = ru.load_RIRE_ground_truth(
        fdata("ct_T1.standard"))
    print "The standard fiducial points are ", fixed_fiducial_points
    print "The standard fiducial points shape ", type(moving_fiducial_points)

    # test_data = fdata("training_001_ct.mha")
    fixed_data = "/home/maguangshen/PycharmProjects/SPL/Data_SPL/test_resampled_MRI.mha"
    fixed_img = sitk.ReadImage(fixed_data, sitk.sitkFloat32)

    moving_data = "/home/maguangshen/PycharmProjects/SPL/Data_SPL/test_US_before.mha"
    moving_img = sitk.ReadImage(moving_data, sitk.sitkFloat32)

    # This is the list -- can be used for numpy array
    # Read the MRI fiducial
    csv_MRI = "/home/maguangshen/PycharmProjects/SPL/Data_SPL/Case23-MRI.csv"
    fiducial_MRI, fiducial_MRI_list = Read_tsv(csv_MRI)
    print "The fiducials of MRI are ", fiducial_MRI_list

    # Read the US fiducial
    csv_US = "/home/maguangshen/PycharmProjects/SPL/Data_SPL/Case23-beforeUS.csv"
    fiducial_US, fiducial_US_list = Read_tsv(csv_US)
    print "The ficucials of US are ", fiducial_US_list

    R, t = ru.absolute_orientation_m(fiducial_MRI_list, fiducial_US_list)
    print "The testing R is ", R
    print "The testing T is ", t
    print "The R flatten is ", R.flatten()

    # Set up the registration parameters
    reference_transform = sitk.Euler3DTransform()  # Euler 3D Transform
    reference_transform.SetMatrix(R.flatten())  # Flatten the R matrix
    reference_transform.SetTranslation(t)  # Set the translation vector
    reference_errors_mean, reference_errors_std, _, reference_errors_max, _ = ru.registration_errors(
        reference_transform, fiducial_MRI_list, fiducial_US_list)
    print(
        'Reference data errors (FRE) in millimeters, mean(std): {:.2f}({:.2f}), max: {:.2f}'
        .format(reference_errors_mean, reference_errors_std,
                reference_errors_max))
    # Viz the results to map all the points

    # Generate a reference dataset from the reference transformation
    # Apply the transformation first based on the fiducial registration results
    fixed_points = ru.generate_random_pointset(
        image=fixed_img,
        num_points=100)  # generate random point sets from the volume dataset
    moving_points = [
        reference_transform.TransformPoint(p) for p in fixed_points
    ]

    # Compute the TRE prior to the registration
    pre_errors_mean, pre_errors_std, pre_errors_min, pre_errors_max, _ = ru.registration_errors(
        sitk.Euler3DTransform(),
        fixed_points,
        moving_points,
        display_errors=True)
    print(
        'Before registration, errors (TRE) in millimeters, mean(std): {:.2f}({:.2f}), max: {:.2f}'
        .format(pre_errors_mean, pre_errors_std, pre_errors_max))

    ## Initial alignment
    # Initialization of the transform
    initial_transform = sitk.CenteredTransformInitializer(
        sitk.Cast(fixed_img, moving_img.GetPixelID()), moving_img,
        sitk.Euler3DTransform(),
        sitk.CenteredTransformInitializerFilter.GEOMETRY)
    initial_errors_mean, initial_errors_std, initial_errors_min, initial_errors_max, _ = ru.registration_errors(
        initial_transform,
        fixed_points,
        moving_points,
        min_err=pre_errors_min,
        max_err=pre_errors_max,
        display_errors=True)
    print(
        'After initialization, errors (TRE) in millimeters, mean(std): {:.2f}({:.2f}), max: {:.2f}'
        .format(initial_errors_mean, initial_errors_std, initial_errors_max))

    # Begin registration
    registration_method = sitk.ImageRegistrationMethod()
    registration_method.SetMetricAsMattesMutualInformation(
        numberOfHistogramBins=50)  # Define the number of bins as 50
    registration_method.SetMetricSamplingStrategy(
        registration_method.RANDOM)  # Define the
    registration_method.SetMetricSamplingPercentage(
        0.01)  # Default sampling percentage is 0.01
    registration_method.SetInterpolator(
        sitk.sitkNearestNeighbor)  # Replace with sitkLinear
    registration_method.SetOptimizerAsGradientDescent(
        learningRate=1.0, numberOfIterations=100)  # Increase to 1000
    registration_method.SetOptimizerScalesFromPhysicalShift(
    )  # Understand the optimization method
    registration_method.SetInitialTransform(
        initial_transform, inPlace=False)  # Apply the initial transform

    # Add the callback to display the similarity value
    registration_method.AddCommand(sitk.sitkStartEvent,
                                   rc.metric_and_reference_start_plot)
    registration_method.AddCommand(sitk.sitkEndEvent,
                                   rc.metric_and_reference_end_plot)
    registration_method.AddCommand(
        sitk.sitkIterationEvent, lambda: rc.metric_and_reference_plot_values(
            registration_method, fixed_points, moving_points))

    final_transform_single_scale = registration_method.Execute(
        sitk.Cast(fixed_img, sitk.sitkFloat32),
        sitk.Cast(moving_img, sitk.sitkFloat32))
    print('Final metric value: {0}'.format(
        registration_method.GetMetricValue()))
    print('Optimizer\'s stopping condition, {0}'.format(
        registration_method.GetOptimizerStopConditionDescription()))
    final_errors_mean, final_errors_std, _, final_errors_max, _ = ru.registration_errors(
        final_transform_single_scale,
        fixed_points,
        moving_points,
        min_err=initial_errors_min,
        max_err=initial_errors_max,
        display_errors=True)
    print(
        'After registration, errors in millimeters, mean(std): {:.2f}({:.2f}), max: {:.2f}'
        .format(final_errors_mean, final_errors_std, final_errors_max))
    final_errors_mean, final_errors_std, _, final_errors_max, _ = ru.registration_errors(
        final_transform_single_scale,
        fixed_points,
        moving_points,
        display_errors=True)
Exemplo n.º 5
0
the input images' to be overlapping.

"""

import matplotlib.pyplot as plt
import SimpleITK as sitk
from downloaddata import fetch_data as fdata
from myshow import myshow, myshow3d

##############################################################################
# LabelOverlay In 2D
# ------------------
# We start by loading a segmented image. As the segmentation is just an image
# with integral data, we can display the labels as we would any other image.

img1 = sitk.ReadImage(fdata("cthead1.png"))
img1_seg = sitk.ReadImage(fdata("2th_cthead1.png"))

myshow(img1, title="cthead1")
myshow(img1_seg, title="Label Image as Grayscale")

##############################################################################
# We can also map the scalar label image to a color image as shown below.

myshow(sitk.LabelToRGB(img1_seg), title="Label Image as RGB")

##############################################################################
# Most filters which take multiple images as arguments require that the images
# occupy the same physical space. That is the pixel you are operating must
# refer to the same location. Luckily for us our image and labels do occupy the
# same physical space, allowing us to overlay the segmentation onto the