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"
26 //------------------------------------------------------------------------------
27 vvImageReader::vvImageReader()
30 mInputFilenames.resize(0);
32 mType = UNDEFINEDIMAGETYPE;
35 //------------------------------------------------------------------------------
38 //------------------------------------------------------------------------------
39 vvImageReader::~vvImageReader() { }
40 //------------------------------------------------------------------------------
43 //------------------------------------------------------------------------------
44 void vvImageReader::Update()
48 //------------------------------------------------------------------------------
51 //------------------------------------------------------------------------------
52 void vvImageReader::Update(LoadedImageType type)
54 itk::ImageIOBase::Pointer reader = itk::ImageIOFactory::CreateImageIO(mInputFilenames[0].c_str(), itk::ImageIOFactory::ReadMode);
56 mLastError="Unable to read file.";
58 reader->SetFileName(mInputFilenames[0]);
59 reader->ReadImageInformation();
60 if (mInputFilenames.size() > 1)
61 Update(reader->GetNumberOfDimensions()+1,reader->GetComponentTypeAsString(reader->GetComponentType()),type);
63 Update(reader->GetNumberOfDimensions(),reader->GetComponentTypeAsString(reader->GetComponentType()),type);
66 //------------------------------------------------------------------------------
69 //------------------------------------------------------------------------------
70 void vvImageReader::Update(int dim,std::string inputPixelType, LoadedImageType type)
72 //CALL_FOR_ALL_DIMS(dim,UpdateWithDim,inputPixelType);
75 mInputPixelType=inputPixelType;
79 UpdateWithDim<2>(mInputPixelType);
82 UpdateWithDim<3>(mInputPixelType);
85 UpdateWithDim<4>(mInputPixelType);
88 std::cerr << "dimension unknown in Update ! " << std::endl;
91 //------------------------------------------------------------------------------
94 //------------------------------------------------------------------------------
95 void vvImageReader::SetInputFilename(const std::string & filename)
97 mInputFilenames.resize(0);
98 mInputFilenames.push_back(filename);
100 //------------------------------------------------------------------------------
103 //------------------------------------------------------------------------------
104 void vvImageReader::SetInputFilenames(const std::vector<std::string> & filenames)
106 mInputFilenames = filenames;
108 //------------------------------------------------------------------------------
111 //------------------------------------------------------------------------------
112 //Read transformation in NKI format (Xdr, transposed, cm)
113 void vvImageReader::ReadNkiImageTransform()
116 std::string filename = mInputFilenames[0]+".MACHINEORIENTATION";
117 if(itksys::SystemTools::FileExists(filename.c_str())){
118 typedef itk::ImageFileReader< itk::Image< double, 2 > > MatrixReaderType;
119 MatrixReaderType::Pointer readerTransfo = MatrixReaderType::New();
120 readerTransfo->SetFileName(filename);
123 readerTransfo->Update();
124 } catch( itk::ExceptionObject & err ) {
126 std::cerr << "Cannot read " << filename << std::endl
127 << "The error is: " << err << std::endl;
131 //Transpose matrix (NKI format)
132 for(int j=0; j<4; j++)
133 for(int i=0; i<4; i++)
134 mImage->GetTransform()->GetMatrix()->SetElement(j,i,readerTransfo->GetOutput()->GetBufferPointer()[4*i+j]);
137 for(int i=0; i<3; i++)
138 mImage->GetTransform()->GetMatrix()->SetElement(i,3,10*mImage->GetTransform()->GetMatrix()->GetElement(i,3));
140 mImage->GetTransform()->Inverse();
144 //------------------------------------------------------------------------------
147 //------------------------------------------------------------------------------
148 //Read transformation in ASCII format
149 void vvImageReader::ReadMatImageTransform()
151 std::string filename(mInputFilenames[0]);
152 std::string ext(itksys::SystemTools::GetFilenameLastExtension(filename));
153 if (ext.length() > 0) {
154 size_t pos = filename.rfind(ext);
155 filename.replace(pos, ext.length(), ".mat");
160 std::ifstream f(filename.c_str());
164 itk::Matrix<double, 4, 4> itkMat = clitk::ReadMatrix3D(filename);
166 vtkSmartPointer<vtkMatrix4x4> matrix = vtkSmartPointer<vtkMatrix4x4>::New();
168 for(int j=0; j<4; j++)
169 for(int i=0; i<4; i++)
170 matrix->SetElement(j,i,itkMat[j][i]);
173 // For some reason, the transformation matrix given in the MHD
174 // file is inverted wrt the transformation given in the MAT file.
175 // We don't really know where the inversion takes place inside
176 // VTK or ITK. For what we could see in VV, the transformation
177 // given in the MHD file seems "more correct" than that given in
178 // the MAT file. For this reason, we invert the matrix read from
179 // the MAT file before concatenating it to the current transformation.
180 // Still, it would be nice to find out what happens exactly between
184 // We actually use vtkImageReslice which takes the inverse transform
185 // as input. So it seems more correct to inverse the matrix given by
186 // itk::ImageBase< VImageDimension >::GetDirection() than the matrix
187 // in .mat which is indeed the inverse optimized by a typical
188 // affine registration algorithm.
189 // Validated using clitkAffineTransform --transform_grid
190 if (matrix->Determinant() == 0)
192 vtkGenericWarningMacro("Matrix in " << filename.c_str() << " cannot be inverted (determinant = 0)");
195 mImage->GetTransform()->PostMultiply();
196 mImage->GetTransform()->Concatenate(matrix);
197 mImage->GetTransform()->Update();
200 //------------------------------------------------------------------------------