]> Creatis software - clitk.git/blob - common/clitkImageUtilities.txx
Initial revision
[clitk.git] / common / clitkImageUtilities.txx
1 #ifndef CLITKIMAGEUTILITIES_TXX
2 #define CLITKIMAGEUTILITIES_TXX
3
4 /**
5  =================================================
6  * @file   clitkImageUtilities.txx
7  * @author David Sarrut <david.sarrut@creatis.insa-lyon.fr>
8  * @date   22 Sep 2006 10:39:48
9  * 
10  * @brief  
11  * 
12  * 
13  =================================================*/
14
15 //====================================================================
16 // Compute the number of different intensities in an image
17 template<class ImageType>
18 int ComputeHowManyDifferentIntensity(const typename ImageType::Pointer & image, 
19                                                                          std::vector<typename ImageType::PixelType> & l)
20 {
21   //std::set<typename ImageType::PixelType> listOfIntensities;
22   std::map<typename ImageType::PixelType, bool> listOfIntensities;
23   //  listOfIntensities.resize(0);
24   typedef itk::ImageRegionConstIterator<ImageType> ConstIteratorType;
25   ConstIteratorType pi(image, image->GetLargestPossibleRegion());
26   pi.Begin();
27   while (!pi.IsAtEnd()) {
28         if (!listOfIntensities[pi.Get()]) listOfIntensities[pi.Get()] = true;
29         // if (std::find(listOfIntensities.begin(), 
30 //                                listOfIntensities.end(), 
31 //                                pi.Get()) == listOfIntensities.end()) {
32 //        listOfIntensities.insert(pi.Get());
33 //      }
34         ++pi;
35   }
36   
37   //typename std::set<typename ImageType::PixelType>::const_iterator ppi = listOfIntensities.begin();
38   typename std::map<typename ImageType::PixelType, bool>::const_iterator ppi = listOfIntensities.begin();
39   while (ppi != listOfIntensities.end()) {
40         l.push_back(ppi->first);
41         ++ppi;
42   }
43
44   return listOfIntensities.size();
45 }
46 //====================================================================
47   
48 //====================================================================
49 template<class InputImageType, class MaskImageType>
50 void ComputeWeightsOfEachClasses(const typename InputImageType::Pointer & input, 
51                                                                  const typename MaskImageType::Pointer & mask,
52                                                                  const std::vector<typename MaskImageType::PixelType> & listOfIntensities, 
53                                                                  std::map<typename MaskImageType::PixelType, 
54                                                                  std::map<typename InputImageType::PixelType, double> > & mapOfLabelsAndWeights) {
55   // Check size
56   if (input->GetLargestPossibleRegion() != mask->GetLargestPossibleRegion()) {
57         itkGenericExceptionMacro(<< "Input and mask images have not the same size"
58                                                          << std::endl
59                                                          << "Input = " << input->GetLargestPossibleRegion()
60                                                          << std::endl
61                                                          << "Mask = " << mask->GetLargestPossibleRegion());
62   }
63
64   // reset weights list
65   mapOfLabelsAndWeights.clear();
66
67   // loop
68   typedef itk::ImageRegionConstIterator<InputImageType> ConstInputIteratorType;
69   ConstInputIteratorType pi(input, input->GetLargestPossibleRegion());
70   typedef itk::ImageRegionConstIterator<MaskImageType> ConstMaskIteratorType;
71   ConstMaskIteratorType pm(mask, mask->GetLargestPossibleRegion());
72   pi.Begin();
73   pm.Begin();
74   while (!pi.IsAtEnd()) {
75         mapOfLabelsAndWeights[pm.Get()][pi.Get()]++;
76         ++pi;
77         ++pm;
78   }
79 }
80 //====================================================================
81
82 // //====================================================================
83 // template<class ImageType>
84 // typename ImageType::Pointer NewImage3D(int x, int y, int z, float dx, float dy, float dz) {  
85 //   typename ImageType::Pointer output = ImageType::New();
86 //   typename ImageType::RegionType region;
87 //   typename ImageType::SizeType size;
88 //   size[0] = x;
89 //   size[1] = y;
90 //   size[2] = z;
91 //   region.SetSize(size);
92 //   output->SetRegions(region);
93 //   output->Allocate();
94 //   typename ImageType::SpacingType spacing;  
95 //   spacing[0] = dx;
96 //   spacing[1] = dy;
97 //   spacing[2] = dz;
98 //   output->SetSpacing(spacing);
99 //   return output;
100 // }
101 // //====================================================================
102
103
104 #endif /* end #define CLITKIMAGEUTILITIES_TXX */
105