transform a set of points does not work

573 views
Skip to first unread message

Ibrahim A

unread,
Apr 4, 2018, 11:28:12 AM4/4/18
to elastix-imageregistration
Dear all,

I registered two images successfully using 3D rigid transform as the transform component. I took a set of points from the moving image and tried to transform it using transformix and the resulted transform file:
transformix -def movingPoints.txt -out outFolder -tp outFolder/TransformParameters.0.txt -m FixedImage.nrrd
I got new set of points but when I visualize them e.g. using 3D Slicer, they are not aligned correctly. 

I tested transformix using the same images, it works (images are aligned correctly in 3D Slicer).  
transformix -in MovingImage.nrrd -out outFolder -tp outFolder/TransformParameters.0.txt -m FixedImage.nrrd

Why transformix works on images but not on points from the same images?

I tried another approach that also didn't work (points are also not aligned), I wrote a simple python script (bellow) that took the set of points and transform them using the parameters I got from elastix.

What I am missing or doing wrong? 

Thanks!
Ibraheem

import numpy as np
import sys, math 
print("******************************************************")
print ("   Transform Points ")
print("******************************************************")
np.set_printoptions(precision=2)
np.set_printoptions(suppress=True)
print("---------------------------------------------------")
print("   Define input ")
print("---------------------------------------------------")
# Input Points 
p=np.array([[-1.1499  , 4.96146 ,-3.73897  ],[ 1.12107 , 6.15124 ,-4.09615  ],   [ 2.56265 , 7.01562 ,-4.0416   ],   [ 4.1089  , 7.87024 ,-3.37502  ],   [ 5.23609 , 8.08297 ,-1.92341  ],   [ 5.37945 , 7.84559 , 0.0562932],   [ 4.72791 , 6.72648 , 1.12291  ],   [ 3.17306 , 5.88972 , 1.40773  ],   [ 2.09212 , 5.46035 , 0.298082],   [ 1.39364 , 5.93    ,-0.860716],   [ 1.80359 , 6.98823 ,-1.9811  ],   [ 2.79237 , 8.16489 ,-1.89504 ],   [ 3.54371 , 8.4603  ,-0.688138],   [ 3.18755 , 8.01944 , 0.458005],   [ 1.99834 , 7.59242 , 0.468096],   [ 1.50188 , 7.8538  ,-0.489043],   [ 2.28302 , 8.5749  ,-0.594259]])
print(p)
#elastix parameters
ep=np.array([ -0.369236, -0.074973, 0.177428, -0.895437, -7.486193, -9.989277])
print("---------------------------------------------------")
print("   elastixParameters to Rotation Matrix ")
print("---------------------------------------------------")
[rx,ry,rz]=ep[0:3];
[tx,ty,tz]=ep[3:6];
Ms = []
cosx = math.cos(rx)
sinx = math.sin(rx)
Ms.append(np.array([[1, 0   ,  0   ],
                    [0, cosx, -sinx],
                    [0, sinx,  cosx]]))
cosy = math.cos(ry)
siny = math.sin(ry)
Ms.append(np.array( [[cosy , 0, siny],
                     [0    , 1, 0   ],
                     [-siny, 0, cosy]]))
cosz = math.cos(rz)
sinz = math.sin(rz)
Ms.append(np.array([[cosz, -sinz, 0],
                    [sinz,  cosz, 0],
                    [0   ,   0  , 1] ]))
if Ms:
        mt = reduce(np.dot, Ms[::-1])
        # convert to 4x4
        m=   np.zeros((4,4)) ; m[3,3]=1 ;        m[0:3,0:3]=mt
        # add translations 
        m[0:3,3]=[tx,ty,tz]
else:    
        m=  np.eye(3)
print("---------------------------------------------------")
print("   displacement field ")
print("---------------------------------------------------")
# get the displacement vector = len(p) 
d=np.zeros((len(p),4))
#print(p_dis)
for i in range(0,len(p)):
    dt=np.ones(4); dt[0:3] = dt[0:3] * p[i]
    #print(dt)    
    np.matmul(m,dt,d[i])
    print(d[i])
print("---------------------------------------------------")
print("    new transformed points ")
print("---------------------------------------------------")
# get the new points locations 
pn=np.zeros((len(p),3))
for i in range(0,len(p)):
    pn[i]= p[i] + d[i][0:3]
    print(pn[i])        

gökhan günay

unread,
Apr 4, 2018, 12:24:50 PM4/4/18
to elastix-imag...@googlegroups.com, ibr...@gmail.com
Hi Ibrahim.
In the registration, fixed image coordinate system is transformed to moving image coordinate system to get the spatial correspondence between the two images. So transformix would work fine when you want to transform points in fixed image to moving image domain. In your case, you are trying to do opposite where you get the error. 
One possible approach (may be tedious) to do so is:
1- Prepare an empty 3d volume in moving image domain.
2- Place small spheres whose centers are the points to be transformed and voxel intensities degrade by getting far from the center. So the center voxels have the highest intensity.
3- Transform this image using transformix as you transform the moving image.
4- Seek for the highest intensity voxels in the deformed image volume. These points are those points you are actually looking for.

Another approach is getting inverse transform of your registrations's transformation and then applying transfomix, but I have never followed this way in my previous experiments. So I cannot recommend anything for this option.

Best,
Gokhan

