From: Romulo Pinho Date: Thu, 24 Jan 2013 13:26:49 +0000 (+0100) Subject: added tool to interpolate VFs X-Git-Tag: v1.4.0~254^2~1 X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=a386a950bd841d0f57ca636f9d1f789670924828;p=clitk.git added tool to interpolate VFs - support for float/double;Dim=3;Comp=3; vectors only --- diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 505c210..36fb8d9 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -54,6 +54,11 @@ IF (CLITK_BUILD_TOOLS) TARGET_LINK_LIBRARIES(clitkVFResample clitkCommon ${ITK_LIBRARIES}) SET(TOOLS_INSTALL ${TOOLS_INSTALL} clitkVFResample) + WRAP_GGO(clitkVFInterpolate_GGO_C clitkVFInterpolate.ggo) + ADD_EXECUTABLE(clitkVFInterpolate clitkVFInterpolate.cxx clitkVFInterpolateGenericFilter.cxx ${clitkVFInterpolate_GGO_C}) + TARGET_LINK_LIBRARIES(clitkVFInterpolate clitkCommon ${ITK_LIBRARIES}) + SET(TOOLS_INSTALL ${TOOLS_INSTALL} clitkVFInterpolate) + WRAP_GGO(clitkImageCreate_GGO_C clitkImageCreate.ggo) ADD_EXECUTABLE(clitkImageCreate clitkImageCreate.cxx ${clitkImageCreate_GGO_C}) TARGET_LINK_LIBRARIES(clitkImageCreate clitkCommon ${ITK_LIBRARIES}) diff --git a/tools/clitkVFInterpolate.cxx b/tools/clitkVFInterpolate.cxx new file mode 100644 index 0000000..eaff9b1 --- /dev/null +++ b/tools/clitkVFInterpolate.cxx @@ -0,0 +1,78 @@ +/*========================================================================= + 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 +===========================================================================**/ +#ifndef CLITKVFRESAMPLE_CXX +#define CLITKVFRESAMPLE_CXX + +// clitk +#include "clitkVFInterpolate_ggo.h" +#include "clitkIO.h" +#include "clitkVFInterpolateGenericFilter.h" + +//-------------------------------------------------------------------- +int main(int argc, char * argv[]) +{ + + // Init command line + GGO(clitkVFInterpolate, args_info); + CLITK_INIT; + + // Read input image header to check image dimension + itk::ImageIOBase::Pointer header = clitk::readImageHeader(args_info.input1_arg); + unsigned int dim = header->GetNumberOfDimensions(); + std::string pixelTypeName = header->GetComponentTypeAsString(header->GetComponentType()); + + // Print image info if verbose + if (args_info.verbose_flag) { + std::cout << "Input image <" << args_info.input1_arg << "> is "; + clitk::printImageHeader(header, std::cout); + std::cout << std::endl; + } + + // Get input size/spacing + std::vector inputSize; + std::vector inputSpacing; + inputSize.resize(dim); + inputSpacing.resize(dim); + for (unsigned int i=0; iGetSpacing(i); + inputSize[i] = header->GetDimensions(i); + } + + if (args_info.verbose_flag) { + std::cout << "Output image will be : " << std::endl; + } + + // Create a filter + clitk::VFInterpolateGenericFilter::Pointer filter = clitk::VFInterpolateGenericFilter::New(); + filter->SetInputFilename(args_info.input1_arg); + filter->SetInputFilename2(args_info.input2_arg); + filter->SetInterpolationName(args_info.interp_arg); + filter->SetDistance(args_info.distance_arg); +// filter->SetBSplineOrder(args_info.order_arg); +// filter->SetBLUTSampling(args_info.sampling_arg); + filter->SetOutputFilename(args_info.output_arg); + + // Go ! + filter->Update(); + + // this is the end my friend + return 0; +}// end main +//-------------------------------------------------------------------- + +#endif /* end #define CLITKVFRESAMPLE_CXX */ diff --git a/tools/clitkVFInterpolate.ggo b/tools/clitkVFInterpolate.ggo new file mode 100644 index 0000000..f6e0c1b --- /dev/null +++ b/tools/clitkVFInterpolate.ggo @@ -0,0 +1,15 @@ +#File clitkImageInterpolate.ggo +package "clitkImageInterpolate" +version "1.0" +purpose "Interpolate an image. You can specify the interpolation, you can apply a Gaussian filter before." + +option "config" - "Config file" string no +option "input1" i "Input image filename" string yes +option "input2" j "Input image filename" string yes +option "output" o "Output image filename" string yes +option "distance" d "Distance (d in [0,1])" float yes +option "verbose" v "Verbose" flag off +option "interp" - "Interpolation type: {nn, linear}" string no default="nn" +#option "order" b "BSpline ordre (range 0-5)" int no default="3" +#option "sampling" s "BLUT sampling value" int no default="30" + diff --git a/tools/clitkVFInterpolateGenericFilter.cxx b/tools/clitkVFInterpolateGenericFilter.cxx new file mode 100644 index 0000000..777bb13 --- /dev/null +++ b/tools/clitkVFInterpolateGenericFilter.cxx @@ -0,0 +1,181 @@ +/*========================================================================= + 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 +===========================================================================**/ +#ifndef CLITKVFRESAMPLEGENERICFILTER_CXX +#define CLITKVFRESAMPLEGENERICFILTER_CXX + +#include "clitkVFInterpolateGenericFilter.h" +#include "itkInterpolateImageFilter.h" +#include "itkLinearInterpolateImageFunction.h" +#include "itkNearestNeighborInterpolateImageFunction.h" +#include "itkNthElementImageAdaptor.h" + +//-------------------------------------------------------------------- +clitk::VFInterpolateGenericFilter::VFInterpolateGenericFilter(): + clitk::ImageToImageGenericFilter("VFInterpolate") +{ + //InitializeImageType<2>(); + InitializeImageType<3>(); + // InitializeImageType<4>(); + mInterpolatorName = "nn"; + mBSplineOrder=3; + mDistance=0; +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +template +void clitk::VFInterpolateGenericFilter::InitializeImageType() +{ + //typedef itk::Vector v3f; + //ADD_IMAGE_TYPE(Dim, v3f); +// ADD_VEC_IMAGE_TYPE(Dim,2,double) + ADD_VEC_IMAGE_TYPE(Dim,3,float) + ADD_VEC_IMAGE_TYPE(Dim,3,double) + +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +template +void clitk::VFInterpolateGenericFilter::UpdateWithInputImageType() +{ + + if (m_NbOfComponents == 1) { + std::cerr << "Error, only one components ? Use clitkImageInterpolate instead." << std::endl; + exit(0); + } + typedef typename ImageType::PixelType PixelType; +// if (m_NbOfComponents == 2) Update_WithDimAndPixelTypeAndComponent(); + if (m_NbOfComponents == 3) Update_WithDimAndPixelTypeAndComponent(); +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +template +void clitk::VFInterpolateGenericFilter::Update_WithDimAndPixelTypeAndComponent() +{ + // Reading input + // typedef itk::Vector DisplacementType; + typedef PixelType DisplacementType; + typedef itk::Image< DisplacementType, Dim > ImageType; + + typename ImageType::Pointer input1 = clitk::readImage(m_InputFilenames[0], m_IOVerbose); + typename ImageType::Pointer input2 = clitk::readImage(mInputFilename2, m_IOVerbose); + + // Main filter + typename ImageType::Pointer outputImage = ComputeImage(input1, input2); + + // Write results + SetNextOutput(outputImage); +} +//-------------------------------------------------------------------- + +//-------------------------------------------------------------------- +template +typename ImageType::Pointer +clitk::VFInterpolateGenericFilter::ComputeImage(typename ImageType::Pointer inputImage1, typename ImageType::Pointer inputImage2) +{ + + // Some typedefs + typedef itk::Image ScalarImageType; + typedef itk::Image InterpolationImageType; + typedef itk::NthElementImageAdaptor ImageAdaptorType; + typename ImageAdaptorType::Pointer adaptor1 = ImageAdaptorType::New(); + typename ImageAdaptorType::Pointer adaptor2 = ImageAdaptorType::New(); + adaptor1->SetImage(inputImage1); + adaptor2->SetImage(inputImage2); + + // Create Image Filter + typedef itk::InterpolateImageFilter FilterType; + typename FilterType::Pointer filter = FilterType::New(); + filter->SetInput1(adaptor1); + filter->SetInput2(adaptor2); + filter->SetDistance(mDistance); + + // Select interpolator + if (mInterpolatorName == "nn") { + typedef itk::NearestNeighborInterpolateImageFunction InterpolatorType; + typename InterpolatorType::Pointer interpolator = InterpolatorType::New(); + filter->SetInterpolator(interpolator); + } else { + if (mInterpolatorName == "linear") { + typedef itk::LinearInterpolateImageFunction InterpolatorType; + typename InterpolatorType::Pointer interpolator = InterpolatorType::New(); + filter->SetInterpolator(interpolator); + } else { + std::cerr << "Sorry, I do not know the interpolator (for vector field) '" << mInterpolatorName + << "'. Known interpolators are : nn, linear" << std::endl; + exit(0); + } + } + + typename ImageType::Pointer output = ImageType::New(); + typename ImageAdaptorType::Pointer adaptorOutput = ImageAdaptorType::New(); + output->CopyInformation(inputImage1); + output->SetRegions(inputImage1->GetLargestPossibleRegion()); + output->Allocate(); + + typedef itk::ImageRegionIterator IteratorType1; + typedef itk::ImageRegionIterator IteratorType2; + + for (unsigned int i = 0; i < ImageType::PixelType::Dimension; i++) { + adaptor1->SelectNthElement(i); + adaptor2->SelectNthElement(i); + + // Go ! + try { + filter->Update(); + } catch( itk::ExceptionObject & err ) { + std::cerr << "Error while filtering " << m_InputFilenames[0].c_str() + << " " << err << std::endl; + exit(0); + } + + adaptorOutput->SelectNthElement(i); + adaptorOutput->SetImage(output); + + IteratorType1 it1(filter->GetOutput(), filter->GetOutput()->GetLargestPossibleRegion()); + IteratorType2 it2(adaptorOutput, adaptorOutput->GetLargestPossibleRegion()); + + it1.GoToBegin(); + it2.GoToBegin(); + while ( ! it1.IsAtEnd() ) { + it2.Set(it1.Get()); + ++it1; + ++it2; + } + } + + // Return result + return output; + +} +//-------------------------------------------------------------------- + +//-------------------------------------------------------------------- +void clitk::VFInterpolateGenericFilter::SetInterpolationName(const std::string & inter) +{ + mInterpolatorName = inter; +} +//-------------------------------------------------------------------- + +#endif + diff --git a/tools/clitkVFInterpolateGenericFilter.h b/tools/clitkVFInterpolateGenericFilter.h new file mode 100644 index 0000000..2d84dcf --- /dev/null +++ b/tools/clitkVFInterpolateGenericFilter.h @@ -0,0 +1,102 @@ +/*========================================================================= + 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 +===========================================================================**/ +#ifndef CLITKIMAGERESAMPLEGENERICFILTER_H +#define CLITKIMAGERESAMPLEGENERICFILTER_H +/** + ------------------------------------------------------------------- + * @file clitkVFInterpolateGenericFilter.h + * @author David Sarrut + * @date 23 Feb 2008 08:37:53 + + * @brief + -------------------------------------------------------------------*/ + +// clitk include +#include "clitkCommon.h" +#include "clitkImageCommon.h" +#include "clitkImageToImageGenericFilter.h" + +// itk include +#include "itkImage.h" +#include "itkVectorImage.h" +#include "itkFixedArray.h" +#include "itkImageFileReader.h" +#include "itkImageSeriesReader.h" +#include "itkImageFileWriter.h" +#include "itkRecursiveGaussianImageFilter.h" +#include "itkInterpolateImageFilter.h" +#include "itkAffineTransform.h" +#include "itkVectorNearestNeighborInterpolateImageFunction.h" +#include "itkVectorLinearInterpolateImageFunction.h" +#include "itkBSplineInterpolateImageFunction.h" +#include "itkBSplineInterpolateImageFunctionWithLUT.h" +#include "itkCommand.h" + +namespace clitk { + + //-------------------------------------------------------------------- + class VFInterpolateGenericFilter: + public clitk::ImageToImageGenericFilter { + + public: + // constructor + VFInterpolateGenericFilter(); + + // Types + typedef VFInterpolateGenericFilter Self; + typedef itk::SmartPointer Pointer; + typedef itk::SmartPointer ConstPointer; + + // New + itkNewMacro(Self); + + void SetInputFilename1(const std::string& file) { mInputFilename1 = file; } + void SetInputFilename2(const std::string& file) { mInputFilename2 = file; } + void SetInterpolationName(const std::string & inter); + void SetDistance(double distance) { mDistance = distance; } + void SetBSplineOrder(int o) { mBSplineOrder = o; } + void SetBLUTSampling(int b) { mSamplingFactors.resize(1); mSamplingFactors[0] = b; } + + //-------------------------------------------------------------------- + // Main function called each time the filter is updated + template + void UpdateWithInputImageType(); + + protected: + template void InitializeImageType(); + //-------------------------------------------------------------------- + std::string mInputFilename1, mInputFilename2; + std::string mInterpolatorName; + int mBSplineOrder; + std::vector mSamplingFactors; + double mDistance; + + //-------------------------------------------------------------------- + template + void Update_WithDimAndPixelTypeAndComponent(); + template + typename ImageType::Pointer ComputeImage(typename ImageType::Pointer inputImage1, typename ImageType::Pointer inputImage2); + + }; // end class VFInterpolateGenericFilter + //-------------------------------------------------------------------- + +} // end namespace +//-------------------------------------------------------------------- + +#endif /* end #define CLITKIMAGERESAMPLEGENERICFILTER_H */ + diff --git a/tools/clitkVFInterpolateGenericFilter.txx b/tools/clitkVFInterpolateGenericFilter.txx new file mode 100644 index 0000000..7b4aea1 --- /dev/null +++ b/tools/clitkVFInterpolateGenericFilter.txx @@ -0,0 +1,34 @@ +/*========================================================================= + 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 +===========================================================================**/ +#ifndef CLITKVFRESAMPLEGENERICFILTER_TXX +#define CLITKVFRESAMPLEGENERICFILTER_TXX +/** + ------------------------------------------------= + * @file clitkVFInterpolateGenericFilter.txx + * @author David Sarrut + * @date 23 Feb 2008 08:40:11 + * + * @brief + * + * + ------------------------------------------------=*/ + +//-------------------------------------------------------------------- + +#endif /* end #define CLITKVFRESAMPLEGENERICFILTER_TXX */ +