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 "gdcmImageHelper.h"
23 #include "vvImageReader.h"
24 #include "vvImageReader.txx"
25 #include "clitkTransformUtilities.h"
26 #include "clitkElastix.h"
28 //------------------------------------------------------------------------------
29 vvImageReader::vvImageReader()
32 mInputFilenames.resize(0);
34 mType = UNDEFINEDIMAGETYPE;
37 //------------------------------------------------------------------------------
40 //------------------------------------------------------------------------------
41 vvImageReader::~vvImageReader() { }
42 //------------------------------------------------------------------------------
45 //------------------------------------------------------------------------------
46 void vvImageReader::Update()
50 //------------------------------------------------------------------------------
53 //------------------------------------------------------------------------------
54 void vvImageReader::Update(LoadedImageType type)
56 itk::ImageIOBase::Pointer reader = itk::ImageIOFactory::CreateImageIO(mInputFilenames[0].c_str(), itk::ImageIOFactory::ReadMode);
58 mLastError="Unable to read file.";
60 reader->SetFileName(mInputFilenames[0]);
61 gdcm::ImageHelper::SetForcePixelSpacing(true);
62 reader->ReadImageInformation();
63 if (mInputFilenames.size() > 1)
64 Update(reader->GetNumberOfDimensions()+1,reader->GetComponentTypeAsString(reader->GetComponentType()),type);
65 else if (reader->GetNumberOfComponents() > 1 && type != VECTORFIELD && type != VECTORFIELDWITHTIME)
66 Update(reader->GetNumberOfDimensions()+1,reader->GetComponentTypeAsString(reader->GetComponentType()),VECTORPIXELIMAGE);
68 Update(reader->GetNumberOfDimensions(),reader->GetComponentTypeAsString(reader->GetComponentType()),type);
71 //------------------------------------------------------------------------------
74 //------------------------------------------------------------------------------
75 void vvImageReader::Update(int dim,std::string inputPixelType, LoadedImageType type)
77 //CALL_FOR_ALL_DIMS(dim,UpdateWithDim,inputPixelType);
80 mInputPixelType=inputPixelType;
84 UpdateWithDim<2>(mInputPixelType);
87 UpdateWithDim<3>(mInputPixelType);
90 UpdateWithDim<4>(mInputPixelType);
93 std::cerr << "dimension unknown in Update ! " << std::endl;
96 //------------------------------------------------------------------------------
99 //------------------------------------------------------------------------------
100 void vvImageReader::SetInputFilename(const std::string & filename)
102 mInputFilenames.resize(0);
103 mInputFilenames.push_back(filename);
105 //------------------------------------------------------------------------------
108 //------------------------------------------------------------------------------
109 void vvImageReader::SetInputFilenames(const std::vector<std::string> & filenames)
111 mInputFilenames = filenames;
113 //------------------------------------------------------------------------------
116 //------------------------------------------------------------------------------
117 //Read transformation in NKI format (Xdr, transposed, cm)
118 //void vvImageReader::ReadNkiImageTransform()
121 // std::string filename = mInputFilenames[0]+".MACHINEORIENTATION";
122 // if(itksys::SystemTools::FileExists(filename.c_str())){
123 // typedef itk::ImageFileReader< itk::Image< double, 2 > > MatrixReaderType;
124 // MatrixReaderType::Pointer readerTransfo = MatrixReaderType::New();
125 // readerTransfo->SetFileName(filename);
128 // readerTransfo->Update();
129 // } catch( itk::ExceptionObject & err ) {
131 // std::cerr << "Cannot read " << filename << std::endl
132 // << "The error is: " << err << std::endl;
136 // //Transpose matrix (NKI format)
137 // for(int j=0; j<4; j++)
138 // for(int i=0; i<4; i++)
139 // mImage->GetTransform()->GetMatrix()->SetElement(j,i,readerTransfo->GetOutput()->GetBufferPointer()[4*i+j]);
142 // for(int i=0; i<3; i++)
143 // mImage->GetTransform()->GetMatrix()->SetElement(i,3,10*mImage->GetTransform()->GetMatrix()->GetElement(i,3));
145 // mImage->GetTransform()->Inverse();
149 //------------------------------------------------------------------------------
152 //------------------------------------------------------------------------------
153 //Read transformation in ASCII format
154 void vvImageReader::ReadMatImageTransform()
156 std::string filename(mInputFilenames[0]);
157 std::string ext(itksys::SystemTools::GetFilenameLastExtension(filename));
159 // Try a ".mat" extension
160 if (ext.length() > 0) {
161 size_t pos = filename.rfind(ext);
162 filename.replace(pos, ext.length(), ".mat");
166 std::ifstream f(filename.c_str());
167 itk::Matrix<double, 4, 4> itkMat;
168 bool itkMatRead = false;
172 itkMat.SetIdentity();
174 itkMat = clitk::ReadMatrix3D(filename);
176 catch (itk::ExceptionObject & err) {
177 itkWarningMacro(<< "Found " << filename
178 << " but this is not a 4x4 matrix so it is ignored.");
184 // Try a ".elx" extension
185 filename = mInputFilenames[0];
186 if (ext.length() > 0) {
187 size_t pos = filename.rfind(ext);
188 filename.replace(pos, ext.length(), ".elx");
192 f.open(filename.c_str());
193 if(!itkMatRead && f.is_open()) {
195 itkMat = clitk::createMatrixFromElastixFile<3>(filename, true);
200 vtkSmartPointer<vtkMatrix4x4> matrix = vtkSmartPointer<vtkMatrix4x4>::New();
202 for(int j=0; j<4; j++)
203 for(int i=0; i<4; i++)
204 matrix->SetElement(j,i,itkMat[j][i]);
207 // For some reason, the transformation matrix given in the MHD
208 // file is inverted wrt the transformation given in the MAT file.
209 // We don't really know where the inversion takes place inside
210 // VTK or ITK. For what we could see in VV, the transformation
211 // given in the MHD file seems "more correct" than that given in
212 // the MAT file. For this reason, we invert the matrix read from
213 // the MAT file before concatenating it to the current transformation.
214 // Still, it would be nice to find out what happens exactly between
218 // We actually use vtkImageReslice which takes the inverse transform
219 // as input. So it seems more correct to inverse the matrix given by
220 // itk::ImageBase< VImageDimension >::GetDirection() than the matrix
221 // in .mat which is indeed the inverse optimized by a typical
222 // affine registration algorithm.
223 // Validated using clitkAffineTransform --transform_grid
224 if (matrix->Determinant() == 0)
226 vtkGenericWarningMacro("Matrix in " << filename.c_str() << " cannot be inverted (determinant = 0)");
229 // TODO SR and BP: check on the list of transforms and not the first only
230 mImage->GetTransform()[0]->PreMultiply();
231 mImage->GetTransform()[0]->Concatenate(matrix);
232 mImage->GetTransform()[0]->Update();
234 //for image sequences, apply the transform to each images of the sequence
235 if (mImage->IsTimeSequence())
237 for (unsigned i = 1 ; i<mImage->GetTransform().size() ; i++)
239 mImage->GetTransform()[i]->PreMultiply();
240 mImage->GetTransform()[i]->Concatenate(matrix);
241 mImage->GetTransform()[i]->Update();
247 //------------------------------------------------------------------------------