Permalink
Browse files

Created the apply transfomr workflow.

1) Added a function to load the saved affine matrix from the text file.
2) Added the import of ndimage from scipy in the test_parzenhist.py file.
3) Created a new workflow file apply_transform.
  • Loading branch information...
Parichit Sharma
Parichit Sharma committed Jun 18, 2018
1 parent c3b4786 commit b765826b750e242994d84489aa699be7d49fdc59
Showing with 59 additions and 2 deletions.
  1. +1 −0 dipy/align/tests/test_parzenhist.py
  2. +11 −2 dipy/io/image.py
  3. +47 −0 dipy/workflows/apply_transform.py
@@ -1,5 +1,6 @@
import numpy as np
import scipy as sp
from scipy import ndimage
from functools import reduce
from operator import mul
from dipy.core.ndindex import ndindex
@@ -1,7 +1,6 @@
from __future__ import division, print_function, absolute_import

import nibabel as nib
import numpy as np
import nibabel as nib


def load_nifti(fname, return_img=False, return_voxsize=False,
@@ -37,3 +36,13 @@ def save_affine_matrix(fname, affine):
The object containing the affine matrix.
"""
np.savetxt(fname, affine)


def load_affine_matrix(fname):
"""
Parameters
---------
fname : str
The file containing the saved affine matrix.
"""
return np.loadtxt(fname)
@@ -0,0 +1,47 @@
from dipy.workflows.workflow import Workflow

from dipy.align.imaffine import AffineMap
import nibabel as nib
import numpy as np
from os.path import join as pjoin
from os import path
from dipy.io.image import save_nifti, load_affine_matrix


class ApplyTransformFlow(Workflow):

def run(self, reference_image_file, moving_image_files, affine_matrix_file,
out_file='transformed.nii.gz'):

"""
Parameters
----------
reference_image_file : string
Path to the static image file.
moving_image_files : string
Location of the file or folder containing the images to be transformed.
affine_matrix_file : string
The text file containing the affine matrix for transformation.
out_file : string, optional
Name of the transformed file. (default 'transformed.nii.gz')
"""

io = self.get_io_iterator()

for ref_image_file, mov_images, affine_matrix_file, out_file in io:

ref_image = nib.load(ref_image_file)
static_grid2world = ref_image.affine

image = nib.load(mov_images)
image_data = np.array(image.get_data())

affine_matrix = load_affine_matrix(affine_matrix_file)

img_transformation = AffineMap(affine=affine_matrix, domain_grid_shape=image_data.shape)
transformed = img_transformation.transform(image_data)

save_nifti(pjoin(path.split(mov_images)[1]+out_file), transformed, affine=static_grid2world)

0 comments on commit b765826

Please sign in to comment.