brainrat-private  5.1.2
utility_d.h
Go to the documentation of this file.
1 /* Copyright (C) 2000-2013 CEA
2  *
3  * This software and supporting documentation were developed by
4  * bioPICSEL
5  * CEA/DSV/I²BM/MIRCen/LMN, Batiment 61,
6  * 18, route du Panorama
7  * 92265 Fontenay-aux-Roses
8  * France
9  */
10 
11 #ifndef BRAINRAT_UTILITY_UTILITY_D_H
12 #define BRAINRAT_UTILITY_UTILITY_D_H
13 
14 //--- brainrat ----------------------------------------------------------
15 #include <brainrat/utility/utility.h> // class declaration
16 #include <brainrat/utility/imageprocessor.h> // bio::ImageProcessor
17 #include <brainrat/data/measurevector.h> // bio::MeasureVector
18 #include <brainrat/data/classes.h> // bio::BVclasses
19 //--- aims -------------------------------------------------------------------
20 #include <aims/data/data_g.h> // AimsData
21 #include <aims/io/writer.h> // aims::Writer
22 #include <aims/io/reader.h> // aims::Reader
23 #include <aims/utility/channel.h> // ChannelSelector
24 #include <aims/utility/converter_g.h>
25 #include <aims/utility/progress.h> // aims::ProgressInfo
26 #include <aims/threshold/emthreshold.h> // AimsEMThreshold
27 #include <aims/roi/roiIterator.h> // aims::RoiIterator
28 #include <aims/roi/maskIterator.h> // aims::MaskIterator
29 #include <aims/connectivity/connectivity.h> // aims::Connectivity
30 #include <aims/connectivity/component.h>// aims::AimsLabeledConnectedComponent
31 #include <aims/resampling/linearInterpolator.h> // aims::Inteprolator
32 //--- cartodata --------------------------------------------------------------
33 #include <cartodata/volume/volume.h>
34 //--- soma-io ----------------------------------------------------------------
35 #include <soma-io/allocator/allocator.h>
36 //--- cartobase --------------------------------------------------------------
37 #include <cartobase/stream/fileutil.h>
38 #include <cartobase/smart/rcptr.h>
39 #include <cartobase/type/string_conversion.h>
40 //--- system -----------------------------------------------------------------
41 #include <vector>
42 //----------------------------------------------------------------------------
43 
44 
45 namespace obso_temp {
46 
47 //============================================================================
48 // U T I L I T I E S
49 //============================================================================
50 
51  // Return the brainmask from input image
52  template <typename T>
53  AimsData<int16_t> getBrainMask(const AimsData<T> &image, byte channel)
54  {
55  int dx = image.dimX();
56  int dy = image.dimY();
57  int dz = image.dimZ();
58 
59  // Get channel component
60  ChannelSelector< AimsData<T>, AimsData<double> > selector;
61  AimsData<double> G = selector.select(image, channel);
62 
63  // EM Algorithm TODO : remplacer par un seuillage automatique
64  AimsEMThreshold< double, int16_t> em(AIMS_BETWEEN, 0);
65  AimsData<int16_t> thImage = em.bin(G);
66 
67  // Hypothesis : background = 1 connected component including borders
68  // Get connected components and label image
69  AimsBucket<Void> component; // Components = {background , tissue, ...}
70  AimsData<int16_t> C2(dx, dy, dz); // Label image
71  C2 = aims::AimsLabeledConnectedComponent<int16_t>(
72  component,
73  thImage, // ne modifie pas l'image
74  aims::Connectivity::CONNECTIVITY_6_XYZ,
75  int16_t(-1), // background MODIFIER
76  false, // binary mode
77  (size_t) 0, //minSize
78  (size_t) 0, //number of component Max
79  false //verbose
80  );
81 
82  // Seek for component which label is appearing the most within borders
83  int bordure = 2;
84  AimsData <int> fond (component.size());
85  fond = 0;
86  for (int z = 0; ((z < bordure) && (z < dz)) ; z++) // Parcours de l'image des labels
87  for (int y = 0; ((y < bordure) && (y < dy)); y++)
88  for (int x = 0; ((x < bordure) && (x < dx)); x++)
89  {
90  fond(C2(x, y, z)) ++ ;
91  fond(C2(dx - 1 - x, dy - 1 - y, dz - 1 - z)) ++ ;
92  }
93 
94  int ifond = 0; // indice de la composante du fond
95  for (int icomp = 0; icomp < component.size(); icomp++){ // Parcours des composantes
96  ifond = (fond (ifond) > fond (icomp)) ? ifond : icomp; // On garde max occurences
97  }
98 
99  // Get max size of connected components (excluding background)
100  int max_taille;
101  bool init = true;
102  for (int icomp = 0; icomp < component.size(); icomp++){ // Parcours des composantes
103  std::list<AimsBucketItem<Void> > CC = component[icomp];
104  if (icomp != ifond){
105  if (init){
106  max_taille = CC.size();
107  init = false;
108  }else{
109  max_taille = (max_taille > CC.size()) ? max_taille : CC.size();
110  }
111  }
112  }
113 
114  // Add non-background components to the mask
115  AimsData<int16_t> brainMask (dx, dy, dz);
116  brainMask.volume()->header() = image.volume()->header();
117  brainMask = 0;
118 
119  for (int icomp = 0; icomp < component.size(); icomp++){
120  std::list<AimsBucketItem<Void> > CC = component[icomp];
121  if ( (icomp != ifond) && (CC.size() >= 0.001 * max_taille) ) { // filtre petites composantes
122  std::list<AimsBucketItem<Void> >::iterator itel, itele = CC .end();
123  for (itel = CC.begin(); itel != itele; ++itel){ //parcours des buckets
124  AimsBucketItem<Void> pixel = *itel;
125  brainMask(pixel.location()) = 1;
126  }
127  }
128  }
129 
130  return brainMask;
131  }
132 
133 } // namespace obso_temp
134 
135 #endif
carto::VolumeRef< U > bin(const carto::VolumeRef< T > &image)
AimsData< int16_t > getBrainMask(const AimsData< T > &image, byte channel=GreenChannel)
Definition: utility_d.h:53