aimsalgo  5.1.2
Neuroimaging image processing
primalSketch2graph.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 #ifndef AIMS_PRIMALSKETCH_PRIMALSKETCH2GRAPH_H
35 #define AIMS_PRIMALSKETCH_PRIMALSKETCH2GRAPH_H
36 
43 #include <aims/graph/graphmanip.h>
44 #include <aims/resampling/motion.h>
45 #include <graph/graph/graph.h>
46 
47 
48 #include <set>
49 #include <map>
50 #include <list>
51 #include <utility>
52 
53 namespace aims
54 {
55 
56  struct ltstr_blob // ranking criteria for blobs
57  {
58  bool operator()(const std::pair<int,float> p1,
59  const std::pair<int,float> p2) const
60  {
61  if (p1.second>p2.second)
62  return true;
63  else if (p1.second == p2.second)
64  {
65  if (p1.first < p2.first)
66  return true;
67  else
68  return false;
69  }
70  else
71  return false;
72  }
73  };
74 
75  // class that translates a primalsketch into a graph
76 
77  template<typename Geom, typename Text> class Primalsketch2graph
78  {
79  protected:
80 
81  typedef typename SiteType<Geom>::type Site;
82  typedef typename TexType<Text>::type Val;
83 
87 
88  // REM : vertices are scale-space blobs
89  // edges are DIRECTED and represents bifurcation from low scale to high scale
90  // AddEdge(lowScale, highScale, "bifurcation")
91  // As a result, one bifurcation can be represented by several edges.
92 
93  std::map<int, int> sortedLabels; // map [blob label]->[node ranking] generated by SortBlobs();
94 
95  public:
96 
97  Primalsketch2graph(PrimalSketch<Geom,Text> *primalSketch) : _primalSketch(primalSketch), _motion( 0 )
98  {
99  std::cout << "construc : " << primalSketch->BifurcationList().size()
100  << std::endl;
101  _graph = new Graph("PrimalSketchArg");
102  }
104  {
105  delete _motion;
106  }
107  void setMotion( const Motion & m )
108  {
109  if( _motion )
110  delete _motion;
111  _motion = new Motion( m );
112  }
113  void DoIt();
114 
115  void SortBlobs(); //generate a sorted list of blob labels depending on chosen criterion
116  // only max intensity criterion so far, but user's choice later
117 
118  Graph *GetGraph(){return _graph;}
119 
120  };
121 
122  //--------------------------------------------------------------
123  // IMPLEMENTATION
124  //--------------------------------------------------------------
125 
126  template<typename Geom, typename Text>
128  {
129  std::list<ScaleSpaceBlob<Site>*> blobList=_primalSketch->BlobSet();
130  std::set<std::pair<int, float>,ltstr_blob > labelSet;
131  typename std::list<ScaleSpaceBlob<Site>*>::iterator blobIt;
132  std::set<std::pair<int, float>,ltstr_blob >::iterator labelIt;
133  int label=0;
134 
135  std::cout << "Creating blob labels by decreasing maximum intensity" << std::endl;
136  for (blobIt=blobList.begin(); blobIt!=blobList.end(); ++blobIt)
137  {
138  labelSet.insert(std::pair<int,float>((*blobIt)->Label(), (*blobIt)->GetMeasurements().t));
139  }
140  for (labelIt=labelSet.begin(); labelIt!=labelSet.end(); ++labelIt)
141  {
142  label++;
143  sortedLabels[(*labelIt).first]=label;
144  }
145  }
146 
147 
148  template<typename Geom, typename Text>
150  std::list<Bifurcation<Site>*> bifList = _primalSketch->BifurcationList();
151  std::list<ScaleSpaceBlob<Site>*> blobList = _primalSketch->BlobSet();
152 
153  typename std::list<ScaleSpaceBlob<Site>*>::iterator blobIt;
154  typename std::list<Bifurcation<Site> *>::iterator bifIt;
155 
156  std::map<int, Vertex *> vertMap;
157 
158  std::list<ScaleSpaceBlob<Site>*> top, bottom;
159  typename std::list<ScaleSpaceBlob<Site>*>::iterator topIt, bottomIt;
160 
161  std::string typeAtt;
162  Vertex *vert;
163  std::string lab;
164  char conv[10];
165  std::string nameBase = "*";
166  int rank;
167  SortBlobs();
168  _graph->setProperty( "filename_base", "*");
169  _graph->setProperty( "subject", _primalSketch->Subject() );
170 
171 
172  std::string textType;
173  if (_primalSketch->Type()==IMAGE)
174  textType = "image";
175  else if (_primalSketch->Type()==SURFACE)
176  textType = "surface";
177  else
178  textType = "unknown";
179  _graph->setProperty( "type", textType );
180 
181 
182  blobIt = blobList.begin();
183 // std::vector<float> tab;
184  for ( ; blobIt != blobList.end() ; ++blobIt ) {
185 
186  vert =_graph->addVertex( "ssblob" );
187  rank = sortedLabels[(*blobIt)->Label()];
188  sprintf( conv, "%4i", rank );
189  lab = conv;
190  vert->setProperty( "label", 0 );
191  vert->setProperty( "name", rank+10000 );
192  vert->setProperty( "rank", rank );
193  vert->setProperty( "index",(*blobIt)->Label() );
194  vert->setProperty( "tmin",(*blobIt)->ScaleMin() );
195  vert->setProperty( "tmax",(*blobIt)->ScaleMax() );
196  vert->setProperty( "trep", (*blobIt)->ScaleRep() );
197  vert->setProperty( "lifeTime", (*blobIt)->LifeTime() );
198  vert->setProperty( "maxIntensity",(*blobIt)->GetMeasurements().maxIntensity );
199  vert->setProperty( "meanIntensity",(*blobIt)->GetMeasurements().meanIntensity );
200  vert->setProperty( "maxContrast",(*blobIt)->GetMeasurements().maxContrast );
201  vert->setProperty( "meanContrast",(*blobIt)->GetMeasurements().meanContrast );
202  vert->setProperty( "area",(*blobIt)->GetMeasurements().area );
203  vert->setProperty( "tValue",(*blobIt)->GetMeasurements().tValue );
204  vert->setProperty( "t",(*blobIt)->GetMeasurements().t );
205  std::cout << (*blobIt)->GetMeasurements().tValue << ";";
206 // moy+=(*blobIt)->GetMeasurements().tValue;
207 // tab.push_back((*blobIt)->GetMeasurements().tValue);
208  GreyLevelBlobTools<Site> blobTools( (*blobIt)->GlBlobRep() );
209 
210  float x1, x2, y1, y2, z1, z2, gx, gy, gz;
211  std::vector<float> vectF;
212  std::vector<float> triplet(3), ref( 3 );
213 
214  vectF = blobTools.Boundingbox();
215  x1 = vectF[0]; y1 = vectF[1]; z1 = vectF[2];
216  x2 = vectF[3]; y2 = vectF[4]; z2 = vectF[5];
217  gx = ( x1 + x2 ) / 2.0;
218  gy = ( y1 + y2 ) / 2.0;
219  gz = ( z1 + z2 ) / 2.0;
220  triplet[0] = gx;
221  triplet[1] = gy;
222  triplet[2] = gz;
223  triplet = blobTools.Barycenter();
224  vert->setProperty("gravity_center", triplet);
225 
226  if( _motion ) {
227  Point3df tal
228  = _motion->transform( Point3df( triplet[0], triplet[1], triplet[2] ) );
229  ref[0] = tal[0];
230  ref[1] = tal[1];
231  ref[2] = tal[2];
232  vert->setProperty( "refgravity_center", ref );
233  }
234 
235  triplet[0] = x1;
236  triplet[1] = y1;
237  triplet[2] = z1;
238  vert->setProperty( "boundingbox_min", triplet );
239  triplet[0] = x2;
240  triplet[1] = y2;
241  triplet[2] = z2;
242  vert->setProperty( "boundingbox_max", triplet );
243 
244  vertMap[(*blobIt)->Label()] = vert;
245 
246  }
247 
248 
249  bifIt = bifList.begin();
250 
251  // Adding meshes or buckets
252  AddBlobsToPSGraph(_primalSketch, _graph);
253  }
254 
255 }
256 
257 #endif
iterator begin()
class that provide some tools for blob management
std::vector< float > Barycenter()
std::vector< float > Boundingbox()
std::list< Bifurcation< Site > * > BifurcationList()
Definition: primalSketch.h:110
SiteType< Geom >::type Site
void setMotion(const Motion &m)
TexType< Text >::type Val
Primalsketch2graph(PrimalSketch< Geom, Text > *primalSketch)
PrimalSketch< Geom, Text > * _primalSketch
std::map< int, int > sortedLabels
T * iterator
@ SURFACE
Definition: primalSketch.h:54
@ IMAGE
Definition: primalSketch.h:55
void AddBlobsToPSGraph(PrimalSketch< Geom, Text > *sketch, Graph *graph)
reference_wrapper< T > ref(T &ref)
bool operator()(const std::pair< int, float > p1, const std::pair< int, float > p2) const