1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://www.centreleonberard.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
18 #ifndef clitkImageGradientMagnitudeGenericFilter_txx
19 #define clitkImageGradientMagnitudeGenericFilter_txx
21 /* =================================================
22 * @file clitkImageGradientMagnitudeGenericFilter.txx
23 * @author Jef Vandemeulebroucke <jef@creatis.insa-lyon.fr>
28 ===================================================*/
31 #include "itkGradientMagnitudeImageFilter.h"
32 #include "itkGradientMagnitudeRecursiveGaussianImageFilter.h"
33 #include "itkLabelStatisticsImageFilter.h"
34 #include "itkMaskImageFilter.h"
35 #include "itkMaskNegatedImageFilter.h"
36 #include <clitkCommon.h>
41 //--------------------------------------------------------------------
42 template<class args_info_type>
43 ImageGradientMagnitudeGenericFilter<args_info_type>::ImageGradientMagnitudeGenericFilter():
44 ImageToImageGenericFilter<Self>("ImageGradientMagnitude")
46 InitializeImageType<2>();
47 InitializeImageType<3>();
49 //--------------------------------------------------------------------
52 //--------------------------------------------------------------------
53 template<class args_info_type>
54 template<unsigned int Dim>
55 void ImageGradientMagnitudeGenericFilter<args_info_type>::InitializeImageType()
57 ADD_DEFAULT_IMAGE_TYPES(Dim);
59 //--------------------------------------------------------------------
62 //--------------------------------------------------------------------
63 template<class args_info_type>
64 void ImageGradientMagnitudeGenericFilter<args_info_type>::SetArgsInfo(const args_info_type & a)
67 this->SetIOVerbose(mArgsInfo.verbose_flag);
68 if (mArgsInfo.imagetypes_flag) this->PrintAvailableImageTypes();
70 if (mArgsInfo.input_given) {
71 this->SetInputFilename(mArgsInfo.input_arg);
73 if (mArgsInfo.output_given) {
74 this->SetOutputFilename(mArgsInfo.output_arg);
77 if (mArgsInfo.normalize_flag) {
78 this->m_NormalizeOutput = 1;
81 this->m_NormalizeOutput = 0;
83 if (mArgsInfo.mask_given) {
84 this->AddInputFilename(mArgsInfo.mask_arg);
87 //--------------------------------------------------------------------
89 //--------------------------------------------------------------------
90 // Update with the number of dimensions and the pixeltype
91 //--------------------------------------------------------------------
92 template<class args_info_type>
93 template<class InputImageType>
95 ImageGradientMagnitudeGenericFilter<args_info_type>::UpdateWithInputImageType()
98 typedef typename InputImageType::PixelType InputPixelType;
99 typedef itk::Image<float, InputImageType::ImageDimension> OutputImageType;
100 typedef itk::Image<unsigned char, OutputImageType::ImageDimension> MaskImageType;
103 typename InputImageType::Pointer input = this->template GetInput<InputImageType>(0);
105 typename MaskImageType::Pointer mask = NULL;
106 if(mArgsInfo.mask_given) {
107 mask = this->template GetInput<MaskImageType>(1);
110 mask = MaskImageType::New();
111 mask->SetRegions(input->GetLargestPossibleRegion());
112 mask->SetOrigin(input->GetOrigin());
113 mask->SetSpacing(input->GetSpacing());
119 // Create output image
120 typename OutputImageType::Pointer outputImage = OutputImageType::New();
121 outputImage->SetRegions(input->GetLargestPossibleRegion());
122 outputImage->SetOrigin(input->GetOrigin());
123 outputImage->SetSpacing(input->GetSpacing());
124 outputImage->Allocate();
125 outputImage->FillBuffer(0.0);
126 // Set output iterator
127 typedef itk::ImageRegionIterator<OutputImageType> IteratorOutputType;
128 IteratorOutputType ito = IteratorOutputType(outputImage, outputImage->GetLargestPossibleRegion());
131 typename OutputImageType::Pointer outputGradientFilter;
132 if (mArgsInfo.gaussian_filter_flag == 0) {
133 typedef itk::GradientMagnitudeImageFilter<InputImageType, OutputImageType> GradientMagnitudeImageFilterType;
134 typename GradientMagnitudeImageFilterType::Pointer gradientFilter=GradientMagnitudeImageFilterType::New();
135 gradientFilter->SetInput(input);
136 gradientFilter->Update();
137 outputGradientFilter = gradientFilter->GetOutput();
140 typedef itk::GradientMagnitudeRecursiveGaussianImageFilter<InputImageType, OutputImageType> GradientMagnitudeImageFilterType;
141 typename GradientMagnitudeImageFilterType::Pointer gradientFilter=GradientMagnitudeImageFilterType::New();
142 gradientFilter->SetInput(input);
143 gradientFilter->Update();
144 outputGradientFilter = gradientFilter->GetOutput();
147 typedef itk::ImageRegionIterator<OutputImageType> IteratorType;
148 IteratorType it(outputGradientFilter, outputGradientFilter->GetLargestPossibleRegion());
151 typedef itk::ImageRegionIterator<MaskImageType> IteratorMaskType;
152 IteratorMaskType itm(mask, mask->GetLargestPossibleRegion());
154 //typedef itk::MinimumMaximumImageCalculator <OutputImageType> ImageCalculatorFilterType;
155 //typename ImageCalculatorFilterType::Pointer imageCalculatorFilter = ImageCalculatorFilterType::New();
156 //imageCalculatorFilter->SetImage(outputGradientFilter);
157 //imageCalculatorFilter->Compute();
158 typedef itk::LabelStatisticsImageFilter< OutputImageType, MaskImageType > LabelStatisticsImageFilterType;
159 typename LabelStatisticsImageFilterType::Pointer labelStatisticsImageFilter = LabelStatisticsImageFilterType::New();
160 labelStatisticsImageFilter->SetLabelInput( mask );
161 labelStatisticsImageFilter->SetInput(outputGradientFilter);
162 labelStatisticsImageFilter->Update();
164 //std::cout << "Number of labels: " << labelStatisticsImageFilter->GetNumberOfLabels() << std::endl;
166 float minImg = labelStatisticsImageFilter->GetMinimum(1);
167 //std::cout << "minImg: " << minImg << std::endl;
168 float maxImg = labelStatisticsImageFilter->GetMaximum(1);
169 //std::cout << "maxImg: " << maxImg << std::endl;
175 while (!ito.IsAtEnd()) {
176 if(m_NormalizeOutput && itm.Get() == 1) {
177 ito.Set(((float) it.Get() - minImg)/(maxImg-minImg));
179 if (m_NormalizeOutput == 0 && itm.Get() == 1) {
180 ito.Set((float) it.Get());
187 //typename OutputImageType::Pointer outputImage = outputGradientFilter;
188 this->template SetNextOutput<OutputImageType>(outputImage);
190 //--------------------------------------------------------------------
195 #endif //#define clitkImageGradientMagnitudeGenericFilter_txx