1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://www.centreleonberard.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
18 #ifndef VVIMAGEREADER_CXX
19 #define VVIMAGEREADER_CXX
21 #include <itkImageFileReader.h>
22 #include "vvImageReader.h"
23 #include "vvImageReader.txx"
24 #include "clitkTransformUtilities.h"
25 #include "clitkElastix.h"
27 //------------------------------------------------------------------------------
28 vvImageReader::vvImageReader()
31 mInputFilenames.resize(0);
33 mType = UNDEFINEDIMAGETYPE;
36 //------------------------------------------------------------------------------
39 //------------------------------------------------------------------------------
40 vvImageReader::~vvImageReader() { }
41 //------------------------------------------------------------------------------
44 //------------------------------------------------------------------------------
45 void vvImageReader::Update()
49 //------------------------------------------------------------------------------
52 //------------------------------------------------------------------------------
53 void vvImageReader::Update(LoadedImageType type)
55 itk::ImageIOBase::Pointer reader = itk::ImageIOFactory::CreateImageIO(mInputFilenames[0].c_str(), itk::ImageIOFactory::ReadMode);
57 mLastError="Unable to read file.";
59 reader->SetFileName(mInputFilenames[0]);
60 reader->ReadImageInformation();
61 if (mInputFilenames.size() > 1)
62 Update(reader->GetNumberOfDimensions()+1,reader->GetComponentTypeAsString(reader->GetComponentType()),type);
63 else if (reader->GetNumberOfComponents() > 1 && type != VECTORFIELD && type != VECTORFIELDWITHTIME)
64 Update(reader->GetNumberOfDimensions()+1,reader->GetComponentTypeAsString(reader->GetComponentType()),VECTORPIXELIMAGE);
66 Update(reader->GetNumberOfDimensions(),reader->GetComponentTypeAsString(reader->GetComponentType()),type);
69 //------------------------------------------------------------------------------
72 //------------------------------------------------------------------------------
73 void vvImageReader::Update(int dim,std::string inputPixelType, LoadedImageType type)
75 //CALL_FOR_ALL_DIMS(dim,UpdateWithDim,inputPixelType);
78 mInputPixelType=inputPixelType;
82 UpdateWithDim<2>(mInputPixelType);
85 UpdateWithDim<3>(mInputPixelType);
88 UpdateWithDim<4>(mInputPixelType);
91 std::cerr << "dimension unknown in Update ! " << std::endl;
94 //------------------------------------------------------------------------------
97 //------------------------------------------------------------------------------
98 void vvImageReader::SetInputFilename(const std::string & filename)
100 mInputFilenames.resize(0);
101 mInputFilenames.push_back(filename);
103 //------------------------------------------------------------------------------
106 //------------------------------------------------------------------------------
107 void vvImageReader::SetInputFilenames(const std::vector<std::string> & filenames)
109 mInputFilenames = filenames;
111 //------------------------------------------------------------------------------
114 //------------------------------------------------------------------------------
115 //Read transformation in NKI format (Xdr, transposed, cm)
116 //void vvImageReader::ReadNkiImageTransform()
119 // std::string filename = mInputFilenames[0]+".MACHINEORIENTATION";
120 // if(itksys::SystemTools::FileExists(filename.c_str())){
121 // typedef itk::ImageFileReader< itk::Image< double, 2 > > MatrixReaderType;
122 // MatrixReaderType::Pointer readerTransfo = MatrixReaderType::New();
123 // readerTransfo->SetFileName(filename);
126 // readerTransfo->Update();
127 // } catch( itk::ExceptionObject & err ) {
129 // std::cerr << "Cannot read " << filename << std::endl
130 // << "The error is: " << err << std::endl;
134 // //Transpose matrix (NKI format)
135 // for(int j=0; j<4; j++)
136 // for(int i=0; i<4; i++)
137 // mImage->GetTransform()->GetMatrix()->SetElement(j,i,readerTransfo->GetOutput()->GetBufferPointer()[4*i+j]);
140 // for(int i=0; i<3; i++)
141 // mImage->GetTransform()->GetMatrix()->SetElement(i,3,10*mImage->GetTransform()->GetMatrix()->GetElement(i,3));
143 // mImage->GetTransform()->Inverse();
147 //------------------------------------------------------------------------------
150 //------------------------------------------------------------------------------
151 //Read transformation in ASCII format
152 void vvImageReader::ReadMatImageTransform()
154 std::string filename(mInputFilenames[0]);
155 std::string ext(itksys::SystemTools::GetFilenameLastExtension(filename));
157 // Try a ".mat" extension
158 if (ext.length() > 0) {
159 size_t pos = filename.rfind(ext);
160 filename.replace(pos, ext.length(), ".mat");
164 std::ifstream f(filename.c_str());
165 itk::Matrix<double, 4, 4> itkMat;
166 bool itkMatRead = false;
170 itkMat.SetIdentity();
172 itkMat = clitk::ReadMatrix3D(filename);
174 catch (itk::ExceptionObject & err) {
175 itkWarningMacro(<< "Found " << filename
176 << " but this is not a 4x4 matrix so it is ignored.");
182 // Try a ".elx" extension
183 filename = mInputFilenames[0];
184 if (ext.length() > 0) {
185 size_t pos = filename.rfind(ext);
186 filename.replace(pos, ext.length(), ".elx");
190 f.open(filename.c_str());
191 if(!itkMatRead && f.is_open()) {
193 itkMat = clitk::createMatrixFromElastixFile<3>(filename, true);
198 vtkSmartPointer<vtkMatrix4x4> matrix = vtkSmartPointer<vtkMatrix4x4>::New();
200 for(int j=0; j<4; j++)
201 for(int i=0; i<4; i++)
202 matrix->SetElement(j,i,itkMat[j][i]);
205 // For some reason, the transformation matrix given in the MHD
206 // file is inverted wrt the transformation given in the MAT file.
207 // We don't really know where the inversion takes place inside
208 // VTK or ITK. For what we could see in VV, the transformation
209 // given in the MHD file seems "more correct" than that given in
210 // the MAT file. For this reason, we invert the matrix read from
211 // the MAT file before concatenating it to the current transformation.
212 // Still, it would be nice to find out what happens exactly between
216 // We actually use vtkImageReslice which takes the inverse transform
217 // as input. So it seems more correct to inverse the matrix given by
218 // itk::ImageBase< VImageDimension >::GetDirection() than the matrix
219 // in .mat which is indeed the inverse optimized by a typical
220 // affine registration algorithm.
221 // Validated using clitkAffineTransform --transform_grid
222 if (matrix->Determinant() == 0)
224 vtkGenericWarningMacro("Matrix in " << filename.c_str() << " cannot be inverted (determinant = 0)");
227 // TODO SR and BP: check on the list of transforms and not the first only
228 mImage->GetTransform()[0]->PreMultiply();
229 mImage->GetTransform()[0]->Concatenate(matrix);
230 mImage->GetTransform()[0]->Update();
232 //for image sequences, apply the transform to each images of the sequence
233 if (mImage->IsTimeSequence())
235 for (unsigned i = 1 ; i<mImage->GetTransform().size() ; i++)
237 mImage->GetTransform()[i]->PreMultiply();
238 mImage->GetTransform()[i]->Concatenate(matrix);
239 mImage->GetTransform()[i]->Update();
245 //------------------------------------------------------------------------------