From c993ae170e42e6cd193abb3240af1765263fed13 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Beno=C3=AEt=20Presles?= Date: Fri, 18 Oct 2013 15:12:34 +0200 Subject: [PATCH] new tool to compute the laplacian of an image --- tools/CMakeLists.txt | 5 + tools/clitkImageLaplacian.cxx | 50 ++++++ tools/clitkImageLaplacian.ggo | 14 ++ tools/clitkImageLaplacianGenericFilter.h | 69 ++++++++ tools/clitkImageLaplacianGenericFilter.txx | 183 +++++++++++++++++++++ 5 files changed, 321 insertions(+) create mode 100644 tools/clitkImageLaplacian.cxx create mode 100644 tools/clitkImageLaplacian.ggo create mode 100644 tools/clitkImageLaplacianGenericFilter.h create mode 100644 tools/clitkImageLaplacianGenericFilter.txx diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index c3984c3..a4594a2 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -305,6 +305,11 @@ IF (CLITK_BUILD_TOOLS) TARGET_LINK_LIBRARIES(clitkImageGradientMagnitude clitkCommon ) SET(TOOLS_INSTALL ${TOOLS_INSTALL} clitkImageGradientMagnitude) + WRAP_GGO(clitkImageLaplacian_GGO_C clitkImageLaplacian.ggo) + ADD_EXECUTABLE(clitkImageLaplacian clitkImageLaplacian.cxx ${clitkImageLaplacian_GGO_C}) + TARGET_LINK_LIBRARIES(clitkImageLaplacian clitkCommon ) + SET(TOOLS_INSTALL ${TOOLS_INSTALL} clitkImageLaplacian) + #========================================================= option(CLITK_USE_ROOT "Build experimental tools using root" OFF) diff --git a/tools/clitkImageLaplacian.cxx b/tools/clitkImageLaplacian.cxx new file mode 100644 index 0000000..9f9901e --- /dev/null +++ b/tools/clitkImageLaplacian.cxx @@ -0,0 +1,50 @@ +/*========================================================================= + 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 +===========================================================================**/ + +/* ================================================= + * @file clitkImageLaplacianGenericFilter.txx + * @author xxx + * @date 29 June 2029 + * + * @brief ImageLaplacian an image + * + ===================================================*/ + +// clitk +#include "clitkImageLaplacian_ggo.h" +#include "clitkImageLaplacianGenericFilter.h" + +//-------------------------------------------------------------------- +int main(int argc, char * argv[]) +{ + + // Init command line + GGO(clitkImageLaplacian, args_info); + CLITK_INIT; + + // Filter + typedef clitk::ImageLaplacianGenericFilter FilterType; + FilterType::Pointer filter = FilterType::New(); + + filter->SetArgsInfo(args_info); + filter->Update(); + + return EXIT_SUCCESS; +}// end main + +//-------------------------------------------------------------------- diff --git a/tools/clitkImageLaplacian.ggo b/tools/clitkImageLaplacian.ggo new file mode 100644 index 0000000..2066df9 --- /dev/null +++ b/tools/clitkImageLaplacian.ggo @@ -0,0 +1,14 @@ +#File clitkImageLaplacian.ggo +package "clitkImageLaplacian" +version "1.0" +purpose "" + +option "config" - "Config file" string optional +option "verbose" v "Verbose" flag off + +option "imagetypes" - "Display allowed image types" flag off + +option "input" i "Input image filename" string required +option "mask" m "Mask input image filename" string optional +option "output" o "Output image filename" string required +option "normalize" n "Normalize the output image between 0 and 1" flag off diff --git a/tools/clitkImageLaplacianGenericFilter.h b/tools/clitkImageLaplacianGenericFilter.h new file mode 100644 index 0000000..928f3a5 --- /dev/null +++ b/tools/clitkImageLaplacianGenericFilter.h @@ -0,0 +1,69 @@ +/*========================================================================= + 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 CLITKImageLaplacianGENERICFILTER_H +#define CLITKImageLaplacianGENERICFILTER_H +#include "clitkIO.h" +#include "clitkImageToImageGenericFilter.h" + +//-------------------------------------------------------------------- +namespace clitk +{ + +template +class ITK_EXPORT ImageLaplacianGenericFilter: + public ImageToImageGenericFilter > +{ + +public: + + //-------------------------------------------------------------------- + ImageLaplacianGenericFilter(); + + //-------------------------------------------------------------------- + typedef ImageLaplacianGenericFilter Self; + typedef itk::SmartPointer Pointer; + typedef itk::SmartPointer ConstPointer; + + //-------------------------------------------------------------------- + // Method for creation through the object factory + // and Run-time type information (and related methods) + itkNewMacro(Self); + itkTypeMacro(ImageLaplacianGenericFilter, LightObject); + + //-------------------------------------------------------------------- + void SetArgsInfo(const args_info_type & a); + + //-------------------------------------------------------------------- + // Main function called each time the filter is updated + template + void UpdateWithInputImageType(); + +protected: + template void InitializeImageType(); + args_info_type mArgsInfo; + +}; // end class +//-------------------------------------------------------------------- + +} // end namespace clitk + +#ifndef ITK_MANUAL_INSTANTIATION +#include "clitkImageLaplacianGenericFilter.txx" +#endif + +#endif // #define clitkImageLaplacianGenericFilter_h diff --git a/tools/clitkImageLaplacianGenericFilter.txx b/tools/clitkImageLaplacianGenericFilter.txx new file mode 100644 index 0000000..2ffa43b --- /dev/null +++ b/tools/clitkImageLaplacianGenericFilter.txx @@ -0,0 +1,183 @@ +/*========================================================================= + 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 clitkImageLaplacianGenericFilter_txx +#define clitkImageLaplacianGenericFilter_txx + +/* ================================================= + * @file clitkImageLaplacianGenericFilter.txx + * @author Jef Vandemeulebroucke + * @date 29 june 2009 + * + * @brief + * + ===================================================*/ + +// itk include +#include "itkCastImageFilter.h" +#include "itkLaplacianImageFilter.h" +#include "itkLabelStatisticsImageFilter.h" +#include "itkMaskImageFilter.h" +#include "itkMaskNegatedImageFilter.h" +#include + +namespace clitk +{ + +//-------------------------------------------------------------------- +template +ImageLaplacianGenericFilter::ImageLaplacianGenericFilter(): + ImageToImageGenericFilter("ImageLaplacian") +{ + InitializeImageType<2>(); + InitializeImageType<3>(); +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +template +template +void ImageLaplacianGenericFilter::InitializeImageType() +{ + ADD_DEFAULT_IMAGE_TYPES(Dim); +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +template +void ImageLaplacianGenericFilter::SetArgsInfo(const args_info_type & a) +{ + mArgsInfo=a; + this->SetIOVerbose(mArgsInfo.verbose_flag); + if (mArgsInfo.imagetypes_flag) this->PrintAvailableImageTypes(); + + if (mArgsInfo.input_given) { + this->SetInputFilename(mArgsInfo.input_arg); + } + if (mArgsInfo.output_given) { + this->SetOutputFilename(mArgsInfo.output_arg); + } + if (mArgsInfo.mask_given) { + this->AddInputFilename(mArgsInfo.mask_arg); + } +} +//-------------------------------------------------------------------- + +//-------------------------------------------------------------------- +// Update with the number of dimensions and the pixeltype +//-------------------------------------------------------------------- +template +template +void +ImageLaplacianGenericFilter::UpdateWithInputImageType() +{ + // Main filter + typedef typename InputImageType::PixelType InputPixelType; + typedef itk::Image FloatImageType; + typedef itk::Image MaskImageType; + + // Reading input + typename InputImageType::Pointer input = this->template GetInput(0); + + //Cast input to float + typedef itk::CastImageFilter< InputImageType, FloatImageType > CastFilterType; + typename CastFilterType::Pointer castFilter = CastFilterType::New(); + castFilter->SetInput(input); + castFilter->Update(); + + typename MaskImageType::Pointer mask = NULL; + if(mArgsInfo.mask_given) { + mask = this->template GetInput(1); + } + else { + mask = MaskImageType::New(); + mask->SetRegions(input->GetLargestPossibleRegion()); + mask->SetOrigin(input->GetOrigin()); + mask->SetSpacing(input->GetSpacing()); + mask->Allocate(); + mask->FillBuffer(1); + } + + + // Create output image + typename FloatImageType::Pointer outputImage = FloatImageType::New(); + outputImage->SetRegions(input->GetLargestPossibleRegion()); + outputImage->SetOrigin(input->GetOrigin()); + outputImage->SetSpacing(input->GetSpacing()); + outputImage->Allocate(); + outputImage->FillBuffer(0.0); + // Set output iterator + typedef itk::ImageRegionIterator IteratorOutputType; + IteratorOutputType ito = IteratorOutputType(outputImage, outputImage->GetLargestPossibleRegion()); + + // Filter + typedef itk::LaplacianImageFilter LaplacianImageFilterType; + typename LaplacianImageFilterType::Pointer laplacianFilter=LaplacianImageFilterType::New(); + laplacianFilter->SetInput(castFilter->GetOutput()); + laplacianFilter->Update(); + // Set iterator + typedef itk::ImageRegionIterator IteratorType; + IteratorType it(laplacianFilter->GetOutput(), laplacianFilter->GetOutput()->GetLargestPossibleRegion()); + + // Set mask iterator + typedef itk::ImageRegionIterator IteratorMaskType; + IteratorMaskType itm(mask, mask->GetLargestPossibleRegion()); + + //typedef itk::MinimumMaximumImageCalculator ImageCalculatorFilterType; + //typename ImageCalculatorFilterType::Pointer imageCalculatorFilter = ImageCalculatorFilterType::New(); + //imageCalculatorFilter->SetImage(gradientFilter->GetOutput()); + //imageCalculatorFilter->Compute(); + typedef itk::LabelStatisticsImageFilter< FloatImageType, MaskImageType > LabelStatisticsImageFilterType; + typename LabelStatisticsImageFilterType::Pointer labelStatisticsImageFilter = LabelStatisticsImageFilterType::New(); + labelStatisticsImageFilter->SetLabelInput( mask ); + labelStatisticsImageFilter->SetInput(laplacianFilter->GetOutput()); + labelStatisticsImageFilter->Update(); + + //std::cout << "Number of labels: " << labelStatisticsImageFilter->GetNumberOfLabels() << std::endl; + + float minImg = labelStatisticsImageFilter->GetMinimum(1); + //std::cout << "minImg: " << minImg << std::endl; + float maxImg = labelStatisticsImageFilter->GetMaximum(1); + //std::cout << "maxImg: " << maxImg << std::endl; + + it.GoToBegin(); + ito.GoToBegin(); + itm.GoToBegin(); + + while (!ito.IsAtEnd()) { + if(mArgsInfo.normalize_flag && itm.Get() == 1) { + ito.Set(((float) it.Get() - minImg)/(maxImg-minImg)); + } + if (mArgsInfo.normalize_flag == 0 && itm.Get() == 1) { + ito.Set((float) it.Get()); + } + ++it; + ++ito; + ++itm; + } + + //typename OutputImageType::Pointer outputImage = gradientFilter->GetOutput(); + this->template SetNextOutput(outputImage); +} +//-------------------------------------------------------------------- + + +}//end clitk + +#endif //#define clitkImageLaplacianGenericFilter_txx -- 2.45.1