X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=vv%2FvvImageReader.cxx;h=25e235a08edb3b713b347b5a5a1147aaed7fb061;hb=ff1d5b8af965e75ecdbc1b0cbff0dad6f3b3c511;hp=7cc0e569856b160276385b1f7c81d7e9ecba03ec;hpb=b6cfdecc2f880a7015d8f5a73174a0361cfe7924;p=clitk.git diff --git a/vv/vvImageReader.cxx b/vv/vvImageReader.cxx index 7cc0e56..25e235a 100644 --- a/vv/vvImageReader.cxx +++ b/vv/vvImageReader.cxx @@ -1,7 +1,7 @@ /*========================================================================= Program: vv http://www.creatis.insa-lyon.fr/rio/vv - Authors belong to: + Authors belong to: - University of LYON http://www.universite-lyon.fr/ - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr @@ -22,6 +22,7 @@ #include #include "vvImageReader.h" #include "vvImageReader.txx" +#include "clitkTransformUtilities.h" //------------------------------------------------------------------------------ vvImageReader::vvImageReader() @@ -30,6 +31,7 @@ vvImageReader::vvImageReader() mInputFilenames.resize(0); mLastError = ""; mType = UNDEFINEDIMAGETYPE; + mSlice = 0; } //------------------------------------------------------------------------------ @@ -53,8 +55,7 @@ void vvImageReader::Update(LoadedImageType type) itk::ImageIOBase::Pointer reader = itk::ImageIOFactory::CreateImageIO(mInputFilenames[0].c_str(), itk::ImageIOFactory::ReadMode); if (!reader) { mLastError="Unable to read file."; - } - else { + } else { reader->SetFileName(mInputFilenames[0]); reader->ReadImageInformation(); if (mInputFilenames.size() > 1) @@ -67,17 +68,17 @@ void vvImageReader::Update(LoadedImageType type) //------------------------------------------------------------------------------ -void vvImageReader::Update(int dim,std::string inputPixelType, LoadedImageType type) { +void vvImageReader::Update(int dim,std::string inputPixelType, LoadedImageType type) +{ //CALL_FOR_ALL_DIMS(dim,UpdateWithDim,inputPixelType); mType = type; mDim = dim; mInputPixelType=inputPixelType; this->start(); //Start heavy read operation in a separate thread - while (this->isRunning()) - { - qApp->processEvents(); - this->wait(50); - } + while (this->isRunning()) { + qApp->processEvents(); + this->wait(50); + } } //------------------------------------------------------------------------------ @@ -112,10 +113,90 @@ void vvImageReader::SetInputFilename(const std::string & filename) //------------------------------------------------------------------------------ -void vvImageReader::SetInputFilenames(const std::vector & filenames) { +void vvImageReader::SetInputFilenames(const std::vector & filenames) +{ mInputFilenames = filenames; } //------------------------------------------------------------------------------ + +//------------------------------------------------------------------------------ +//Read transformation in NKI format (Xdr, transposed, cm) +void vvImageReader::ReadNkiImageTransform() +{ + bool bRead=false; + std::string filename = mInputFilenames[0]+".MACHINEORIENTATION"; + if(itksys::SystemTools::FileExists(filename.c_str())){ + typedef itk::ImageFileReader< itk::Image< double, 2 > > MatrixReaderType; + MatrixReaderType::Pointer readerTransfo = MatrixReaderType::New(); + readerTransfo->SetFileName(filename); + try { + bRead = true; + readerTransfo->Update(); + } catch( itk::ExceptionObject & err ) { + bRead=false; + std::cerr << "Cannot read " << filename << std::endl + << "The error is: " << err << std::endl; + } + + if (bRead) { + //Transpose matrix (NKI format) + for(int j=0; j<4; j++) + for(int i=0; i<4; i++) + mImage->GetTransform()->GetMatrix()->SetElement(j,i,readerTransfo->GetOutput()->GetBufferPointer()[4*i+j]); + + //From cm to mm + for(int i=0; i<3; i++) + mImage->GetTransform()->GetMatrix()->SetElement(i,3,10*mImage->GetTransform()->GetMatrix()->GetElement(i,3)); + + mImage->GetTransform()->Inverse(); + } + } +} +//------------------------------------------------------------------------------ + + +//------------------------------------------------------------------------------ +//Read transformation in ASCII format +void vvImageReader::ReadMatImageTransform() +{ + std::string filename(mInputFilenames[0]+".mat"); + std::ifstream f(filename.c_str()); + if(f.is_open()) { + f.close(); + + itk::Matrix itkMat = clitk::ReadMatrix3D(filename); + + vtkSmartPointer matrix = vtkSmartPointer::New(); + matrix->Identity(); + for(int j=0; j<4; j++) + for(int i=0; i<4; i++) + matrix->SetElement(j,i,itkMat[j][i]); + + // RP: 14/03/2011 + // For some reason, the transformation matrix given in the MHD + // file is inverted wrt the transformation given in the MAT file. + // We don't really know where the inversion takes place inside + // VTK or ITK. For what we could see in VV, the transformation + // given in the MHD file seems "more correct" than that given in + // the MAT file. For this reason, we invert the matrix read from + // the MAT file before concatenating it to the current transformation. + // Still, it would be nice to find out what happens exactly between + // VTK and ITK... + // + vtkSmartPointer inv_matrix = vtkSmartPointer::New(); + if (matrix->Determinant() == 0) + { + vtkGenericWarningMacro("Matrix in " << filename.c_str() << " cannot be inverted (determinant = 0)"); + } + else + matrix->Invert(*matrix, *inv_matrix); + + mImage->GetTransform()->PostMultiply(); + mImage->GetTransform()->Concatenate(inv_matrix); + mImage->GetTransform()->Update(); + } +} +//------------------------------------------------------------------------------ #endif