--
You received this message because you are subscribed to the Google Groups "elastix-imageregistration" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elastix-imageregistration+unsubsc...@googlegroups.com.
Visit this group at https://groups.google.com/group/elastix-imageregistration.
To view this discussion on the web visit https://groups.google.com/d/msgid/elastix-imageregistration/afba5081-14b3-4861-a9b9-877e03967246%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Cristiano

unread,
May 22, 2020, 10:54:23 AM5/22/20
to elastix-imageregistration
Hello,

I am having difficulties with Transformix which may or may not be related to the issues that Ibrahim had.

I have two images after reading in two series of DICOMs with the following features:

FixedIm Dtype = 32-bit float
MovingIm Dtype = 32-bit float

FixedIm Size = (212, 256, 30)
MovingIm Size = (192, 256, 35)

FixedIm Spacing = (0.8984375, 0.8984375, 5.0)
MovingIm Spacing = (0.9375, 0.9375, 5.0)

FixedIm Origin = (-104.92378234863281, -152.4906463623047, -9.22148609161377)
MovingIm Origin = (-115.14457702636719, -124.83148193359375, -10.8151273727417)

FixedIm (min, max) x-pos = -104.92378234863281, 85.54496765136719 FixedIm (min, max) y-pos = -152.4906463623047, 77.50935363769531 FixedIm (min, max) z-pos = -9.22148609161377, 140.77851390838623 MovingIm (min, max) x-pos = -115.14457702636719, -82.33207702636719 MovingIm (min, max) y-pos = -124.83148193359375, 115.16851806640625 MovingIm (min, max) z-pos = -10.8151273727417, 164.1848726272583 RegIm (min, max) x-pos = -104.92378234863281, 85.54496765136719 RegIm (min, max) y-pos = -152.4906463623047, 77.50935363769531 RegIm (min, max) z-pos = -9.22148609161377, 140.77851390838623

I also have a DICOM-RTSTRUCT file containing contour points belonging to FixedIm. I've already transformed the contour points to physical coordinates taking into account the ImagePositionPatient, ImageOrientationPatient, PixelSize, etc. I registered MovingIm to FixedIm and I’m satisfied with the results.


20200515_MovingIm_reg_to_FixedIm_mid-stacks.jpg

The plot above shows the result at one slice location.  It may be difficult to see them, but Fixedm and RegIm have contours (in red) overlaid.  The contour points are those belonging to FixedIm as described above.


I wanted to then transform the contour points so they can be overlaid onto MovingIm.  I created a file called inputpoints.pcs with the contour points in the required format:

point
number_of_points
point1_x point1_y point1_z
point2_x point2_y point2_z

Then I transformed the contour points:


TransformixImFilt = sitk.TransformixImageFilter()
TransformixImFilt.SetTransformParameterMap(ElastixImFilt.GetTransformParameterMap())
TransformixImFilt.SetFixedPointSetFileName(FixedContourPtsFname)
# Need to explicitely tell elastix that the image is 3D since by default it will
# only transform to 2D:
TransformixImFilt.SetMovingImage(MovingIm)
TransformixImFilt.Execute()


But when I opened the outputpoints.txt file it became apparent that something is wrong (including perhaps my interpretation) - the x and y indices exceed the dimensions of the images themselves. Following was the first line of outputpoints.txt:

Point 0 ; InputIndex = [ 252 359 14 ] ; InputPoint = [ 126.344037 171.830275 -54.501356 ] ; OutputIndexFixed = [ 248 353 26 ] ; OutputPoint = [ 126.638100 184.489069 3.193988 ] ; Deformation = [ 0.294064 12.658794 57.695343 ] ; OutputIndexMoving = [ 220 356 4 ]

As for the z indices, although they do not exceed the z-dimension, the input and output indices differ by ~10, whereas the reality they should differ by a few slice positions.


Does anyone have any idea why the x and y values of InputIndex, OutputIndexFixed and OutputIndexMoving exceed the x and y dimensions of FixedIm, FixedIm and MovingIm respectively?  Likewise, InputPoint and OutputPoint exceed the physical dimensions of the FixedIm and MovingIm.


Has something gone wrong or have I completely misinterpretted what the indices and points in outputpoints.txt represent?


If the latter, can someone please direct me to a source of info since I've been unable to find any level of detail from the manual that covers the output file.


Best regards,

Cristiano


To unsubscribe from this group and stop receiving emails from it, send an email to elastix-imageregistration+unsub...@googlegroups.com.

Ibrahim A

unread,
May 22, 2020, 4:25:20 PM5/22/20
to elastix-imag...@googlegroups.com
You can use the image approach above but I found out it is better to use the transform module in 3D Slicer [1]. 
1. Convert the points to fiducials using 3D Slicer
2. Generate a deformation field using transformix.
3. Load the deformation field to Slicer as transform then apply it to the fiducial points. 

The above 3 steps can be automated via a simple python script. 


To unsubscribe from this group and stop receiving emails from it, send an email to elastix-imageregis...@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "elastix-imageregistration" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elastix-imageregistration/zMs_jA3YZ9w/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elastix-imageregis...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elastix-imageregistration/46328d8f-7db2-4af8-afe4-6f651151cc0f%40googlegroups.com.

Cristiano

unread,
May 23, 2020, 6:47:19 AM5/23/20
to elastix-imageregistration
Thanks very much for the tips Ibrahim. I'll look into that.

I'm curious to know what led you to take that approach? Did you find Elastix/SimpleElastix too difficult to get to work?

Regards,
Cristiano

Reply all
Reply to author
Forward
0 new messages