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 clitkBinarizeImageGenericFilter_cxx
19 #define clitkBinarizeImageGenericFilter_cxx
21 /* =================================================
22 * @file clitkBinarizeImageGenericFilter.cxx
23 * @author Jef Vandemeulebroucke <jef@creatis.insa-lyon.fr>
28 ===================================================*/
30 #include "clitkBinarizeImageGenericFilter.h"
33 #include "itkBinaryThresholdImageFilter.h"
34 #include "itkMaskImageFilter.h"
35 #include "itkMaskNegatedImageFilter.h"
37 #include <clitkCommon.h>
42 //--------------------------------------------------------------------
43 BinarizeImageGenericFilter::BinarizeImageGenericFilter():
44 ImageToImageGenericFilter<Self>("BinarizeImage")
46 InitializeImageType<2>();
47 InitializeImageType<3>();
48 InitializeImageType<4>();
50 //--------------------------------------------------------------------
53 //--------------------------------------------------------------------
54 template<unsigned int Dim>
55 void BinarizeImageGenericFilter::InitializeImageType()
57 ADD_DEFAULT_IMAGE_TYPES(Dim);
59 //--------------------------------------------------------------------
62 //--------------------------------------------------------------------
63 void BinarizeImageGenericFilter::SetArgsInfo(const args_info_type & a)
66 if (mArgsInfo.verbose_given)
67 SetIOVerbose(mArgsInfo.verbose_flag);
68 if (mArgsInfo.imagetypes_given && mArgsInfo.imagetypes_flag)
69 this->PrintAvailableImageTypes();
71 if (mArgsInfo.input_given) {
72 SetInputFilename(mArgsInfo.input_arg);
74 if (mArgsInfo.output_given) {
75 SetOutputFilename(mArgsInfo.output_arg);
77 if (mArgsInfo.percentage_given) {
78 SetPercentage(mArgsInfo.percentage_arg);
81 //--------------------------------------------------------------------
84 //--------------------------------------------------------------------
85 // Update with the number of dimensions and the pixeltype
86 //--------------------------------------------------------------------
87 template<class InputImageType>
89 BinarizeImageGenericFilter::UpdateWithInputImageType()
91 if (mArgsInfo.percentage_given)
92 MaskOfIntegratedIntensity<InputImageType>();
95 typename InputImageType::Pointer input = this->template GetInput<InputImageType>(0);
98 typedef typename InputImageType::PixelType PixelType;
99 typedef itk::Image<uchar, InputImageType::ImageDimension> OutputImageType;
102 typedef itk::BinaryThresholdImageFilter<InputImageType, OutputImageType> BinaryThresholdImageFilterType;
103 typename BinaryThresholdImageFilterType::Pointer thresholdFilter=BinaryThresholdImageFilterType::New();
104 thresholdFilter->SetInput(input);
105 thresholdFilter->SetInsideValue(mArgsInfo.fg_arg);
107 if (mArgsInfo.lower_given) thresholdFilter->SetLowerThreshold(static_cast<PixelType>(mArgsInfo.lower_arg));
108 if (mArgsInfo.upper_given) thresholdFilter->SetUpperThreshold(static_cast<PixelType>(mArgsInfo.upper_arg));
111 - FG -> only use FG value for pixel in the Foreground (or Inside), keep input values for outside
112 - BG -> only use BG value for pixel in the Background (or Outside), keep input values for inside
113 - both -> use FG and BG (real binary image)
115 if (mArgsInfo.mode_arg == std::string("both")) {
116 thresholdFilter->SetOutsideValue(mArgsInfo.bg_arg);
117 thresholdFilter->Update();
118 typename OutputImageType::Pointer outputImage = thresholdFilter->GetOutput();
119 this->template SetNextOutput<OutputImageType>(outputImage);
121 typename InputImageType::Pointer outputImage;
122 thresholdFilter->SetOutsideValue(0);
123 if (mArgsInfo.mode_arg == std::string("BG")) {
124 typedef itk::MaskImageFilter<InputImageType,OutputImageType> maskFilterType;
125 typename maskFilterType::Pointer maskFilter = maskFilterType::New();
126 maskFilter->SetInput1(input);
127 maskFilter->SetInput2(thresholdFilter->GetOutput());
128 maskFilter->SetOutsideValue(mArgsInfo.bg_arg);
129 maskFilter->Update();
130 outputImage = maskFilter->GetOutput();
132 typedef itk::MaskNegatedImageFilter<InputImageType,OutputImageType> maskFilterType;
133 typename maskFilterType::Pointer maskFilter = maskFilterType::New();
134 maskFilter->SetInput1(input);
135 maskFilter->SetInput2(thresholdFilter->GetOutput());
136 maskFilter->SetOutsideValue(mArgsInfo.fg_arg);
137 maskFilter->Update();
138 outputImage = maskFilter->GetOutput();
140 // Write/Save results
141 this->template SetNextOutput<InputImageType>(outputImage);
145 //--------------------------------------------------------------------
148 //--------------------------------------------------------------------
149 // https://stackoverflow.com/questions/1577475/c-sorting-and-keeping-track-of-indexes
150 template <typename T>
151 std::vector<size_t> sort_indexes(const std::vector<T> &v) {
153 // initialize original index locations
154 std::vector<size_t> idx(v.size());
155 std::vector<std::pair<T, size_t> > compVector(v.size());
156 for (size_t i = 0; i < v.size(); ++i) {
157 compVector[i].first = v[i];
158 compVector[i].second = i;
161 // sort indexes based on comparing values in v
162 std::sort(compVector.begin(), compVector.end(), comparator<T>);
163 for (size_t i = 0; i < v.size(); ++i) {
164 idx[i] = compVector[i].second;
169 //--------------------------------------------------------------------
172 //--------------------------------------------------------------------
173 // Update with the number of dimensions and the pixeltype
174 //--------------------------------------------------------------------
175 template<class InputImageType>
177 BinarizeImageGenericFilter::MaskOfIntegratedIntensity()
180 typedef typename InputImageType::PixelType InputPixelType;
181 typedef itk::Image<unsigned char, InputImageType::ImageDimension> MaskImageType;
184 typename InputImageType::Pointer input = this->template GetInput<InputImageType>(0);
186 typename MaskImageType::Pointer mask;
187 mask = MaskImageType::New();
188 mask->SetRegions(input->GetLargestPossibleRegion());
189 mask->SetOrigin(input->GetOrigin());
190 mask->SetSpacing(input->GetSpacing());
194 // Get a vector of all values (will be easier to sort)
195 // And compute total sum of values
196 std::vector<double> values;
197 typedef itk::ImageRegionIterator<InputImageType> IteratorInputType;
198 IteratorInputType iter(input, input->GetLargestPossibleRegion());
201 while (!iter.IsAtEnd()) {
202 values.push_back(iter.Get());
208 std::vector<size_t> indices = sort_indexes(values);
210 // Get max index of pixel to reach xx percent
211 double current = 0.0;
212 double max = GetPercentage()/100.0*total;
214 int n = input->GetLargestPossibleRegion().GetNumberOfPixels();
215 std::vector<int> should_keep(values.size());;
216 std::fill(should_keep.begin(), should_keep.end(), 0);
217 while (current<max && i<n) { // loop by decreasing pixel values
218 current += values[indices[i]];
219 should_keep[indices[i]] = 1.0;
225 typedef itk::ImageRegionIterator<MaskImageType> IteratorMaskType;
226 IteratorMaskType itm(mask, mask->GetLargestPossibleRegion());
230 while (!iter.IsAtEnd()) {
231 if (should_keep[i]) itm.Set(1);
238 if (this->m_IOVerbose)
239 std::cout << "Sum of pixel values : " << total << std::endl
240 << "Percentage : " << GetPercentage() << "%" << std::endl
241 << "Number of pixels : " << nb << "/" << n << std::endl
242 << "Number of pixels : " << nb/n*100.0 << "%" << std::endl;
244 // Write/Save results
245 this->template SetNextOutput<MaskImageType>(mask);
247 //--------------------------------------------------------------------
249 //--------------------------------------------------------------------
250 template <typename T>
251 bool comparator ( const std::pair<T, size_t>& l, const std::pair<T, size_t>& r)
252 { return l.first > r.first; }
253 //--------------------------------------------------------------------
257 #endif //#define clitkBinarizeImageGenericFilter_cxx