X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;ds=sidebyside;f=tools%2FclitkNormalizeImageFilterGenericFilter.txx;fp=tools%2FclitkNormalizeImageFilterGenericFilter.txx;h=99a43a464ad8157367caf9f5ed7e301b0736ac64;hb=bf97e857638ed65c07c8ee1835cf5373d6b381d6;hp=0000000000000000000000000000000000000000;hpb=69ca7b4f56b0873c4446859655712091b44e6434;p=clitk.git diff --git a/tools/clitkNormalizeImageFilterGenericFilter.txx b/tools/clitkNormalizeImageFilterGenericFilter.txx new file mode 100644 index 0000000..99a43a4 --- /dev/null +++ b/tools/clitkNormalizeImageFilterGenericFilter.txx @@ -0,0 +1,165 @@ +/*========================================================================= + 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 clitkNormalizeImageFilterGenericFilter_txx +#define clitkNormalizeImageFilterGenericFilter_txx + +/* ================================================= + * @file clitkNormalizeImageFilterGenericFilter.txx + * @author Jef Vandemeulebroucke + * @date 29 june 2009 + * + * @brief + * + ===================================================*/ + +// itk include +#include "itkLabelStatisticsImageFilter.h" +#include "itkMaskImageFilter.h" +#include "itkMaskNegatedImageFilter.h" +#include + +namespace clitk +{ + + //-------------------------------------------------------------------- + template + NormalizeImageFilterGenericFilter::NormalizeImageFilterGenericFilter(): + ImageToImageGenericFilter("NormalizeImageFilter") + { + InitializeImageType<2>(); + InitializeImageType<3>(); + } + //-------------------------------------------------------------------- + + + //-------------------------------------------------------------------- + template + template + void NormalizeImageFilterGenericFilter::InitializeImageType() + { + ADD_DEFAULT_IMAGE_TYPES(Dim); + } + //-------------------------------------------------------------------- + + + //-------------------------------------------------------------------- + template + void NormalizeImageFilterGenericFilter::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 + NormalizeImageFilterGenericFilter::UpdateWithInputImageType() + { + + // Main filter + typedef typename InputImageType::PixelType InputPixelType; + typedef itk::Image OutputImageType; + typedef itk::Image MaskImageType; + + // Reading input + typename InputImageType::Pointer input = this->template GetInput(0); + + 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 OutputImageType::Pointer outputImage = OutputImageType::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 + // Set iterator + typedef itk::ImageRegionIterator IteratorType; + IteratorType it(input, input->GetLargestPossibleRegion()); + + // Set mask iterator + typedef itk::ImageRegionIterator IteratorMaskType; + IteratorMaskType itm(mask, mask->GetLargestPossibleRegion()); + + typedef itk::LabelStatisticsImageFilter< InputImageType, MaskImageType > LabelStatisticsImageFilterType; + typename LabelStatisticsImageFilterType::Pointer labelStatisticsImageFilter = LabelStatisticsImageFilterType::New(); + labelStatisticsImageFilter->SetLabelInput( mask ); + labelStatisticsImageFilter->SetInput(input); + 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(itm.Get() == 1) { + ito.Set(((float) it.Get() - minImg)/(maxImg-minImg)); + } + ++it; + ++ito; + ++itm; + } + // + // + // Write/Save results + this->template SetNextOutput(outputImage); + } + //-------------------------------------------------------------------- + + +}//end clitk + +#endif //#define clitkNormalizeImageFilterGenericFilter_txx