Beispiel #1
0
    def test_affine(self):
        # Test affine initialization

        affine_transform = pydeform.AffineTransform(
            np.array((
                (2, 0, 0),
                (0, 3, 0),
                (0, 0, 4)
            )),
            np.array((10, 10, 10))
        )

        # Do a registration pass without actual iterations to see if affine transform is
        # applied to the resulting displacement field
        settings = {
            'max_iteration_count': 0
        }

        fixed = pydeform.Volume(np.zeros((10,10,10), dtype=np.float32))
        moving = pydeform.Volume(np.zeros((10,10,10), dtype=np.float32))

        df = pydeform.register(
            fixed,
            moving,
            settings=settings,
            affine_transform=affine_transform
        )

        df = np.array(df, copy=False)

        # Ax + b -> A(1, 1, 1) + b -> (2, 3, 4) + (10, 10, 10) -> (12, 13, 14)
        # u(x) = Ax + b - x
        self.assertEqual(df[1,1,1,0], 11)
        self.assertEqual(df[1,1,1,1], 12)
        self.assertEqual(df[1,1,1,2], 13)
Beispiel #2
0
def build_mask(volume):
    """ Builds a fuzzy mask for the given volume """

    data = np.array(volume, copy=False)
    mask = (data > 0).astype(np.float32)
    mask = pydeform.Volume(mask)
    mask.copy_meta_from(volume)

    return mask
Beispiel #3
0
def _convert_image(sitk_image):
    """ Converts a SimpleITK.Image to a pydeform.Volume 
    
    Return:
        pydeform.Volume if sitk_image is valid, otherwise None
    """

    if sitk_image is None:
        return None

    return pydeform.Volume(sitk.GetArrayViewFromImage(sitk_image),
                           sitk_image.GetOrigin(), sitk_image.GetSpacing(),
                           np.array(sitk_image.GetDirection()).reshape((3, 3)))
Beispiel #4
0
def build_regularization_map(volume, threshold, rw0, rw1):
    """ Builds a regularization map given a volume. Voxels with intensities above 
        the given threshold have their regularization set to rw1, while all the
        other voxels have regularization weight rw0.
    """

    data = np.array(volume, copy=False)
    regmap = np.zeros(data.shape, dtype=np.float32)
    regmap = (rw0 * (data < threshold) + rw1 * (data >= threshold)).astype(
        np.float32)

    regmap = pydeform.Volume(regmap)
    regmap.copy_meta_from(volume)

    return regmap
Beispiel #5
0
def normalize_volume(vol):
    """ Returns a new normalized copy of `vol` """

    assert isinstance(vol, pydeform.Volume)

    # Create a new array with a copy of the volume data
    data = np.array(vol)

    # Create a new normalized array
    data = data / np.max(data)

    # Create a new volume
    out = pydeform.Volume(data)
    # Copy original meta data (origin, spacing, direction)
    out.copy_meta_from(vol)

    return out
Beispiel #6
0
def downsample(vol, factor):
    """ Rough downsampling of the volume by an integer factor """

    assert isinstance(factor, int)

    # Get the data
    data = np.array(vol, copy=False)

    # Create a downsampled copy of the data
    data = data[::factor, ::factor, ::factor]

    # Create new volume
    out = pydeform.Volume(data)
    # Set metadata
    out.origin = vol.origin
    out.spacing = np.array(vol.spacing) * factor
    out.direction = vol.direction

    return out