Example #1
0
def generateTestingPair(betaGT):
    betaGTRads = np.array(betaGT, dtype=np.float64)
    betaGTRads[0:3] = np.copy(np.pi * betaGTRads[0:3] / 180.0)
    ns = 181
    nr = 217
    nc = 181
    left = np.fromfile('data/t2/t2_icbm_normal_1mm_pn0_rf0.rawb',
                       dtype=np.ubyte).reshape(ns, nr, nc)
    left = left.astype(np.float64)
    right = np.fromfile('data/t1/t1_icbm_normal_1mm_pn0_rf0.rawb',
                        dtype=np.ubyte).reshape(ns, nr, nc)
    right = right.astype(np.float64)
    right = rcommon.applyRigidTransformation3D(right, betaGTRads)
    affine_transform = AffineTransform(
        'ijk', ['aligned-z=I->S', 'aligned-y=P->A', 'aligned-x=L->R'],
        np.eye(4))
    left = Image(left, affine_transform)
    right = Image(right, affine_transform)
    nipy.save_image(left, 'moving.nii')
    nipy.save_image(right, 'fixed.nii')
Example #2
0
def changeCoords2(affine, scale, coords):
    domain = coords.function_domain
    outrange = coords.function_range

    # Create a new matrix
    mod_affine = np.zeros((4, 4))
    # Set up the diagonals
    mod_affine[0, 0] = affine[0, 0] * scale
    mod_affine[1, 1] = affine[1, 1] * scale
    mod_affine[2, 2] = affine[2, 2] * scale
    mod_affine[
        3,
        3] = 1  # This is the new temporal dimension: each vol    ume is 2s apart
    # Pull the last column of the first 3 rows into the new matrix
    mod_affine[0, 3] = affine[0, 3] // scale
    mod_affine[1, 3] = affine[1, 3] // scale
    mod_affine[2, 3] = affine[2, 3] // scale

    modified = AffineTransform(domain, outrange, mod_affine)

    return modified
Example #3
0
def peelTemplateBrain():
    ns = 181
    nr = 217
    nc = 181
    gt_template = np.fromfile('data/phantom_1.0mm_normal_crisp.rawb',
                              dtype=np.ubyte).reshape((ns, nr, nc))
    t1_template = np.fromfile('data/t1/t1_icbm_normal_1mm_pn0_rf0.rawb',
                              dtype=np.ubyte).reshape((ns, nr, nc))
    t2_template = np.fromfile('data/t2/t2_icbm_normal_1mm_pn0_rf0.rawb',
                              dtype=np.ubyte).reshape((ns, nr, nc))
    #t1_template*=((1<=gt_template)*(gt_template<=3)+(gt_template==8))
    t1_template *= ((1 <= gt_template) * (gt_template <= 3))
    t2_template *= ((1 <= gt_template) * (gt_template <= 3))
    affine_transform = AffineTransform(
        'ijk', ['aligned-z=I->S', 'aligned-y=P->A', 'aligned-x=L->R'],
        np.eye(4))
    t1_template = Image(t1_template, affine_transform)
    t2_template = Image(t2_template, affine_transform)
    nipy.save_image(t1_template,
                    'data/t1/t1_icbm_normal_1mm_pn0_rf0_peeled.nii.gz')
    nipy.save_image(t2_template,
                    'data/t2/t2_icbm_normal_1mm_pn0_rf0_peeled.nii.gz')
Example #4
0
def testIntersubjectRigidRegistration(fname0, fname1, level, outfname):
    nib_left = nib.load(fname0)
    nib_right = nib.load(fname1)
    left = nib_left.get_data().astype(np.double).squeeze()
    right = nib_right.get_data().astype(np.double).squeeze()
    leftPyramid = [i for i in rcommon.pyramid_gaussian_3D(left, level)]
    rightPyramid = [i for i in rcommon.pyramid_gaussian_3D(right, level)]
    plotSlicePyramidsAxial(leftPyramid, rightPyramid)
    print 'Estimation started.'
    beta = estimateRigidTransformationMultiscale3D(leftPyramid, rightPyramid)
    print 'Estimation finished.'
    rcommon.applyRigidTransformation3D(left, beta)
    sl = np.array(left.shape) // 2
    sr = np.array(right.shape) // 2
    rcommon.overlayImages(left[sl[0], :, :], leftPyramid[0][sr[0], :, :])
    rcommon.overlayImages(left[sl[0], :, :], right[sr[0], :, :])
    affine_transform = AffineTransform(
        'ijk', ['aligned-z=I->S', 'aligned-y=P->A', 'aligned-x=L->R'],
        np.eye(4))
    left = Image(left, affine_transform)
    nipy.save_image(left, outfname)

    return beta
