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 clitkHistogramImageGenericFilter_cxx
19 #define clitkHistogramImageGenericFilter_cxx
21 /* =================================================
22 * @file clitkHistogramImageGenericFilter.cxx
23 * @author Thomas Baudier <thomas.baudier@creatis.insa-lyon.fr>
28 ===================================================*/
30 #include "clitkHistogramImageGenericFilter.h"
33 #include <itkImageToHistogramFilter.h>
34 #include <itkStatisticsImageFilter.h>
36 #include <clitkCommon.h>
43 //--------------------------------------------------------------------
44 HistogramImageGenericFilter::HistogramImageGenericFilter():
45 ImageToImageGenericFilter<Self>("HistogramImage")
47 InitializeImageType<2>();
48 InitializeImageType<3>();
49 InitializeImageType<4>();
52 //--------------------------------------------------------------------
55 //--------------------------------------------------------------------
56 template<unsigned int Dim>
57 void HistogramImageGenericFilter::InitializeImageType()
59 ADD_DEFAULT_IMAGE_TYPES(Dim);
61 //--------------------------------------------------------------------
64 //--------------------------------------------------------------------
65 vtkFloatArray* HistogramImageGenericFilter::GetArrayX()
69 //--------------------------------------------------------------------
72 //--------------------------------------------------------------------
73 vtkFloatArray* HistogramImageGenericFilter::GetArrayY()
77 //--------------------------------------------------------------------
80 //--------------------------------------------------------------------
81 void HistogramImageGenericFilter::SetArgsInfo(const args_info_type & a)
84 if (mArgsInfo.verbose_given)
85 SetIOVerbose(mArgsInfo.verbose_flag);
86 if (mArgsInfo.imagetypes_given && mArgsInfo.imagetypes_flag)
87 this->PrintAvailableImageTypes();
89 if (mArgsInfo.input_given) {
90 SetInputFilename(mArgsInfo.input_arg);
92 if (mArgsInfo.output_given) {
93 SetOutputFilename(mArgsInfo.output_arg);
95 if (mArgsInfo.size_given) {
96 SetSizeBin(mArgsInfo.size_arg);
99 //--------------------------------------------------------------------
102 //--------------------------------------------------------------------
103 void HistogramImageGenericFilter::SetSizeBin(const double size)
107 //--------------------------------------------------------------------
110 //--------------------------------------------------------------------
111 // Update with the number of dimensions and the pixeltype
112 //--------------------------------------------------------------------
113 template<class InputImageType>
115 HistogramImageGenericFilter::UpdateWithInputImageType()
119 typename InputImageType::Pointer input = this->template GetInput<InputImageType>(0);
120 typedef typename InputImageType::PixelType PixelType;
121 typedef typename InputImageType::IndexType IndexType;
122 typedef typename itk::Statistics::ImageToHistogramFilter<InputImageType> HistogramFilterType;
123 typedef typename itk::StatisticsImageFilter<InputImageType> StatisticsImageFilterType;
126 typename StatisticsImageFilterType::Pointer statisticsImageFilter = StatisticsImageFilterType::New ();
127 statisticsImageFilter->SetInput(input);
128 statisticsImageFilter->Update();
129 double range = statisticsImageFilter->GetMaximum() - statisticsImageFilter->GetMinimum();
131 typedef typename HistogramFilterType::HistogramSizeType SizeType;
132 SizeType binNumber(1);
133 if (!mArgsInfo.size_given)
134 mBinSize = std::log(range);
137 binNumber[0] = (int)(range/mBinSize);
138 if (binNumber[0] == 0)
142 typename HistogramFilterType::Pointer histogramFilter = HistogramFilterType::New();
143 histogramFilter->SetHistogramSize(binNumber);
144 histogramFilter->SetAutoMinimumMaximum(true);
145 typename HistogramFilterType::HistogramMeasurementVectorType lowerBound(1);
146 typename HistogramFilterType::HistogramMeasurementVectorType upperBound(1);
147 lowerBound[0] = statisticsImageFilter->GetMinimum();
148 upperBound[0] = statisticsImageFilter->GetMaximum();
149 histogramFilter->SetHistogramBinMinimum(lowerBound);
150 histogramFilter->SetHistogramBinMaximum(upperBound);
151 histogramFilter->SetInput(input);
152 histogramFilter->Update();
154 mArrayX = vtkSmartPointer<vtkFloatArray>::New();
155 mArrayY = vtkSmartPointer<vtkFloatArray>::New();
157 for(unsigned int i = 0; i < histogramFilter->GetOutput()->GetSize()[0]; ++i)
159 mArrayY->InsertNextTuple1(histogramFilter->GetOutput()->GetFrequency(i));
160 mArrayX->InsertNextTuple1(statisticsImageFilter->GetMinimum() + (i+0.5)*mBinSize);
163 //--------------------------------------------------------------------
165 //------------------------------------------------------------------------------
166 void HistogramImageGenericFilter::SaveAs()
169 std::string textFileName = GetOutputFilename();
170 ofstream fileOpen(textFileName.c_str(), std::ofstream::trunc);
173 cerr << "Error during saving" << endl;
178 fileOpen << "Value represents the number of voxels around the corresponding intensity (by default the windows size around intensity is log(range))" << endl;
179 fileOpen << "Intensity" << "\t" << "Value" << endl;
181 while (i<mArrayX->GetNumberOfTuples()) {
182 fileOpen << mArrayX->GetTuple(i)[0] << "\t" << mArrayY->GetTuple(i)[0] << endl;
188 //------------------------------------------------------------------------------
193 #endif //#define clitkHistogramImageGenericFilter_cxx