def axis_align_dwi(dwi, outfile=None, precision=5): dwi_hdr = get_hdr(dwi) spcdir_orig = matrix(find_spc_dir(dwi_hdr)) print(spcdir_orig) sizes = diag([linalg.norm(spcdir_orig[0,:]),linalg.norm(spcdir_orig[1,:]),linalg.norm(spcdir_orig[2,:])]) print(sizes) R = get_numpy_rotation(spcdir_orig) print(R) spcdir_new = matrix.round(sizes *R*linalg.inv(sizes)*spcdir_orig,4) print(spcdir_new) mf_orig = find_mf(dwi_hdr) print(mf_orig) mf_new = matrix.round(R * matrix(mf_orig),4) print(mf_new) mf_new = [','.join(map(str, x)) for x in mf_new.tolist()] newline = 'measurement frame: (%s) (%s) (%s)\n' % (mf_new[0], mf_new[1], mf_new[2]) dwi_new = splitext(dwi)[0] + '_axisaligned.nhdr' if not outfile else \ outfile t('unu save -f nrrd -e gzip -i %s -o %s' % (dwi, dwi_new)) replace_line_in_file(dwi_new, 'measurement frame:', newline) newline = 'space directions: (%s) (%s) (%s) none\n' % (','.join(map(str, spcdir_new[0])), ','.join(map(str, spcdir_new[1])), ','.join(map(str, spcdir_new[2]))) replace_line_in_file(dwi_new, 'space directions:', newline)
def axis_align_dwi(dwi, outfile=None, precision=5): dwi_hdr = get_hdr(dwi) spcdir_orig = matrix(find_spc_dir(dwi_hdr)) print(spcdir_orig) sizes = diag([ linalg.norm(spcdir_orig[0, :]), linalg.norm(spcdir_orig[1, :]), linalg.norm(spcdir_orig[2, :]) ]) print(sizes) R = get_numpy_rotation(spcdir_orig) print(R) spcdir_new = matrix.round(sizes * R * linalg.inv(sizes) * spcdir_orig, 4) print(spcdir_new) mf_orig = find_mf(dwi_hdr) print(mf_orig) mf_new = matrix.round(R * matrix(mf_orig), 4) print(mf_new) mf_new = [','.join(map(str, x)) for x in mf_new.tolist()] newline = 'measurement frame: (%s) (%s) (%s)\n' % (mf_new[0], mf_new[1], mf_new[2]) dwi_new = splitext(dwi)[0] + '_axisaligned.nhdr' if not outfile else \ outfile t('unu save -f nrrd -e gzip -i %s -o %s' % (dwi, dwi_new)) replace_line_in_file(dwi_new, 'measurement frame:', newline) newline = 'space directions: (%s) (%s) (%s) none\n' % (','.join( map(str, spcdir_new[0])), ','.join(map(str, spcdir_new[1])), ','.join( map(str, spcdir_new[2]))) replace_line_in_file(dwi_new, 'space directions:', newline)
def axis_align_dwi(dwi, hdr_out, outfile=None, precision=5): # hdr_out= hdr.copy() spcdir_orig = hdr_out['space directions'][0:3, 0:3] print(spcdir_orig) sizes = diag([ linalg.norm(spcdir_orig[0, :]), linalg.norm(spcdir_orig[1, :]), linalg.norm(spcdir_orig[2, :]) ]) print(sizes) R = get_numpy_rotation(spcdir_orig) print(R) spcdir_new = matrix.round(sizes @ R @ linalg.inv(sizes) @ spcdir_orig, precision) print(spcdir_new) mf_orig = hdr_out['measurement frame'] print(mf_orig) mf_new = matrix.round(R @ matrix(mf_orig), precision) print(mf_new) hdr_out['space directions'][0:3, 0:3] = array(spcdir_new) hdr_out['measurement frame'] = array(mf_new) nrrd.write(outfile, dwi, header=hdr_out, compression_level=1)
def axis_align_3d(image, outfile=None): img_hdr = get_hdr(image) spcdir_orig = matrix(find_spc_dir(img_hdr)) print(spcdir_orig) sizes = diag([linalg.norm(spcdir_orig[0,:]),linalg.norm(spcdir_orig[1,:]),linalg.norm(spcdir_orig[2,:])]) print(sizes) R = get_numpy_rotation(spcdir_orig) print(R) spcdir_new = matrix.round(sizes *R*linalg.inv(sizes)*spcdir_orig,4) print(spcdir_new) image_new = splitext(image)[0] + '_axisaligned.nhdr' if not outfile else \ outfile t('unu save -f nrrd -e gzip -i %s -o %s' % (image, image_new)) newline = 'space directions: (%s) (%s) (%s)\n' % (','.join(map(str, spcdir_new[0])), ','.join(map(str, spcdir_new[1])), ','.join(map(str, spcdir_new[2]))) replace_line_in_file(image_new, 'space directions:', newline)
def axis_align_3d(image, outfile=None): img_hdr = get_hdr(image) spcdir_orig = matrix(find_spc_dir(img_hdr)) print(spcdir_orig) sizes = diag([ linalg.norm(spcdir_orig[0, :]), linalg.norm(spcdir_orig[1, :]), linalg.norm(spcdir_orig[2, :]) ]) print(sizes) R = get_numpy_rotation(spcdir_orig) print(R) spcdir_new = matrix.round(sizes * R * linalg.inv(sizes) * spcdir_orig, 4) print(spcdir_new) image_new = splitext(image)[0] + '_axisaligned.nhdr' if not outfile else \ outfile t('unu save -f nrrd -e gzip -i %s -o %s' % (image, image_new)) newline = 'space directions: (%s) (%s) (%s)\n' % (','.join( map(str, spcdir_new[0])), ','.join(map(str, spcdir_new[1])), ','.join( map(str, spcdir_new[2]))) replace_line_in_file(image_new, 'space directions:', newline)