From 0db19ec416f7f4f68a398636fd451688792c6e8d Mon Sep 17 00:00:00 2001 From: Simon Rit Date: Wed, 24 Jul 2013 17:04:09 +0200 Subject: [PATCH] Go from matrix to elastix file --- tools/CMakeLists.txt | 4 + tools/clitkMatrixToElastixTransform.cxx | 118 ++++++++++++++++++++++++ tools/clitkMatrixToElastixTransform.ggo | 10 ++ 3 files changed, 132 insertions(+) create mode 100644 tools/clitkMatrixToElastixTransform.cxx create mode 100644 tools/clitkMatrixToElastixTransform.ggo diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 77f12f9..899bd36 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -113,6 +113,10 @@ IF (CLITK_BUILD_TOOLS) TARGET_LINK_LIBRARIES(clitkElastixTransformToMatrix clitkCommon ) SET(TOOLS_INSTALL ${TOOLS_INSTALL} clitkElastixTransformToMatrix) + WRAP_GGO(clitkMatrixToElastixTransform_GGO_C clitkMatrixToElastixTransform.ggo) + ADD_EXECUTABLE(clitkMatrixToElastixTransform clitkMatrixToElastixTransform.cxx ${clitkMatrixToElastixTransform_GGO_C}) + TARGET_LINK_LIBRARIES(clitkMatrixToElastixTransform clitkCommon ) + SET(TOOLS_INSTALL ${TOOLS_INSTALL} clitkMatrixToElastixTransform) WRAP_GGO(clitkSetBackground_GGO_C clitkSetBackground.ggo) ADD_EXECUTABLE(clitkSetBackground clitkSetBackground.cxx clitkSetBackgroundGenericFilter.cxx ${clitkSetBackground_GGO_C}) diff --git a/tools/clitkMatrixToElastixTransform.cxx b/tools/clitkMatrixToElastixTransform.cxx new file mode 100644 index 0000000..a8b7520 --- /dev/null +++ b/tools/clitkMatrixToElastixTransform.cxx @@ -0,0 +1,118 @@ +/*========================================================================= + Program: vv http://www.creatis.insa-lyon.fr/rio/vv + + Authors belong to: + - University of LYON http://www.universite-lyon.fr/ + - Léon Bérard cancer center http://www.centreleonberard.fr + - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the copyright notices for more information. + + It is distributed under dual licence + + - BSD See included LICENSE.txt file + - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html +===========================================================================**/ + +// clitk +#include "clitkMatrixToElastixTransform_ggo.h" +#include "clitkTransformUtilities.h" +#include "clitkIO.h" + +// itk +#include + +//-------------------------------------------------------------------- +int main(int argc, char * argv[]) +{ + // Init command line + GGO(clitkMatrixToElastixTransform, args_info); + CLITK_INIT; + + // Read matrix + itk::Matrix matrix; + try { + matrix = clitk::ReadMatrix3D(args_info.input_arg); + } + catch (itk::ExceptionObject & err) { + std::cerr << "Error reading " << args_info.input_arg << std::endl; + std::cerr << err.GetDescription() << std::endl; + exit(-1); + } + + // Compute parameters from transfer using itk Euler transform + itk::Euler3DTransform::CenterType center; + center.Fill(0.); + if(args_info.center_given==3) { + center[0] = args_info.center_arg[0]; + center[1] = args_info.center_arg[1]; + center[2] = args_info.center_arg[2]; + } + itk::Euler3DTransform::MatrixType rotMat; + itk::Euler3DTransform::OutputVectorType transVec; + for(int i=0; i<3; i++) { + transVec[i] = matrix[i][3]; + for(int j=0; j<3; j++) + rotMat[i][j] = matrix[i][j]; + } + itk::Euler3DTransform::Pointer euler; + euler = itk::Euler3DTransform::New(); + euler->SetCenter(center); + euler->SetOffset(transVec); + euler->SetComputeZYX(false); + try { + euler->SetMatrix(rotMat); + } + catch (itk::ExceptionObject & err) { + std::cerr << "Error reading " << args_info.input_arg << std::endl; + std::cerr << err.GetDescription() << std::endl; + exit(-1); + } + + // Write result + std::ofstream out; + clitk::openFileForWriting(out, args_info.output_arg); + out << "(Transform \"EulerTransform\")" << std::endl; + out << "(NumberOfParameters 6)" << std::endl; + out << "(TransformParameters "; + for(unsigned int i=0; i<6; i++) + out << euler->GetParameters()[i] << ' '; + out << ')' << std::endl; + out << "(InitialTransformParametersFileName \"NoInitialTransform\")" << std::endl; + out << "(HowToCombineTransforms \"Compose\")" << std::endl; + + out << "// EulerTransform specific" << std::endl; + out << "(CenterOfRotationPoint "<< center[0] << ' ' << center[1] << ' ' << center[2] << ')' << std::endl; + out << "(ComputeZYX \"false\")" << std::endl; + + // The rest is commented, up to the user to define it manually + out << "// Image specific" << std::endl; + out << "// (FixedImageDimension 3)" << std::endl; + out << "// (MovingImageDimension 3)" << std::endl; + out << "// (FixedInternalImagePixelType \"float\")" << std::endl; + out << "// (MovingInternalImagePixelType \"float\")" << std::endl; + out << "// (Size 1 1 1)" << std::endl; + out << "// (Index 0 0 0)" << std::endl; + out << "// (Spacing 1 1 1)" << std::endl; + out << "// (Origin -0.5 -0.5 -0.5)" << std::endl; + out << "// (Direction 1 0 0 0 1 0 0 0 1)" << std::endl; + out << "// (UseDirectionCosines \"true\")" << std::endl << std::endl; + + out << "// ResampleInterpolator specific" << std::endl; + out << "// (ResampleInterpolator \"FinalBSplineInterpolator\")" << std::endl; + out << "// (FinalBSplineInterpolationOrder 3)" << std::endl << std::endl; + + out << "// Resampler specific" << std::endl; + out << "// (Resampler \"DefaultResampler\")" << std::endl; + out << "// (DefaultPixelValue 0.000000)" << std::endl; + out << "// (ResultImageFormat \"mhd\")" << std::endl; + out << "// (ResultImagePixelType \"short\")" << std::endl; + out << "// (CompressResultImage \"false\")" << std::endl; + out.close(); + + return EXIT_SUCCESS; +}// end main + +//-------------------------------------------------------------------- diff --git a/tools/clitkMatrixToElastixTransform.ggo b/tools/clitkMatrixToElastixTransform.ggo new file mode 100644 index 0000000..38f73fa --- /dev/null +++ b/tools/clitkMatrixToElastixTransform.ggo @@ -0,0 +1,10 @@ +#File clitkMatrixToElastixTransform.ggo +package "clitkMatrixToElastixTransform" +version "1.0" +purpose "" + +option "config" - "Config file" string optional +option "verbose" v "Verbose" flag off +option "input" i "Input matrix filename" string required +option "output" o "Output elastix filename" string required +option "center" - "Rotation center" float no multiple(3) -- 2.45.2