]> Creatis software - clitk.git/blob - common/clitkImageCommon.txx
- add convenient method
[clitk.git] / common / clitkImageCommon.txx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to: 
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://oncora1.lyon.fnclcc.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
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.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
18 #ifndef CLITKIMAGECOMMON_TXX
19 #define CLITKIMAGECOMMON_TXX
20 /**
21    -------------------------------------------------
22    * @file   clitkImageCommon.txx
23    * @author David Sarrut <david.sarrut@creatis.insa-lyon.fr>
24    * @date   07 Sep 2007 11:34:19
25    * 
26    * @brief  
27    * 
28    * 
29    -------------------------------------------------*/
30
31 //--------------------------------------------------------------------
32 template<class PixelType>
33 typename itk::Image<PixelType,1>::Pointer NewImage1D(int vsize, double vspacing) {
34   typedef itk::Image<PixelType,1> ImageType;
35   typename ImageType::Pointer g = ImageType::New();
36   typename ImageType::SizeType size;
37   size[0] = vsize;
38   typename ImageType::RegionType region;
39   region.SetSize(size);
40   g->SetRegions(region);
41   typename ImageType::SpacingType spacing;
42   spacing[0] = vspacing;
43   g->SetSpacing(spacing);
44   return g;
45 }
46 //--------------------------------------------------------------------
47
48 //--------------------------------------------------------------------
49 template<class PixelType>
50 typename itk::Image<PixelType,2>::Pointer NewImage2D(int sx, int sy, double dx, double dy) {
51   typedef itk::Image<PixelType,2> ImageType;
52   typename ImageType::Pointer g = ImageType::New();
53   typename ImageType::SizeType size;
54   size[0] = sx; size[1] = sy;
55   typename ImageType::RegionType region;
56   region.SetSize(size);
57   g->SetRegions(region);
58   typename ImageType::SpacingType spacing;
59   spacing[0] = dx; spacing[1] = dy;
60   g->SetSpacing(spacing);
61   return g;
62 }
63 //--------------------------------------------------------------------
64
65 //--------------------------------------------------------------------
66 template<class PixelType>
67 typename itk::Image<PixelType,3>::Pointer NewImage3D(int sx, int sy, int sz, double dx, double dy, double dz) {
68   typedef itk::Image<PixelType,3> ImageType;
69   typename ImageType::Pointer g = ImageType::New();
70   typename ImageType::SizeType size;
71   size[0] = sx; size[1] = sy; size[2] = sz;
72   typename ImageType::RegionType region;
73   region.SetSize(size);
74   g->SetRegions(region);
75   typename ImageType::SpacingType spacing;
76   spacing[0] = dx; spacing[1] = dy; spacing[2] = dz;
77   g->SetSpacing(spacing);
78   return g;
79 }
80 //--------------------------------------------------------------------
81
82 //--------------------------------------------------------------------
83 template<class PixelType>
84 typename itk::Image<PixelType,4>::Pointer NewImage4D(int sx, int sy, int sz, int st, double dx, double dy, double dz, double dt) {
85   typedef itk::Image<PixelType,3> ImageType;
86   typename ImageType::Pointer g = ImageType::New();
87   typename ImageType::SizeType size;
88   size[0] = sx; size[1] = sy; size[2] = sz; size[3] = st;
89   typename ImageType::RegionType region;
90   region.SetSize(size);
91   g->SetRegions(region);
92   typename ImageType::SpacingType spacing;
93   spacing[0] = dx; spacing[1] = dy; spacing[2] = dz; spacing[3] = dt;
94   g->SetSpacing(spacing);
95   return g;
96 }
97 //--------------------------------------------------------------------
98
99
100 //--------------------------------------------------------------------
101 template<class ImageType>
102 typename ImageType::Pointer NewImageLike(const typename ImageType::Pointer input, bool allocate) {
103   typename ImageType::Pointer output = ImageType::New();
104   output->SetRegions(input->GetLargestPossibleRegion());
105   output->SetOrigin(input->GetOrigin());
106   output->SetSpacing(input->GetSpacing());
107   if (allocate) output->Allocate();
108   return output;
109 }
110 //--------------------------------------------------------------------
111
112
113 //--------------------------------------------------------------------
114 template<class ImageType>
115 void CopyValues(const typename ImageType::Pointer input, 
116                 typename ImageType::Pointer output) {  
117   typedef itk::ImageRegionConstIterator<ImageType> ConstIteratorType; 
118   ConstIteratorType pi(input,input->GetLargestPossibleRegion());
119   pi.GoToBegin();  
120   typedef itk::ImageRegionIterator<ImageType> IteratorType; 
121   IteratorType po(output,input->GetLargestPossibleRegion());
122   po.GoToBegin(); 
123   while (!pi.IsAtEnd()) {
124     po.Set(pi.Get());
125     ++pi;
126     ++po;
127   }
128 }
129 //--------------------------------------------------------------------
130
131
132 //--------------------------------------------------------------------
133 template<class ImageType>
134 typename ImageType::Pointer readImage(const std::string & filename, const bool verbose) {
135   typedef itk::ImageFileReader<ImageType> ReaderType;
136   typename ReaderType::Pointer reader = ReaderType::New();
137   reader->SetFileName(filename.c_str());
138   if (verbose) {
139     std::cout << "Reading [" << filename << "] ... " << std::endl;
140   }
141   try { 
142     reader->Update(); 
143   }
144   catch(itk::ExceptionObject & err) {
145     std::cerr << "Exception while reading image [" << filename << "]" << std::endl;
146     std::cerr << err << std::endl;
147     exit(0);
148   }
149   return reader->GetOutput();
150 }
151 //--------------------------------------------------------------------
152
153 //--------------------------------------------------------------------
154 template<typename ImageType>
155 typename ImageType::Pointer readImage(const std::vector<std::string> & filenames, 
156                                       const bool verbose) {
157   if (filenames.size() == 1) return readImage<ImageType>(filenames[0], verbose);
158   typedef itk::ImageSeriesReader<ImageType> ReaderType;
159   typename ReaderType::Pointer reader = ReaderType::New();
160   reader->SetFileNames(filenames);
161   if (verbose) {
162     std::cout << "Reading " << filenames[0] << " and others ..." << std::endl;
163   }
164   try {
165     reader->Update(); 
166   }
167   catch( itk::ExceptionObject & err ) {
168     std::cerr << "Error while reading " << filenames[0]
169               << " or other files ..." << err << std::endl;
170     exit(0);
171   }
172   return reader->GetOutput();
173 }
174 //--------------------------------------------------------------------
175
176 //--------------------------------------------------------------------
177 template<class ImageType>
178 void writeImage(const typename ImageType::Pointer image, const std::string & filename, const bool verbose) {
179   typedef itk::ImageFileWriter<ImageType> WriterType;
180   typename WriterType::Pointer writer = WriterType::New();
181   writer->SetFileName(filename.c_str());
182   writer->SetInput(image);
183   if (verbose) {
184     std::cout << "Writing [" << filename << "] ... " << std::endl;
185   }
186   try { 
187     writer->Update(); 
188   }
189   catch( itk::ExceptionObject & err ) {
190     std::cerr << "Exception while writing image [" << filename << "]" << std::endl;
191     std::cerr << err << std::endl;
192     exit(-1);
193   }
194 }
195 //--------------------------------------------------------------------
196
197 //--------------------------------------------------------------------
198 template<class ImageType>
199 void writeImage(const ImageType* image, const std::string & filename, const bool verbose) {
200   typedef itk::ImageFileWriter<ImageType> WriterType;
201   typename WriterType::Pointer writer = WriterType::New();
202   writer->SetFileName(filename.c_str());
203   writer->SetInput(image);
204   if (verbose) {
205     std::cout << "Writing [" << filename << "] ... " << std::endl;
206   }
207   try { 
208     writer->Update(); 
209   }
210   catch( itk::ExceptionObject & err ) {
211     std::cerr << "Exception while writing image [" << filename << "]" << std::endl;
212     std::cerr << err << std::endl;
213     exit(-1);
214   }
215 }
216 // //--------------------------------------------------------------------
217
218 // //--------------------------------------------------------------------
219 // template<class ImageType>
220 // void writeImage(const typename ImageType::ConstPointer image, const std::string & filename, const bool verbose=false) {
221 //   typedef itk::ImageFileWriter<ImageType> WriterType;
222 //   typename WriterType::Pointer writer = WriterType::New();
223 //   writer->SetFileName(filename.c_str());
224 //   writer->SetInput(image);
225 //   if (verbose) {
226 //     std::cout << "Writing [" << filename << "] ... " << std::endl;
227 //   }
228 //   try { 
229 //     writer->Update(); 
230 //   }
231 //   catch( itk::ExceptionObject & err ) {
232 //     std::cerr << "Exception while writing image [" << filename << "]" << std::endl;
233 //     std::cerr << err << std::endl;
234 //     exit(-1);
235 //   }
236 // }
237
238 //--------------------------------------------------------------------
239
240 //--------------------------------------------------------------------
241 // Compute the number of different intensities in an image
242 template<class ImageType>
243 int ComputeHowManyDifferentIntensity(const typename ImageType::Pointer & image, 
244                                      std::vector<typename ImageType::PixelType> & l)
245 {
246   //std::set<typename ImageType::PixelType> listOfIntensities;
247   std::map<typename ImageType::PixelType, bool> listOfIntensities;
248   //  listOfIntensities.resize(0);
249   typedef itk::ImageRegionConstIterator<ImageType> ConstIteratorType;
250   ConstIteratorType pi(image, image->GetLargestPossibleRegion());
251   pi.Begin();
252   while (!pi.IsAtEnd()) {
253     if (!listOfIntensities[pi.Get()]) listOfIntensities[pi.Get()] = true;
254     // if (std::find(listOfIntensities.begin(), 
255     //                            listOfIntensities.end(), 
256     //                            pi.Get()) == listOfIntensities.end()) {
257     //    listOfIntensities.insert(pi.Get());
258     //  }
259     ++pi;
260   }
261   
262   //typename std::set<typename ImageType::PixelType>::const_iterator ppi = listOfIntensities.begin();
263   typename std::map<typename ImageType::PixelType, bool>::const_iterator ppi = listOfIntensities.begin();
264   while (ppi != listOfIntensities.end()) {
265     l.push_back(ppi->first);
266     ++ppi;
267   }
268
269   return listOfIntensities.size();
270 }
271 //--------------------------------------------------------------------
272   
273 //--------------------------------------------------------------------
274 template<class InputImageType, class MaskImageType>
275 void ComputeWeightsOfEachClasses(const typename InputImageType::Pointer & input, 
276                                  const typename MaskImageType::Pointer & mask,
277                                  const std::vector<typename MaskImageType::PixelType> & listOfIntensities, 
278                                  std::map<typename MaskImageType::PixelType, 
279                                  std::map<typename InputImageType::PixelType, double> > & mapOfLabelsAndWeights) {
280   // Check size
281   if (input->GetLargestPossibleRegion() != mask->GetLargestPossibleRegion()) {
282     itkGenericExceptionMacro(<< "Input and mask images have not the same size"
283                              << std::endl
284                              << "Input = " << input->GetLargestPossibleRegion()
285                              << std::endl
286                              << "Mask = " << mask->GetLargestPossibleRegion());
287   }
288
289   // reset weights list
290   mapOfLabelsAndWeights.clear();
291
292   // loop
293   typedef itk::ImageRegionConstIterator<InputImageType> ConstInputIteratorType;
294   ConstInputIteratorType pi(input, input->GetLargestPossibleRegion());
295   typedef itk::ImageRegionConstIterator<MaskImageType> ConstMaskIteratorType;
296   ConstMaskIteratorType pm(mask, mask->GetLargestPossibleRegion());
297   pi.Begin();
298   pm.Begin();
299   while (!pi.IsAtEnd()) {
300     mapOfLabelsAndWeights[pm.Get()][pi.Get()]++;
301     ++pi;
302     ++pm;
303   }
304 }
305 //--------------------------------------------------------------------
306
307 #endif /* end #define CLITKIMAGECOMMON_TXX */
308