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);
64 Update(reader->GetNumberOfDimensions(),reader->GetComponentTypeAsString(reader->GetComponentType()),type);
67 //------------------------------------------------------------------------------
70 //------------------------------------------------------------------------------
71 void vvImageReader::Update(int dim,std::string inputPixelType, LoadedImageType type)
73 //CALL_FOR_ALL_DIMS(dim,UpdateWithDim,inputPixelType);
76 mInputPixelType=inputPixelType;
80 UpdateWithDim<2>(mInputPixelType);
83 UpdateWithDim<3>(mInputPixelType);
86 UpdateWithDim<4>(mInputPixelType);
89 std::cerr << "dimension unknown in Update ! " << std::endl;
92 //------------------------------------------------------------------------------
95 //------------------------------------------------------------------------------
96 void vvImageReader::SetInputFilename(const std::string & filename)
98 mInputFilenames.resize(0);
99 mInputFilenames.push_back(filename);
101 //------------------------------------------------------------------------------
104 //------------------------------------------------------------------------------
105 void vvImageReader::SetInputFilenames(const std::vector<std::string> & filenames)
107 mInputFilenames = filenames;
109 //------------------------------------------------------------------------------
112 //------------------------------------------------------------------------------
113 //Read transformation in NKI format (Xdr, transposed, cm)
114 //void vvImageReader::ReadNkiImageTransform()
117 // std::string filename = mInputFilenames[0]+".MACHINEORIENTATION";
118 // if(itksys::SystemTools::FileExists(filename.c_str())){
119 // typedef itk::ImageFileReader< itk::Image< double, 2 > > MatrixReaderType;
120 // MatrixReaderType::Pointer readerTransfo = MatrixReaderType::New();
121 // readerTransfo->SetFileName(filename);
124 // readerTransfo->Update();
125 // } catch( itk::ExceptionObject & err ) {
127 // std::cerr << "Cannot read " << filename << std::endl
128 // << "The error is: " << err << std::endl;
132 // //Transpose matrix (NKI format)
133 // for(int j=0; j<4; j++)
134 // for(int i=0; i<4; i++)
135 // mImage->GetTransform()->GetMatrix()->SetElement(j,i,readerTransfo->GetOutput()->GetBufferPointer()[4*i+j]);
138 // for(int i=0; i<3; i++)
139 // mImage->GetTransform()->GetMatrix()->SetElement(i,3,10*mImage->GetTransform()->GetMatrix()->GetElement(i,3));
141 // mImage->GetTransform()->Inverse();
145 //------------------------------------------------------------------------------
148 //------------------------------------------------------------------------------
149 //Read transformation in ASCII format
150 void vvImageReader::ReadMatImageTransform()
152 std::string filename(mInputFilenames[0]);
153 std::string ext(itksys::SystemTools::GetFilenameLastExtension(filename));
155 // Try a ".mat" extension
156 if (ext.length() > 0) {
157 size_t pos = filename.rfind(ext);
158 filename.replace(pos, ext.length(), ".mat");
162 std::ifstream f(filename.c_str());
163 itk::Matrix<double, 4, 4> itkMat;
164 bool itkMatRead = false;
168 itkMat.SetIdentity();
170 itkMat = clitk::ReadMatrix3D(filename);
172 catch (itk::ExceptionObject & err) {
173 itkWarningMacro(<< "Found " << filename
174 << " but this is not a 4x4 matrix so it is ignored.");
180 // Try a ".elx" extension
181 filename = mInputFilenames[0];
182 if (ext.length() > 0) {
183 size_t pos = filename.rfind(ext);
184 filename.replace(pos, ext.length(), ".elx");
188 f.open(filename.c_str());
189 if(!itkMatRead && f.is_open()) {
191 std::vector<std::string> l;
192 l.push_back(filename);
193 itkMat = clitk::createMatrixFromElastixFile<3>(l, 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 //------------------------------------------------------------------------------