VT - How to work with transformations¶
import vt
from timagetk.io.image import _image_from_url
/builds/J-gEBwyb/0/mosaic/timagetk/src/timagetk/components/labelled_image.py:31: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
from tqdm.autonotebook import tqdm
tmp_path = _image_from_url('https://zenodo.org/record/7149218/files/090223-p58-flo-top.lsm',
hash_value='5548917d3d1490200d0d56cbb31c0d35', hash_method='md5')
im = vt.vtImage(tmp_path)
vxs = im.spacing() # XYZ sorted voxelsize
print(vxs)
[0.2003195434808731, 0.2003195434808731, 1.0]
Create transformations¶
Identity transformation¶
id_trsf = vt.create_trsf(params=' -trsf-type rigid -identity')
print(id_trsf)
print(id_trsf.copy_to_array())
vtTransformation : {
type : RIGID_3D,
unit : real,
}
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
Random linear transformation¶
rand_linear_trsf = vt.create_trsf(im, params=' -trsf-type rigid -value random -srandom 12 -angle-range 0.0 0.1')
print(rand_linear_trsf)
print(rand_linear_trsf.copy_to_array())
vtTransformation : {
type : RIGID_3D,
unit : real,
}
[[ 0.99779585 0.06530346 -0.01178573 3.76788433]
[-0.06579991 0.99666279 -0.04830798 -7.40149029]
[ 0.00859172 0.04897701 0.99876295 -3.77669926]
[ 0. 0. 0. 1. ]]
Random non-linear transformation¶
We cannot access the non-linear transformation in VT, so we have to save the transfo with its write
method and read it with vtImage
to make sure we have indeed a vectorfield transformation of the right shape!
rand_vf_trsf = vt.create_trsf(im, params=" -trsf-type vectorfield -value sinus3D")
from tempfile import NamedTemporaryFile
with NamedTemporaryFile() as f:
fname = f.name+'.inr'
rand_vf_trsf.write(fname)
print(fname)
/tmp/tmpfl9aygo7.inr
trsf_im = vt.vtImage(fname)
print(trsf_im.copy_to_array().shape)
(59, 460, 460, 3)
Order of the last axe? XYZ or ZYX ?
Apply transformations¶
Isotropic voxel¶
# Use the `-isotropic-voxel` string parameter to performs isometric resampling of the image
id_trsf = vt.create_trsf(params=' -trsf-type rigid -identity')
iso_image = vt.apply_trsf(im, id_trsf, None, params=f'-isotropic-voxel {min(vxs)} -interpolation linear')
print(iso_image)
vtImage : {
type : unsigned char,
shape : [460, 460, 295],
spacing : [0.200320, 0.200320, 0.200320],
}
Linear transformations¶
# Apply the random linear transformation with linear interpolation
reg_image = vt.apply_trsf(im, rand_linear_trsf, None, params='-interpolation linear')
print(reg_image)
vtImage : {
type : unsigned char,
shape : [460, 460, 59],
spacing : [0.200320, 0.200320, 1.000000],
}
# Add isometric image as template to resample and apply the random linear transformation with linear interpolation
iso_reg_image = vt.apply_trsf(im, rand_linear_trsf, iso_image, params='-interpolation linear')
print(iso_reg_image)
vtImage : {
type : unsigned char,
shape : [460, 460, 295],
spacing : [0.200320, 0.200320, 0.200320],
}
Non-linear transformation¶
# Apply the random vectorfield transformation with linear interpolation
reg_image = vt.apply_trsf(im, rand_vf_trsf, None, params='-interpolation linear')
print(reg_image)
vtImage : {
type : unsigned char,
shape : [460, 460, 59],
spacing : [0.200320, 0.200320, 1.000000],
}
# Add isometric image as template to resample and apply the random vectorfield transformation with linear interpolation
iso_reg_image = vt.apply_trsf(im, rand_vf_trsf, iso_image, params='-interpolation linear')
print(iso_reg_image)
None
BAL_ResampleImage: result image and vector field transformation have different dimensions
result image = [460 460 295]
transformation = [460 460 59]
API_applyTrsf: unable to compute resampling
API_applyTrsf(vtImageBridge*, vtTransformationBridge*, vtImageBridge*, const std::string&, const std::string&): unable to resample image
Providing a template (or ref
) image to vt.apply_trsf
does not work!