# Troble loading and reading LinearTransform.h5

**URL:** https://discourse.itk.org/t/troble-loading-and-reading-lineartransform-h5/3008
**Category:** Engineering
**Tags:** registration, transformfileread
**Created:** [April 26, 2020, 12:56am UTC](https://discourse.itk.org/t/troble-loading-and-reading-lineartransform-h5/3008 "2020-04-26T00:56:38Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![machadoL](https://discourse.itk.org/user_avatar/discourse.itk.org/machadol/32/662_2.png) [@machadoL](https://discourse.itk.org/u/machadoL)
#### Post date: [April 26, 2020, 12:56am UTC](https://discourse.itk.org/t/troble-loading-and-reading-lineartransform-h5/3008/1 "2020-04-26T00:56:38Z")

</div>

Hello, everyone.

I am implementing a pipeline to load a `LinearTransform.h5` and convert that into a displacement field. However, I am having a problem loading the transform into a real transform object-pointer to proceed. Please take a look into (part of) code:

```
#include "itkVersorRigid3DTransform.h"

#include <iostream>
#include <fstream>

#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"

#include "itkTransformFileReader.h"
#include "itkImageRegionIterator.h"

    > // Writing Transform
    > using TransformReaderType = itk::TransformFileReaderTemplate<double>;
    > TransformReaderType::Pointer transformReader = TransformReaderType::New();
    > transformReader->SetFileName(argv[3]);
    > transformReader->Update();
    > 
    > TransformReaderType::TransformListType * transforms = transformReader->GetTransformList();
    > auto it = transforms->begin();
    > std::cout<< "Size:" << (*it). << std::endl;
    > 
    > // Creating and writing the displacement vector image
    > // Based on example: https://itk.org/Doxygen/html/Examples_2RegistrationITKv3_2DeformableRegistration8_8cxx-example.html
    > 
    > using VectorType = itk::Vector<float, Dimension>;
    > using DisplacementFieldType = itk::Image<VectorType, Dimension>;
    > DisplacementFieldType::Pointer field = DisplacementFieldType::New();
    > field->SetRegions(fixedImageReader->GetOutput()->GetBufferedRegion());
    > field->SetSpacing(fixedImageReader->GetOutput()->GetSpacing());
    > field->SetOrigin(fixedImageReader->GetOutput()->GetOrigin());
    > field->SetDirection(fixedImageReader->GetOutput()->GetDirection());
    > field->Allocate();
    > 
    > using FieldIteratorType = itk::ImageRegionIterator<DisplacementFieldType>;
    > FieldIteratorType fi( field, movingImageReader->GetOutput()->GetBufferedRegion());
    > fi.GoToBegin();
    > 
    > using TransformType = itk::VersorRigid3DTransform<double>;
    > TransformType::InputPointType movingPoint;
    > TransformType::OutputPointType transformedPoint;
    > DisplacementFieldType::IndexType index;
    > TransformType::Pointer transform = TransformType::New();
    > 
    > 
    > VectorType displacement;
    > 
    > while( !fi.IsAtEnd()){
    > index = fi.GetIndex();
    > field->TransformIndexToPhysicalPoint( index, movingPoint );
    > transforms->begin();
    > transformedPoint = transforms-> ->TransformPoint( movingPoint );
    > displacement = transformedPoint - movingPoint;
    > fi.Set(displacement);
    > ++fi;
    > }
    > 
    > using FieldWriterType = itk::ImageFileWriter<DisplacementFieldType>;
    > FieldWriterType::Pointer fieldWriter = FieldWriterType::New();
    > fieldWriter->SetInput(field);
    > fieldWriter->SetFileName(imagePath + "dispField-q=" + id + ".nrrd");
    > fieldWriter->Update();
    >   

```

Thanks in advance!

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [April 27, 2020, 1:56pm UTC](https://discourse.itk.org/t/troble-loading-and-reading-lineartransform-h5/3008/2 "2020-04-27T13:56:11Z")

</div>

And what is the problem with this code?

---

<div class="post-metadata">

### Author: ![machadoL](https://discourse.itk.org/user_avatar/discourse.itk.org/machadol/32/662_2.png) [@machadoL](https://discourse.itk.org/u/machadoL)
#### Post date: [April 27, 2020, 5:52pm UTC](https://discourse.itk.org/t/troble-loading-and-reading-lineartransform-h5/3008/3 "2020-04-27T17:52:13Z")

</div>

```
using TransformType = itk::VersorRigid3DTransform<double>;
TransformType::InputPointType movingPoint; 
TransformType::OutputPointType transformedPoint; 
DisplacementFieldType::IndexType index; 
TransformType::Pointer transform = TransformType::New();

```

The problem is : I do not know how to connect correctly `the transform-input-file` that I read with the `transform pointer object` that i created in this last line (above).

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [April 27, 2020, 5:53pm UTC](https://discourse.itk.org/t/troble-loading-and-reading-lineartransform-h5/3008/4 "2020-04-27T17:53:56Z")

</div>

Have you seen [this example](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Examples/IO/TransformReadWrite.cxx)?

---

<div class="post-metadata">

### Author: ![machadoL](https://discourse.itk.org/user_avatar/discourse.itk.org/machadol/32/662_2.png) [@machadoL](https://discourse.itk.org/u/machadoL)
#### Post date: [April 27, 2020, 6:01pm UTC](https://discourse.itk.org/t/troble-loading-and-reading-lineartransform-h5/3008/5 "2020-04-27T18:01:53Z")

</div>

Yes, I did read.  
Although I got confused with the Idea of reading a List o transforms. I am loading just one. Do we always have to go through a loop to read a single object?

---

<div class="post-metadata">

### Author: ![machadoL](https://discourse.itk.org/user_avatar/discourse.itk.org/machadol/32/662_2.png) [@machadoL](https://discourse.itk.org/u/machadoL)
#### Post date: [April 27, 2020, 6:41pm UTC](https://discourse.itk.org/t/troble-loading-and-reading-lineartransform-h5/3008/6 "2020-04-27T18:41:53Z")

</div>

I guess I got it. I read, and then I do a cast to the type I am working (Versor3D).  
Although, when I ask the object to print its name class and type it prints as the reader did (Affine), not the type I casted (Versor3D).

Any suggestion?  
Thanks.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [April 27, 2020, 6:55pm UTC](https://discourse.itk.org/t/troble-loading-and-reading-lineartransform-h5/3008/7 "2020-04-27T18:55:51Z")

</div>

If affine is what is written in the file, and you want versor, you could read an affine transform and then [convert it into versor representation](https://github.com/BRAINSia/BRAINSTools/blob/a8c0c926a2c183b37786100f0d4d0ceb6725e9b4/BRAINSCommonLib/ConvertToRigidAffine.h#L398-L460).

---

<div class="post-metadata">

### Author: ![machadoL](https://discourse.itk.org/user_avatar/discourse.itk.org/machadol/32/662_2.png) [@machadoL](https://discourse.itk.org/u/machadoL)
#### Post date: [April 27, 2020, 7:04pm UTC](https://discourse.itk.org/t/troble-loading-and-reading-lineartransform-h5/3008/8 "2020-04-27T19:04:35Z")

</div>

All right I did exactly as the example mentioned, Sounds to work at the beginning, But it yelds an error:

```
tk::ERROR: ImageFileWriter(0x559f0f047c70): Largest possible region does not fully contain requested paste IO regionPaste IO region: ImageIORegion (0x7fff26c0cf20)
      Dimension: 3
      Index: 0 0 0 
      Size: 0 0 0 
    Largest possible region: ImageRegion (0x7fff26c0cfc0)
      Dimension: 3
      Index: [0, 0, 0]
      Size: [0, 0, 0]

```

Do not know why.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [April 28, 2020, 5:14pm UTC](https://discourse.itk.org/t/troble-loading-and-reading-lineartransform-h5/3008/9 "2020-04-28T17:14:13Z")

</div>

That error is coming from an `Image`, not a `Transform`, suggesting the mistake is elsewhere.

---

<div class="post-metadata">

### Author: ![bearbenbin](https://discourse.itk.org/user_avatar/discourse.itk.org/bearbenbin/32/1806_2.png) [@bearbenbin](https://discourse.itk.org/u/bearbenbin)
#### Post date: [November 28, 2020, 1:45am UTC](https://discourse.itk.org/t/troble-loading-and-reading-lineartransform-h5/3008/10 "2020-11-28T01:45:55Z")

</div>

So, what’s the reason and how to deal with it?
