aimsalgo  5.0.5
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, const std::pair<int,float> p2)
59  {
60  if (p1.second>p2.second)
61  return true;
62  else if (p1.second == p2.second)
63  {
64  if (p1.first < p2.first)
65  return true;
66  else
67  return false;
68  }
69  else
70  return false;
71  }
72  };
73 
74  // class that translates a primalsketch into a graph
75 
76  template<typename Geom, typename Text> class Primalsketch2graph
77  {
78  protected:
79 
80  typedef typename SiteType<Geom>::type Site;
81  typedef typename TexType<Text>::type Val;
82 
86 
87  // REM : vertices are scale-space blobs
88  // edges are DIRECTED and represents bifurcation from low scale to high scale
89  // AddEdge(lowScale, highScale, "bifurcation")
90  // As a result, one bifurcation can be represented by several edges.
91 
92  std::map<int, int> sortedLabels; // map [blob label]->[node ranking] generated by SortBlobs();
93 
94  public:
95 
96  Primalsketch2graph(PrimalSketch<Geom,Text> *primalSketch) : _primalSketch(primalSketch), _motion( 0 )
97  {
98  std::cout << "construc : " << primalSketch->BifurcationList().size()
99  << std::endl;
100  _graph = new Graph("PrimalSketchArg");
101  }
103  {
104  delete _motion;
105  }
106  void setMotion( const Motion & m )
107  {
108  if( _motion )
109  delete _motion;
110  _motion = new Motion( m );
111  }
112  void DoIt();
113 
114  void SortBlobs(); //generate a sorted list of blob labels depending on chosen criterion
115  // only max intensity criterion so far, but user's choice later
116 
117  Graph *GetGraph(){return _graph;}
118 
119  };
120 
121  //--------------------------------------------------------------
122  // IMPLEMENTATION
123  //--------------------------------------------------------------
124 
125  template<typename Geom, typename Text>
127  {
128  std::list<ScaleSpaceBlob<Site>*> blobList=_primalSketch->BlobSet();
129  std::set<std::pair<int, float>,ltstr_blob > labelSet;
130  typename std::list<ScaleSpaceBlob<Site>*>::iterator blobIt;
131  std::set<std::pair<int, float>,ltstr_blob >::iterator labelIt;
132  int label=0;
133 
134  std::cout << "Creating blob labels by decreasing maximum intensity" << std::endl;
135  for (blobIt=blobList.begin(); blobIt!=blobList.end(); ++blobIt)
136  {
137  labelSet.insert(std::pair<int,float>((*blobIt)->Label(), (*blobIt)->GetMeasurements().t));
138  }
139  for (labelIt=labelSet.begin(); labelIt!=labelSet.end(); ++labelIt)
140  {
141  label++;
142  sortedLabels[(*labelIt).first]=label;
143  }
144  }
145 
146 
147  template<typename Geom, typename Text>
149  std::list<Bifurcation<Site>*> bifList = _primalSketch->BifurcationList();
150  std::list<ScaleSpaceBlob<Site>*> blobList = _primalSketch->BlobSet();
151 
152  typename std::list<ScaleSpaceBlob<Site>*>::iterator blobIt;
153  typename std::list<Bifurcation<Site> *>::iterator bifIt;
154 
155  std::map<int, Vertex *> vertMap;
156 
157  std::list<ScaleSpaceBlob<Site>*> top, bottom;
158  typename std::list<ScaleSpaceBlob<Site>*>::iterator topIt, bottomIt;
159 
160  std::string typeAtt;
161  Vertex *vert;
162  std::string lab;
163  char conv[10];
164  std::string nameBase = "*";
165  int rank;
166  SortBlobs();
167  _graph->setProperty( "filename_base", "*");
168  _graph->setProperty( "subject", _primalSketch->Subject() );
169 
170 
171  std::string textType;
172  if (_primalSketch->Type()==IMAGE)
173  textType = "image";
174  else if (_primalSketch->Type()==SURFACE)
175  textType = "surface";
176  else
177  textType = "unknown";
178  _graph->setProperty( "type", textType );
179 
180 
181  blobIt = blobList.begin();
182 // std::vector<float> tab;
183  for ( ; blobIt != blobList.end() ; ++blobIt ) {
184 
185  vert =_graph->addVertex( "ssblob" );
186  rank = sortedLabels[(*blobIt)->Label()];
187  sprintf( conv, "%4i", rank );
188  lab = conv;
189  vert->setProperty( "label", 0 );
190  vert->setProperty( "name", rank+10000 );
191  vert->setProperty( "rank", rank );
192  vert->setProperty( "index",(*blobIt)->Label() );
193  vert->setProperty( "tmin",(*blobIt)->ScaleMin() );
194  vert->setProperty( "tmax",(*blobIt)->ScaleMax() );
195  vert->setProperty( "trep", (*blobIt)->ScaleRep() );
196  vert->setProperty( "lifeTime", (*blobIt)->LifeTime() );
197  vert->setProperty( "maxIntensity",(*blobIt)->GetMeasurements().maxIntensity );
198  vert->setProperty( "meanIntensity",(*blobIt)->GetMeasurements().meanIntensity );
199  vert->setProperty( "maxContrast",(*blobIt)->GetMeasurements().maxContrast );
200  vert->setProperty( "meanContrast",(*blobIt)->GetMeasurements().meanContrast );
201  vert->setProperty( "area",(*blobIt)->GetMeasurements().area );
202  vert->setProperty( "tValue",(*blobIt)->GetMeasurements().tValue );
203  vert->setProperty( "t",(*blobIt)->GetMeasurements().t );
204  std::cout << (*blobIt)->GetMeasurements().tValue << ";";
205 // moy+=(*blobIt)->GetMeasurements().tValue;
206 // tab.push_back((*blobIt)->GetMeasurements().tValue);
207  GreyLevelBlobTools<Site> blobTools( (*blobIt)->GlBlobRep() );
208 
209  float x1, x2, y1, y2, z1, z2, gx, gy, gz;
210  std::vector<float> vectF;
211  std::vector<float> triplet(3), ref( 3 );
212 
213  vectF = blobTools.Boundingbox();
214  x1 = vectF[0]; y1 = vectF[1]; z1 = vectF[2];
215  x2 = vectF[3]; y2 = vectF[4]; z2 = vectF[5];
216  gx = ( x1 + x2 ) / 2.0;
217  gy = ( y1 + y2 ) / 2.0;
218  gz = ( z1 + z2 ) / 2.0;
219  triplet[0] = gx;
220  triplet[1] = gy;
221  triplet[2] = gz;
222  triplet = blobTools.Barycenter();
223  vert->setProperty("gravity_center", triplet);
224 
225  if( _motion ) {
226  Point3df tal
227  = _motion->transform( Point3df( triplet[0], triplet[1], triplet[2] ) );
228  ref[0] = tal[0];
229  ref[1] = tal[1];
230  ref[2] = tal[2];
231  vert->setProperty( "refgravity_center", ref );
232  }
233 
234  triplet[0] = x1;
235  triplet[1] = y1;
236  triplet[2] = z1;
237  vert->setProperty( "boundingbox_min", triplet );
238  triplet[0] = x2;
239  triplet[1] = y2;
240  triplet[2] = z2;
241  vert->setProperty( "boundingbox_max", triplet );
242 
243  vertMap[(*blobIt)->Label()] = vert;
244 
245  }
246 
247 
248  bifIt = bifList.begin();
249 
250  // Adding meshes or buckets
251  AddBlobsToPSGraph(_primalSketch, _graph);
252  }
253 
254 }
255 
256 #endif
std::map< int, int > sortedLabels
std::list< Bifurcation< Site > * > BifurcationList()
Definition: primalSketch.h:110
std::vector< float > Barycenter()
void AddBlobsToPSGraph(PrimalSketch< Geom, Text > *sketch, Graph *graph)
PrimalSketch< Geom, Text > * _primalSketch
SiteType< Geom >::type Site
bool operator()(const std::pair< int, float > p1, const std::pair< int, float > p2)
TexType< Text >::type Val
Primalsketch2graph(PrimalSketch< Geom, Text > *primalSketch)
void setMotion(const Motion &m)
T * iterator
reference_wrapper< T > ref(T &ref)