From bb584c26a90ee926ae1769537b36123c815ea942 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Beno=C3=AEt=20Presles?= Date: Thu, 17 Oct 2013 18:11:18 +0200 Subject: [PATCH] added an optional mask + the possibility to get a normalized output image --- tools/clitkImageGradientMagnitude.ggo | 2 + ...clitkImageGradientMagnitudeGenericFilter.h | 3 + ...itkImageGradientMagnitudeGenericFilter.txx | 204 ++++++++++++------ 3 files changed, 145 insertions(+), 64 deletions(-) diff --git a/tools/clitkImageGradientMagnitude.ggo b/tools/clitkImageGradientMagnitude.ggo index ae0d84f..4677ac3 100644 --- a/tools/clitkImageGradientMagnitude.ggo +++ b/tools/clitkImageGradientMagnitude.ggo @@ -9,4 +9,6 @@ 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/clitkImageGradientMagnitudeGenericFilter.h b/tools/clitkImageGradientMagnitudeGenericFilter.h index 9d2b5f5..660ff89 100644 --- a/tools/clitkImageGradientMagnitudeGenericFilter.h +++ b/tools/clitkImageGradientMagnitudeGenericFilter.h @@ -57,6 +57,9 @@ protected: template void InitializeImageType(); args_info_type mArgsInfo; +private: + bool m_NormalizeOutput; + }; // end class //-------------------------------------------------------------------- diff --git a/tools/clitkImageGradientMagnitudeGenericFilter.txx b/tools/clitkImageGradientMagnitudeGenericFilter.txx index 988aecb..3167a1e 100644 --- a/tools/clitkImageGradientMagnitudeGenericFilter.txx +++ b/tools/clitkImageGradientMagnitudeGenericFilter.txx @@ -29,6 +29,7 @@ // itk include #include "itkGradientMagnitudeImageFilter.h" +#include "itkLabelStatisticsImageFilter.h"" #include "itkMaskImageFilter.h" #include "itkMaskNegatedImageFilter.h" #include @@ -36,70 +37,145 @@ namespace clitk { -//-------------------------------------------------------------------- -template -ImageGradientMagnitudeGenericFilter::ImageGradientMagnitudeGenericFilter(): - ImageToImageGenericFilter("ImageGradientMagnitude") -{ - InitializeImageType<2>(); - InitializeImageType<3>(); -} -//-------------------------------------------------------------------- - - -//-------------------------------------------------------------------- -template -template -void ImageGradientMagnitudeGenericFilter::InitializeImageType() -{ - ADD_DEFAULT_IMAGE_TYPES(Dim); -} -//-------------------------------------------------------------------- - - -//-------------------------------------------------------------------- -template -void ImageGradientMagnitudeGenericFilter::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); - } -} -//-------------------------------------------------------------------- - -//-------------------------------------------------------------------- -// Update with the number of dimensions and the pixeltype -//-------------------------------------------------------------------- -template -template -void -ImageGradientMagnitudeGenericFilter::UpdateWithInputImageType() -{ - - // Reading input - typename InputImageType::Pointer input = this->template GetInput(0); - - // Main filter - typedef typename InputImageType::PixelType PixelType; - typedef itk::Image OutputImageType; - - // Filter - typedef itk::GradientMagnitudeImageFilter GradientMagnitudeImageFilterType; - typename GradientMagnitudeImageFilterType::Pointer gradientFilter=GradientMagnitudeImageFilterType::New(); - gradientFilter->SetInput(input); - gradientFilter->Update(); - - typename OutputImageType::Pointer outputImage = gradientFilter->GetOutput(); - this->template SetNextOutput(outputImage); -} -//-------------------------------------------------------------------- + //-------------------------------------------------------------------- + template + ImageGradientMagnitudeGenericFilter::ImageGradientMagnitudeGenericFilter(): + ImageToImageGenericFilter("ImageGradientMagnitude") + { + InitializeImageType<2>(); + InitializeImageType<3>(); + } + //-------------------------------------------------------------------- + + + //-------------------------------------------------------------------- + template + template + void ImageGradientMagnitudeGenericFilter::InitializeImageType() + { + ADD_DEFAULT_IMAGE_TYPES(Dim); + } + //-------------------------------------------------------------------- + + + //-------------------------------------------------------------------- + template + void ImageGradientMagnitudeGenericFilter::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.normalize_flag) { + this->m_NormalizeOutput = 1; + } + else { + this->m_NormalizeOutput = 0; + } + if (mArgsInfo.mask_given) { + this->AddInputFilename(mArgsInfo.mask_arg); + } + } + //-------------------------------------------------------------------- + + //-------------------------------------------------------------------- + // Update with the number of dimensions and the pixeltype + //-------------------------------------------------------------------- + template + template + void + ImageGradientMagnitudeGenericFilter::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 + typedef itk::GradientMagnitudeImageFilter GradientMagnitudeImageFilterType; + typename GradientMagnitudeImageFilterType::Pointer gradientFilter=GradientMagnitudeImageFilterType::New(); + gradientFilter->SetInput(input); + gradientFilter->Update(); + // Set iterator + typedef itk::ImageRegionIterator IteratorType; + IteratorType it(gradientFilter->GetOutput(), gradientFilter->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< OutputImageType, MaskImageType > LabelStatisticsImageFilterType; + typename LabelStatisticsImageFilterType::Pointer labelStatisticsImageFilter = LabelStatisticsImageFilterType::New(); + labelStatisticsImageFilter->SetLabelInput( mask ); + labelStatisticsImageFilter->SetInput(gradientFilter->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(m_NormalizeOutput && itm.Get() == 1) { + ito.Set(((float) it.Get() - minImg)/(maxImg-minImg)); + } + if (m_NormalizeOutput == 0 && itm.Get() == 1) { + ito.Set((float) it.Get()); + } + ++it; + ++ito; + ++itm; + } + + //typename OutputImageType::Pointer outputImage = gradientFilter->GetOutput(); + this->template SetNextOutput(outputImage); + } + //-------------------------------------------------------------------- }//end clitk -- 2.47.1