本文整理汇总了Python中msct_image.Image.transfo_phys2pix方法的典型用法代码示例。如果您正苦于以下问题:Python Image.transfo_phys2pix方法的具体用法?Python Image.transfo_phys2pix怎么用?Python Image.transfo_phys2pix使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类msct_image.Image
的用法示例。
在下文中一共展示了Image.transfo_phys2pix方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: register_seg
# 需要导入模块: from msct_image import Image [as 别名]
# 或者: from msct_image.Image import transfo_phys2pix [as 别名]
def register_seg(seg_input, seg_dest):
"""Slice-by-slice registration by translation of two segmentations.
For each slice, we estimate the translation vector by calculating the difference of position of the two centers of
mass.
The segmentations can be of different sizes but the output segmentation must be smaller than the input segmentation.
input:
seg_input: name of moving segmentation file (type: string)
seg_dest: name of fixed segmentation file (type: string)
output:
x_displacement: list of translation along x axis for each slice (type: list)
y_displacement: list of translation along y axis for each slice (type: list)
"""
seg_input_img = Image(seg_input)
seg_dest_img = Image(seg_dest)
seg_input_data = seg_input_img.data
seg_dest_data = seg_dest_img.data
x_center_of_mass_input = [0 for i in range(seg_dest_data.shape[2])]
y_center_of_mass_input = [0 for i in range(seg_dest_data.shape[2])]
print "\nGet center of mass of the input segmentation for each slice (corresponding to a slice in the output segmentation)..." # different if size of the two seg are different
# TO DO: select only the slices corresponding to the output segmentation
coord_origin_dest = seg_dest_img.transfo_pix2phys([[0, 0, 0]])
[[x_o, y_o, z_o]] = seg_input_img.transfo_phys2pix(coord_origin_dest)
for iz in xrange(seg_dest_data.shape[2]):
x_center_of_mass_input[iz], y_center_of_mass_input[iz] = ndimage.measurements.center_of_mass(
array(seg_input_data[:, :, z_o + iz])
)
x_center_of_mass_output = [0 for i in range(seg_dest_data.shape[2])]
y_center_of_mass_output = [0 for i in range(seg_dest_data.shape[2])]
print "\nGet center of mass of the output segmentation for each slice ..."
for iz in xrange(seg_dest_data.shape[2]):
x_center_of_mass_output[iz], y_center_of_mass_output[iz] = ndimage.measurements.center_of_mass(
array(seg_dest_data[:, :, iz])
)
x_displacement = [0 for i in range(seg_input_data.shape[2])]
y_displacement = [0 for i in range(seg_input_data.shape[2])]
print "\nGet displacement by voxel..."
for iz in xrange(seg_dest_data.shape[2]):
x_displacement[iz] = -(
x_center_of_mass_output[iz] - x_center_of_mass_input[iz]
) # WARNING: in ITK's coordinate system, this is actually Tx and not -Tx
y_displacement[iz] = (
y_center_of_mass_output[iz] - y_center_of_mass_input[iz]
) # This is Ty in ITK's and fslview' coordinate systems
return x_displacement, y_displacement
示例2: register_seg
# 需要导入模块: from msct_image import Image [as 别名]
# 或者: from msct_image.Image import transfo_phys2pix [as 别名]
def register_seg(seg_input, seg_dest):
seg_input_img = Image(seg_input)
seg_dest_img = Image(seg_dest)
seg_input_data = seg_input_img.data
seg_dest_data = seg_dest_img.data
x_center_of_mass_input = [0 for i in range(seg_dest_data.shape[2])]
y_center_of_mass_input = [0 for i in range(seg_dest_data.shape[2])]
print "\nGet center of mass of the input segmentation for each slice (corresponding to a slice in the output segmentation)..." # different if size of the two seg are different
# TO DO: select only the slices corresponding to the output segmentation
coord_origin_dest = seg_dest_img.transfo_pix2phys([[0, 0, 0]])
[[x_o, y_o, z_o]] = seg_input_img.transfo_phys2pix(coord_origin_dest)
for iz in xrange(seg_dest_data.shape[2]):
print iz
x_center_of_mass_input[iz], y_center_of_mass_input[iz] = ndimage.measurements.center_of_mass(
array(seg_input_data[:, :, z_o + iz])
)
x_center_of_mass_output = [0 for i in range(seg_dest_data.shape[2])]
y_center_of_mass_output = [0 for i in range(seg_dest_data.shape[2])]
print "\nGet center of mass of the output segmentation for each slice ..."
for iz in xrange(seg_dest_data.shape[2]):
x_center_of_mass_output[iz], y_center_of_mass_output[iz] = ndimage.measurements.center_of_mass(
array(seg_dest_data[:, :, iz])
)
x_displacement = [0 for i in range(seg_input_data.shape[2])]
y_displacement = [0 for i in range(seg_input_data.shape[2])]
print "\nGet displacement by voxel..."
for iz in xrange(seg_dest_data.shape[2]):
x_displacement[iz] = -(
x_center_of_mass_output[iz] - x_center_of_mass_input[iz]
) # strangely, this is the inverse of x_displacement when the same equation defines y_displacement
y_displacement[iz] = y_center_of_mass_output[iz] - y_center_of_mass_input[iz]
return x_displacement, y_displacement
示例3: generate_warping_field
# 需要导入模块: from msct_image import Image [as 别名]
# 或者: from msct_image.Image import transfo_phys2pix [as 别名]
#
# generate_warping_field('data_T2_RPI.nii.gz', x_disp_2_smooth, y_disp_2_smooth, fname='warping_field_im_trans.nii.gz')
# sct.run('sct_apply_transfo -i data_RPI_registered_reg1.nii.gz -d data_T2_RPI.nii.gz -w warping_field_im_trans.nii.gz -o data_RPI_registered_reg2.nii.gz -x spline')
f_1 = "/Users/tamag/data/data_template/independant_templates/Results_magma/t2_avg_RPI.nii.gz"
f_2 = "/Users/tamag/data/data_template/independant_templates/Results_magma/t1_avg.independent_RPI_reg1_unpad.nii.gz"
f_3 = "/Users/tamag/data/data_template/independant_templates/Results_magma/t1_avg.independent_RPI.nii.gz"
os.chdir("/Users/tamag/data/data_template/independant_templates/Results_magma")
im_1 = Image(f_1)
im_2 = Image(f_2)
data_1 = im_1.data
coord_test1 = [[1,1,1]]
coord_test = [[1,1,1],[2,2,2],[3,3,3]]
coordi_phys = im_1.transfo_pix2phys(coordi=coord_test)
coordi_pix = im_1.transfo_phys2pix(coordi = coordi_phys)
bla
# im_3 = nibabel.load(f_3)
# data_3 = im_3.get_data()
# hdr_3 = im_3.get_header()
#
# data_f = data_3 - laplace(data_3)
#
# img_f = nibabel.Nifti1Image(data_f, None, hdr_3)
# nibabel.save(img_f, "rehauss.nii.gz")
示例4: register_images
# 需要导入模块: from msct_image import Image [as 别名]
# 或者: from msct_image.Image import transfo_phys2pix [as 别名]
def register_images(
im_input,
im_dest,
mask="",
paramreg=Paramreg(
step="0", type="im", algo="Translation", metric="MI", iter="5", shrink="1", smooth="0", gradStep="0.5"
),
remove_tmp_folder=1,
):
path_i, root_i, ext_i = sct.extract_fname(im_input)
path_d, root_d, ext_d = sct.extract_fname(im_dest)
path_m, root_m, ext_m = sct.extract_fname(mask)
# set metricSize
if paramreg.metric == "MI":
metricSize = "32" # corresponds to number of bins
else:
metricSize = "4" # corresponds to radius (for CC, MeanSquares...)
# initiate default parameters of antsRegistration transformation
ants_registration_params = {
"rigid": "",
"affine": "",
"compositeaffine": "",
"similarity": "",
"translation": "",
"bspline": ",10",
"gaussiandisplacementfield": ",3,0",
"bsplinedisplacementfield": ",5,10",
"syn": ",3,0",
"bsplinesyn": ",3,32",
}
# Get image dimensions and retrieve nz
print "\nGet image dimensions of destination image..."
nx, ny, nz, nt, px, py, pz, pt = sct.get_dimension(im_dest)
print ".. matrix size: " + str(nx) + " x " + str(ny) + " x " + str(nz)
print ".. voxel size: " + str(px) + "mm x " + str(py) + "mm x " + str(pz) + "mm"
# Define x and y displacement as list
x_displacement = [0 for i in range(nz)]
y_displacement = [0 for i in range(nz)]
theta_rotation = [0 for i in range(nz)]
matrix_def = [0 for i in range(nz)]
# create temporary folder
print ("\nCreate temporary folder...")
path_tmp = "tmp." + time.strftime("%y%m%d%H%M%S")
sct.create_folder(path_tmp)
print "\nCopy input data..."
sct.run("cp " + im_input + " " + path_tmp + "/" + root_i + ext_i)
sct.run("cp " + im_dest + " " + path_tmp + "/" + root_d + ext_d)
if mask:
sct.run("cp " + mask + " " + path_tmp + "/mask.nii.gz")
# go to temporary folder
os.chdir(path_tmp)
# Split input volume along z
print "\nSplit input volume..."
sct.run(sct.fsloutput + "fslsplit " + im_input + " " + root_i + "_z -z")
# file_anat_split = ['tmp.anat_orient_z'+str(z).zfill(4) for z in range(0,nz,1)]
# Split destination volume along z
print "\nSplit destination volume..."
sct.run(sct.fsloutput + "fslsplit " + im_dest + " " + root_d + "_z -z")
# file_anat_split = ['tmp.anat_orient_z'+str(z).zfill(4) for z in range(0,nz,1)]
# Split mask volume along z
if mask:
print "\nSplit mask volume..."
sct.run(sct.fsloutput + "fslsplit mask.nii.gz mask_z -z")
# file_anat_split = ['tmp.anat_orient_z'+str(z).zfill(4) for z in range(0,nz,1)]
im_dest_img = Image(im_dest)
im_input_img = Image(im_input)
coord_origin_dest = im_dest_img.transfo_pix2phys([[0, 0, 0]])
coord_origin_input = im_input_img.transfo_pix2phys([[0, 0, 0]])
coord_diff_origin_z = coord_origin_dest[0][2] - coord_origin_input[0][2]
[[x_o, y_o, z_o]] = im_input_img.transfo_phys2pix([[0, 0, coord_diff_origin_z]])
# loop across slices
for i in range(nz):
# set masking
num = numerotation(i)
num_2 = numerotation(int(num) + z_o)
if mask:
masking = "-x mask_z" + num + ".nii"
else:
masking = ""
cmd = (
"isct_antsRegistration "
"--dimensionality 2 "
"--transform "
+ paramreg.algo
+ "["
+ paramreg.gradStep
+ ants_registration_params[paramreg.algo.lower()]
#.........这里部分代码省略.........
示例5: register_seg
# 需要导入模块: from msct_image import Image [as 别名]
# 或者: from msct_image.Image import transfo_phys2pix [as 别名]
def register_seg(seg_input, seg_dest, verbose=1):
"""Slice-by-slice registration by translation of two segmentations.
For each slice, we estimate the translation vector by calculating the difference of position of the two centers of
mass in voxel unit.
The segmentations can be of different sizes but the output segmentation must be smaller than the input segmentation.
input:
seg_input: name of moving segmentation file (type: string)
seg_dest: name of fixed segmentation file (type: string)
output:
x_displacement: list of translation along x axis for each slice (type: list)
y_displacement: list of translation along y axis for each slice (type: list)
"""
seg_input_img = Image(seg_input)
seg_dest_img = Image(seg_dest)
seg_input_data = seg_input_img.data
seg_dest_data = seg_dest_img.data
x_center_of_mass_input = [0] * seg_dest_data.shape[2]
y_center_of_mass_input = [0] * seg_dest_data.shape[2]
sct.printv('\nGet center of mass of the input segmentation for each slice '
'(corresponding to a slice in the output segmentation)...', verbose) # different if size of the two seg are different
# TODO: select only the slices corresponding to the output segmentation
# grab physical coordinates of destination origin
coord_origin_dest = seg_dest_img.transfo_pix2phys([[0, 0, 0]])
# grab the voxel coordinates of the destination origin from the source image
[[x_o, y_o, z_o]] = seg_input_img.transfo_phys2pix(coord_origin_dest)
# calculate center of mass for each slice of the input image
for iz in xrange(seg_dest_data.shape[2]):
# starts from z_o, which is the origin of the destination image in the source image
x_center_of_mass_input[iz], y_center_of_mass_input[iz] = ndimage.measurements.center_of_mass(array(seg_input_data[:, :, z_o + iz]))
# initialize data
x_center_of_mass_output = [0] * seg_dest_data.shape[2]
y_center_of_mass_output = [0] * seg_dest_data.shape[2]
# calculate center of mass for each slice of the destination image
sct.printv('\nGet center of mass of the destination segmentation for each slice ...', verbose)
for iz in xrange(seg_dest_data.shape[2]):
try:
x_center_of_mass_output[iz], y_center_of_mass_output[iz] = ndimage.measurements.center_of_mass(array(seg_dest_data[:, :, iz]))
except Exception as e:
sct.printv('WARNING: Exception error in msct_register_regularized during register_seg:', 1, 'warning')
print 'Error on line {}'.format(sys.exc_info()[-1].tb_lineno)
print e
# calculate displacement in voxel space
x_displacement = [0] * seg_input_data.shape[2]
y_displacement = [0] * seg_input_data.shape[2]
sct.printv('\nGet displacement by voxel...', verbose)
for iz in xrange(seg_dest_data.shape[2]):
x_displacement[iz] = -(x_center_of_mass_output[iz] - x_center_of_mass_input[iz]) # WARNING: in ITK's coordinate system, this is actually Tx and not -Tx
y_displacement[iz] = y_center_of_mass_output[iz] - y_center_of_mass_input[iz] # This is Ty in ITK's and fslview' coordinate systems
return x_displacement, y_displacement, None