bioprocessing  5.1.2
extremafinder.h
Go to the documentation of this file.
1 #ifndef BIOPROCESSING_UTILITY_EXTREMAFINDER_H
2 #define BIOPROCESSING_UTILITY_EXTREMAFINDER_H
3 
4 //============================================================================
5 // EXTREMA FINDER
6 //============================================================================
7 
8 //--- aims -------------------------------------------------------------------
9 #include <aims/connectivity/structuring_element.h> // StructuringElement
10 #include <aims/data/cartodatavolume.h> // AimsData
11 #include <aims/bucket/bucketMap.h> // BucketMap
12 #include <aims/utility/progress.h>
13 //--- cartodata --------------------------------------------------------------
14 #include <cartodata/volume/volume.h> // VolumeRef
15 //--- cartobase --------------------------------------------------------------
16 #include <cartobase/config/verbose.h> // verbose
17 #include <cartobase/smart/rcptr.h> // rc_ptr
18 //--- std --------------------------------------------------------------------
19 #include <iostream>
20 //----------------------------------------------------------------------------
21 
22 namespace bio {
23 
25  {
26  public:
28  _strel( new aims::strel::Sphere(1.0) ),
29  _connex( new aims::strel::Connectivity26XYZ() ),
30  _modemin(true),
31  _modestrict(false),
32  _value(1),
33  _modecc(false)
34  {}
35  virtual ~ExtremaFinder() {}
36 
37  //--- parameters
38  void setStructuringElement( const aims::StructuringElement & se )
39  {
40  _strel.reset( se.clone() );
41  }
42  void setModeMin() { _modemin = true; _modestrict = false; }
43  void setModeMax() { _modemin = false; _modestrict = false; }
44  void setModeStrictMin() { _modemin = true; _modestrict = true; }
45  void setModeStrictMax() { _modemin = false; _modestrict = true; }
46  void setModeValueConst( double value ) { _value = value; _modecc = false; }
47  void setModeValueConnectComp( const aims::strel::Connectivity & c )
48  {
49  _connex.reset( c.clone() );
50  _modecc = true;
51  }
52 
53  //--- execute
54  template <typename IN, typename OUT>
55  void computeExtrema( carto::VolumeRef<IN> in,
56  carto::VolumeRef<OUT> out );
57  template <typename IN, typename OUT, typename M>
58  void computeExtrema( carto::VolumeRef<IN> in,
59  carto::VolumeRef<OUT> out,
60  carto::VolumeRef<M> mask );
61  template <typename IN, typename OUT>
62  void computeExtrema( carto::VolumeRef<IN> in,
63  aims::BucketMap<OUT> & out );
64  template <typename IN, typename OUT, typename M>
65  void computeExtrema( carto::VolumeRef<IN> in,
66  aims::BucketMap<OUT> & out,
67  carto::VolumeRef<M> mask );
68 
69  //--- utility methods
70  template <typename IN, typename OUT>
71  carto::VolumeRef<OUT> computeExtremaVolume( carto::VolumeRef<IN> in )
72  {
73  carto::VolumeRef<OUT> out( in.getSizeX(),
74  in.getSizeY(),
75  in.getSizeZ(),
76  in.getSizeT() );
77  out->copyHeaderFrom( in.header() );
78  out.fill(0);
79  computeExtrema( in, out );
80  return out;
81  }
82 
83  template <typename IN, typename OUT, typename M >
84  carto::VolumeRef<OUT> computeExtremaVolume(
85  carto::VolumeRef<IN> in,
86  carto::VolumeRef<M> mask )
87  {
88  carto::VolumeRef<OUT> out( in.getSizeX(),
89  in.getSizeY(),
90  in.getSizeZ(),
91  in.getSizeT() );
92  out->copyHeaderFrom( in.header() );
93  out.fill(0);
94  computeExtrema( in, out, mask );
95  return out;
96  }
97 
98  template <typename IN, typename OUT>
99  aims::BucketMap<OUT> computeExtremaBucket( carto::VolumeRef<IN> in )
100  {
101  aims::BucketMap<OUT> out;
102  std::vector<float> vs(4,1.);
103  if( in.header().hasProperty( "voxel_size" ) )
104  in.header().getProperty( "voxel_size", vs );
105  out.setSizeXYZT( vs[0], vs[1], vs[2], vs[3] );
106  computeExtrema( in, out );
107  return out;
108  }
109 
110  template <typename IN, typename OUT, typename M>
111  aims::BucketMap<OUT> computeExtremaBucket(
112  carto::VolumeRef<IN> in,
113  carto::VolumeRef<M> mask
114  = carto::VolumeRef<M>( (carto::Volume<M>*)0 ) )
115  {
116  aims::BucketMap<OUT> out;
117  std::vector<float> vs(4,1.);
118  if( in.header().hasProperty( "voxel_size" ) )
119  in.header().getProperty( "voxel_size", vs );
120  out.setSizeXYZT( vs[0], vs[1], vs[2], vs[3] );
121  computeExtrema( in, out, mask );
122  return out;
123  }
124 
125  private:
126  template <typename IN>
127  bool isExtremum( carto::VolumeRef<IN> in, int x, int y, int z, int t );
128  carto::rc_ptr<aims::StructuringElement> _strel;
129  carto::rc_ptr<aims::strel::Connectivity> _connex;
130  bool _modemin;
131  bool _modestrict;
132  bool _modecc;
133  double _value;
134  };
135 
136  template <typename IN>
137  bool ExtremaFinder::isExtremum( carto::VolumeRef<IN> in,
138  int x, int y, int z, int t )
139  {
140  aims::StructuringElement::const_iterator p;
141 
142  for( p = _strel->begin(); p != _strel->end(); ++p )
143  {
144  if( _modemin && _modestrict )
145  {
146  if( in( x, y, z, t ) >= in( x + (*p)[0], y + (*p)[1], z + (*p)[2], t ) )
147  return false;
148  }
149  else if( _modemin && !_modestrict )
150  {
151  if( in( x, y, z, t ) > in( x + (*p)[0], y + (*p)[1], z + (*p)[2], t ) )
152  return false;
153  }
154  else if( !_modemin && _modestrict )
155  {
156  if( in( x, y, z, t ) <= in( x + (*p)[0], y + (*p)[1], z + (*p)[2], t ) )
157  return false;
158  }
159  else if( !_modemin && !_modestrict )
160  {
161  if( in( x, y, z, t ) < in( x + (*p)[0], y + (*p)[1], z + (*p)[2], t ) )
162  return false;
163  }
164  }
165 
166  return true;
167  }
168 
169  template <typename IN, typename OUT>
170  void ExtremaFinder::computeExtrema( carto::VolumeRef<IN> in,
171  carto::VolumeRef<OUT> out )
172  {
173  int x, y, z, t;
174  for( t=0; t<in.getSizeT(); ++t )
175  for( z=0; z<in.getSizeZ(); ++z )
176  for( y=0; y<in.getSizeY(); ++y )
177  for( x=0; x<in.getSizeX(); ++x )
178  if( isExtremum( in, x, y, z, t ) )
179  out( x, y, z, t ) = _value;
180 
181  if( _modecc )
182  std::cout << "ExtremaFinder:: connected components mode not implemented yet." << std::endl;
183  }
184 
185  template <typename IN, typename OUT, typename M>
186  void ExtremaFinder::computeExtrema( carto::VolumeRef<IN> in,
187  carto::VolumeRef<OUT> out,
188  carto::VolumeRef<M> mask )
189  {
190  int x, y, z, t;
191  for( t=0; t<in.getSizeT(); ++t )
192  for( z=0; z<in.getSizeZ(); ++z )
193  for( y=0; y<in.getSizeY(); ++y )
194  for( x=0; x<in.getSizeX(); ++x )
195  if( mask( x, y, z, t ) )
196  if( isExtremum( in, x, y, z, t ) )
197  out( x, y, z, t ) = _value;
198 
199  if( _modecc )
200  std::cout << "ExtremaFinder:: connected components mode not implemented yet." << std::endl;
201  }
202 
203  template <typename IN, typename OUT>
204  void ExtremaFinder::computeExtrema( carto::VolumeRef<IN> in,
205  aims::BucketMap<OUT> & out )
206  {
207  int x, y, z, t;
208 
209  double lmax = in.getSizeT();
210  lmax *= in.getSizeZ();
211  lmax *= in.getSizeY();
212  double l=0;
213 
214  for( t=0; t<in.getSizeT(); ++t )
215  for( z=0; z<in.getSizeZ(); ++z )
216  for( y=0; y<in.getSizeY(); ++y )
217  for( x=0; x<in.getSizeX(); ++x )
218  if( isExtremum( in, x, y, z, t ) )
219  out.insert( Point3d(x,y,z), (OUT)_value );
220 
221  if( _modecc )
222  std::cout << "ExtremaFinder:: connected components mode not implemented yet." << std::endl;
223  }
224 
225  template <typename IN, typename OUT, typename M>
226  void ExtremaFinder::computeExtrema( carto::VolumeRef<IN> in,
227  aims::BucketMap<OUT> & out,
228  carto::VolumeRef<M> mask )
229  {
230  int x, y, z, t;
231 
232  double lmax = in.getSizeT();
233  lmax *= in.getSizeZ();
234  lmax *= in.getSizeY();
235  double l=0;
236 
237  for( t=0; t<in.getSizeT(); ++t )
238  for( z=0; z<in.getSizeZ(); ++z )
239  for( y=0; y<in.getSizeY(); ++y )
240  for( x=0; x<in.getSizeX(); ++x )
241  if( mask( x, y, z, t ) )
242  if( isExtremum( in, x, y, z, t ) )
243  out.insert( Point3d(x,y,z), (OUT)_value );
244 
245  if( _modecc )
246  std::cout << "ExtremaFinder:: connected components mode not implemented yet." << std::endl;
247  }
248 
249 } // namespace bio
250 
251 #endif
void computeExtrema(carto::VolumeRef< IN > in, carto::VolumeRef< OUT > out)
aims::BucketMap< OUT > computeExtremaBucket(carto::VolumeRef< IN > in)
Definition: extremafinder.h:99
aims::BucketMap< OUT > computeExtremaBucket(carto::VolumeRef< IN > in, carto::VolumeRef< M > mask=carto::VolumeRef< M >((carto::Volume< M > *) 0))
carto::VolumeRef< OUT > computeExtremaVolume(carto::VolumeRef< IN > in)
Definition: extremafinder.h:71
virtual ~ExtremaFinder()
Definition: extremafinder.h:35
void setStructuringElement(const aims::StructuringElement &se)
Definition: extremafinder.h:38
carto::VolumeRef< OUT > computeExtremaVolume(carto::VolumeRef< IN > in, carto::VolumeRef< M > mask)
Definition: extremafinder.h:84
void setModeValueConnectComp(const aims::strel::Connectivity &c)
Definition: extremafinder.h:47
void setModeValueConst(double value)
Definition: extremafinder.h:46