Example #5
0
def convert3DCoordsTo4D(orig):
    # Get the data from the original coordinates
    domain = orig.function_domain
    outrange = orig.function_range
    affine = orig.affine
    print(type(affine), affine)

    # Add the temporal dimension
    mod_domain = CoordinateSystem(domain.coord_names + ('t', ), domain.name,
                                  domain.coord_dtype)
    mod_range = CoordinateSystem(outrange.coord_names + ('t', ), outrange.name,
                                 outrange.coord_dtype)

    # Temporal is a little trickier in the affine matrix
    # Create a new matrix
    mod_affine = np.zeros((5, 5))
    # Set up the diagonals
    mod_affine[0, 0] = affine[0, 0]
    mod_affine[1, 1] = affine[1, 1]
    mod_affine[2, 2] = affine[2, 2]
    mod_affine[
        3,
        3] = 2  # This is the new temporal dimension: each volume is 2s apart
    mod_affine[
        4,
        4] = 1  # This is the last row in the matrix and should be all 0 except this column
    # Pull the last column of the first 3 rows into the new matrix
    mod_affine[0, 4] = affine[0, 3]
    mod_affine[1, 4] = affine[1, 3]
    mod_affine[2, 4] = affine[2, 3]

    print(mod_affine)

    modified = AffineTransform(mod_domain, mod_range, mod_affine)

    return modified
Example #6
0
def create(rootdir):

    out = None
    prev = None

    out_is_there = False

    dirs = sorted(os.listdir(rootdir))

    for d in dirs:

        files = os.listdir(os.path.join(rootdir, d))

        for f in files:
            input_image = tif.imread(os.path.join(rootdir, d, f))
            # print input_image
            # print type(input_image)
            # print 'ccc',input_image.flatten()
            if out_is_there:
                #out = np.concatenate([out, input_image.flatten()])
                out = np.dstack([out, input_image])
            else:
                # out = input_image.flatten()
                out = input_image
                out_is_there = True

    # return

    # i = 0
    # for root, dirs, files in os.walk(rootdir):

    #     fullpaths = [(os.path.join(root, name)) for name in files]

    #     for f in fullpaths:
    #       print f
    #       input_image = tif.imread(f)
    #       # print input_image
    #       # print type(input_image)
    #       # print 'ccc',input_image.flatten()
    #       if out_is_there:
    #         #out = np.concatenate([out, input_image.flatten()])
    #         out = np.dstack([out, input_image])
    #       else:
    #         # out = input_image.flatten()
    #         out = input_image
    #         out_is_there = True


#>>> from nipy.io.api import save_image
#>>> data = np.zeros((91,109,91), dtype=np.uint8)
#>>> cmap = AffineTransform('kji', 'zxy', np.eye(4))
#>>> img = Image(data, cmap)
#>>> fname1 = os.path.join(tmpdir, 'img1.nii.gz')
#>>> saved_img1 = save_image(img, fname1)

# image_data = PILImage.open(os.path.join(subdir,file))
# if image_data.mode != "RGB":
#   image_data = image_data.convert("RGB")

# print image_data.toString()
# break

# shutil.copy(os.path.join(subdir, file), '/tmp/'+str(i)+'.tif')
# i+=1
    length = out.shape[0]
    print 'aaa'
    print out
    # out = out.reshape((512, 512, length/512/512))
    print out
    cmap = AffineTransform('kji', 'zxy', np.eye(4))
    img = Image(out, cmap)
    save_image(img, '/tmp/out.nii.gz')