X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=tools%2FclitkAffineTransformGenericFilter.txx;h=93d43571b11d170825ad6f31e09b5646e653f086;hb=8786715f17744e1a8d97bdf4fec517d443875bb0;hp=b2d7a534aa7e250b2d51a4300ebc144fe88669dd;hpb=fb28336c3cac08742cd8175bf9f74534d114c295;p=clitk.git diff --git a/tools/clitkAffineTransformGenericFilter.txx b/tools/clitkAffineTransformGenericFilter.txx index b2d7a53..93d4357 100644 --- a/tools/clitkAffineTransformGenericFilter.txx +++ b/tools/clitkAffineTransformGenericFilter.txx @@ -22,6 +22,7 @@ #include #include #include +#include "clitkElastix.h" namespace clitk { @@ -52,12 +53,12 @@ namespace clitk // Call UpdateWithDim if(Dimension==2) UpdateWithDim<2>(PixelType, Components); else - if(Dimension==3) UpdateWithDim<3>(PixelType, Components); - else if (Dimension==4)UpdateWithDim<4>(PixelType, Components); - else { - std::cout<<"Error, Only for 2, 3 or 4 Dimensions!!!"<(PixelType, Components); + else if (Dimension==4)UpdateWithDim<4>(PixelType, Components); + else { + std::cout<<"Error, Only for 2, 3 or 4 Dimensions!!!"<(); } - // else if(PixelType == "unsigned_short"){ - // if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl; - // UpdateWithDimAndPixelType(); - // } + else if(PixelType == "unsigned_short"){ + if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl; + UpdateWithDimAndPixelType(); + } else if (PixelType == "unsigned_char") { if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and unsigned_char..." << std::endl; @@ -138,7 +139,7 @@ namespace clitk { if (m_ArgsInfo.matrix_given) { - std::cerr << "You must use either rotate/translate or matrix options" << std::cout; + std::cerr << "You must use either rotate/translate or matrix options" << std::endl; return; } itk::Array transformParameters(2 * Dimension); @@ -181,7 +182,8 @@ namespace clitk } else { if (m_ArgsInfo.elastix_given) { - matrix = createMatrixFromElastixFile(m_ArgsInfo.elastix_arg); + std::string filename(m_ArgsInfo.elastix_arg); + matrix = createMatrixFromElastixFile(filename, m_Verbose); } else matrix.SetIdentity(); @@ -210,6 +212,7 @@ namespace clitk likeReader->SetFileName(m_ArgsInfo.like_arg); likeReader->Update(); resampler->SetOutputParametersFromImage(likeReader->GetOutput()); + resampler->SetOutputDirection(likeReader->GetOutput()->GetDirection()); } else if(m_ArgsInfo.transform_grid_flag) { typename itk::Matrix invMatrix( matrix.GetInverse() ); typename itk::Matrix invRotMatrix( clitk::GetRotationalPartMatrix(invMatrix) ); @@ -366,7 +369,7 @@ namespace clitk { if (m_ArgsInfo.matrix_given) { - std::cerr << "You must use either rotate/translate or matrix options" << std::cout; + std::cerr << "You must use either rotate/translate or matrix options" << std::endl; return; } itk::Array transformParameters(2 * Dimension); @@ -489,121 +492,6 @@ namespace clitk } //------------------------------------------------------------------- - - - //------------------------------------------------------------------- - template - template - typename itk::Matrix - AffineTransformGenericFilter::createMatrixFromElastixFile(std::string filename) - { - if (Dimension != 3) { - FATAL("Only 3D yet" << std::endl); - } - typename itk::Matrix matrix; - - // Open file - std::ifstream is; - clitk::openFileForReading(is, filename); - - // Check Transform - std::string s; - bool b = GetElastixValueFromTag(is, "Transform ", s); - if (!b) { - FATAL("Error must read 'Transform' in " << filename << std::endl); - } - if (s != "EulerTransform") { - FATAL("Sorry only 'EulerTransform'" << std::endl); - } - - // FIXME check - // (InitialTransformParametersFileName "NoInitialTransform") - - // Get CenterOfRotationPoint - GetElastixValueFromTag(is, "CenterOfRotationPoint ", s); // space is needed - if (!b) { - FATAL("Error must read 'CenterOfRotationPoint' in " << filename << std::endl); - } - std::vector cor; - GetValuesFromValue(s, cor); - - // Get Transformparameters - GetElastixValueFromTag(is, "TransformParameters ", s); // space is needed - if (!b) { - FATAL("Error must read 'TransformParameters' in " << filename << std::endl); - } - std::vector results; - GetValuesFromValue(s, results); - - // construct a stream from the string - itk::CenteredEuler3DTransform::Pointer mat = itk::CenteredEuler3DTransform::New(); - itk::CenteredEuler3DTransform::ParametersType p; - p.SetSize(9); - for(uint i=0; i<3; i++) - p[i] = atof(results[i].c_str()); // Rotation - for(uint i=0; i<3; i++) - p[i+3] = atof(cor[i].c_str()); // Centre of rotation - for(uint i=0; i<3; i++) - p[i+6] = atof(results[i+3].c_str()); // Translation - mat->SetParameters(p); - - if (m_Verbose) { - std::cout << "Rotation (deg) : " << rad2deg(p[0]) << " " << rad2deg(p[1]) << " " << rad2deg(p[2]) << std::endl; - std::cout << "Translation (phy) : " << p[3] << " " << p[4] << " " << p[5] << std::endl; - std::cout << "Center of rot (phy) : " << p[6] << " " << p[7] << " " << p[8] << std::endl; - } - - for(uint i=0; i<3; i++) - for(uint j=0; j<3; j++) - matrix[i][j] = mat->GetMatrix()[i][j]; - // Offset is -Rc + t + c - matrix[0][3] = mat->GetOffset()[0]; - matrix[1][3] = mat->GetOffset()[1]; - matrix[2][3] = mat->GetOffset()[2]; - matrix[3][3] = 1; - - return matrix; - } - - //------------------------------------------------------------------- - template - bool - AffineTransformGenericFilter::GetElastixValueFromTag(std::ifstream & is, - std::string tag, - std::string & value) - { - std::string line; - is.seekg (0, is.beg); - while(std::getline(is, line)) { - unsigned pos = line.find(tag); - if (pos - void - AffineTransformGenericFilter::GetValuesFromValue(const std::string & s, - std::vector & values) - { - std::stringstream strstr(s); - std::istream_iterator it(strstr); - std::istream_iterator end; - std::vector results(it, end); - values.clear(); - values.resize(results.size()); - for(uint i=0; i