VirtualPlants / timagetk

3D and 2D Image processing algorithms in Python for tissues
https://virtualplants.github.io/timagetk
Other
13 stars 5 forks source link

Apply registration transformation to another image #18

Open rosscarter3 opened 6 years ago

rosscarter3 commented 6 years ago

Hello, Is there a way within the python framework to apply the transformation matrices returned by the image registration functions to a different spacial image? This would be a very useful feature to have if not. Regards, Ross

gcerutti commented 6 years ago

Hi Ross, There definitely is! If you're just looking for a way to apply the Transformation object on another image, the apply_trsf will do the trick.

Here is a little example on how to use it, for instance if you want to compute a transform on signal images and transform the already segmented image.

from timagetk.algorithms.blockmatching import blockmatching
from timagetk.algorithms.trsf import apply_trsf

reference_img = ...
floating_img = ...

reference_seg_img = ...
floating_seg_img = ...

transform, transform_img = blockmatching(floating_img, reference_img, param_str_1 ='-trsf-type rigid')

transform_seg_img = apply_trsf(floating_seg_img,transform,template_img=reference_seg_img,param_str_2='-resize -interpolation nearest')

Just take into account that it will introduce zeros outside the frame of the transformed image.

If you are looking for a way to do it with a matrix of your own, it is also possible but a bit less direct...

Just let us know if that works for you!

rosscarter3 commented 6 years ago

Thanks Guillaume, That's exactly what I needed! Cheers, Ross

rosscarter3 commented 6 years ago

Hello again, Now I'm having trouble loading a saved transformation from a file. I think I've got the wrong syntax, I'm doing:

(trsf_2, def_im_2) = register_images(def_im_1, im2, method='deformable_registration') trsf_2.write(trsf2_path)

and later:

trsf_2 = BalTransformation() trsf_2.read(trsf2_path)

but it's throwing a segmentation fault. Any suggestions? Cheers, Ross

gcerutti commented 6 years ago

You might need to do something like this:

from timagetk.algorithms.trsf import BalTransformation, create_trsf

trsf_2 = create_trsf(template_img=im2, param_str_2='-identity', trsf_type=BalTransformation.VECTORFIELD_3D, trsf_unit=BalTransformation.REAL_UNIT)
trsf_2.read(trsf2_path)

Basically initializing the memory so that the read does not create the segfault...

Tell me if it fixes your error!

rosscarter3 commented 6 years ago

That's excellent, thanks so much for your help! Ross

rosscarter3 commented 6 years ago

Hi Guillaume, thanks for your help so far, still getting errors though. My code is a s follows:

if os.path.exists(trsf1_path): trsf_1 = create_trsf(template_img=im1, param_str_2='-identity', trsf_type=BalTransformation.AFFINE_3D, trsf_unit=BalTransformation.REAL_UNIT) trsf_1.read(trsf1_path) def_im_1 = apply_trsf(im1, bal_transformation=trsf_1)

The bal_transformation appears to be initialised properly, but I still get a segfault at the trsf_1.read() stage...

Cheers, Ross

gcerutti commented 6 years ago

So i guess the transform in trsf1_path is an affine one? This is not something i have worked with a lot so i don't have the solution right here. But i might have a workaround i used for rigid transforms.

Essentially, the transform can be summed up as a 4x4 homogeneous matrix that you can recover and save when you compute the transform:

import numpy as np

trsf_2, def_im_2 = blockmatching(def_im_1, im2, param_str_1 ='-trsf-type rigid')
matrix = trsf_2.mat.to_np_array()
trsf2_path = "***.csv"
np.savetxt(trsf2_path, matrix, delimiter=";")

Then i have been able to get a working bal_transformation from this saved file, using a piece of code like this one:

from timagetk.algorithms.trsf import create_trsf, allocate_c_bal_matrix

matrix = np.loadtxt(trsf2_path, delimiter=";")
trsf_2 = create_trsf(param_str_2='-identity', trsf_type=BalTransformation.RIGID_3D, trsf_unit=BalTransformation.REAL_UNIT)
allocate_c_bal_matrix(trsf_2.mat.c_struct, matrix)

Could you try something in that spirit with the affine registration?

jlegrand62 commented 6 years ago

Hello @rosscarter3,

Sorry for the late reply, we finally fixed the bug and I made a Pull request #19. Unfortunately, I am not a listed contributor of this repository and I cannot integrate it here. However, we are developing a fork under GitLab: https://gitlab.inria.fr/mosaic/timagetk.