Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
itkConnectomicsNetworkToConnectivityMatrixImageFilter.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #ifndef ITK_ConnectomicsNetworkToConnectivityMatrixImageFilter_CPP
18 #define ITK_ConnectomicsNetworkToConnectivityMatrixImageFilter_CPP
19 
20 #include <itkImageRegionIterator.h>
21 
22 #include <vector>
23 
25  : m_BinaryConnectivity(false)
26  , m_RescaleConnectivity(false)
27  , m_InputNetwork(NULL)
28 {
29 }
30 
31 
33 {
34 }
35 
37 {
38 
39  this->AllocateOutputs();
40 
41  OutputImageType::Pointer output = this->GetOutput();
42 
43  if(m_InputNetwork.IsNull())
44  {
45  MITK_ERROR << "Failed to generate data, no valid network was set.";
46  return;
47  }
48 
49  typedef mitk::ConnectomicsNetwork::NetworkType NetworkType;
50  typedef boost::graph_traits< NetworkType >::vertex_descriptor DescriptorType;
51  typedef boost::graph_traits< NetworkType >::vertex_iterator IteratorType;
52 
53  // prepare connectivity matrix for data
54  std::vector< std::vector< int > > connectivityMatrix;
55  int numberOfVertices = m_InputNetwork->GetNumberOfVertices();
56 
57  connectivityMatrix.resize( numberOfVertices );
58  for( int index(0); index < numberOfVertices; index++ )
59  {
60  connectivityMatrix[ index ].resize( numberOfVertices );
61  }
62 
63  // Create LabelToIndex translation
64  std::map< std::string, DescriptorType > labelToIndexMap;
65 
66  NetworkType boostGraph = *(m_InputNetwork->GetBoostGraph());
67 
68  // translate index to label
69  IteratorType iterator, end;
70  boost::tie(iterator, end) = boost::vertices( boostGraph );
71 
72  for ( ; iterator != end; ++iterator)
73  {
74  labelToIndexMap.insert(
75  std::pair< std::string, DescriptorType >(
76  boostGraph[ *iterator ].label, *iterator )
77  );
78  }
79 
80  std::vector< std::string > indexToLabel;
81 
82  // translate index to label
83  indexToLabel.resize( numberOfVertices );
84 
85  boost::tie(iterator, end) = boost::vertices( boostGraph );
86 
87  for ( ; iterator != end; ++iterator)
88  {
89  indexToLabel[ *iterator ] = boostGraph[ *iterator ].label;
90  }
91 
92  //translate label to position
93  std::vector< std::string > positionToLabelVector;
94  positionToLabelVector = indexToLabel;
95 
96  std::sort ( positionToLabelVector.begin(), positionToLabelVector.end() );
97 
98  for( int outerLoop( 0 ); outerLoop < numberOfVertices; outerLoop++ )
99  {
100  DescriptorType fromVertexDescriptor = labelToIndexMap.find( positionToLabelVector[ outerLoop ] )->second;
101  for( int innerLoop( outerLoop + 1 ); innerLoop < numberOfVertices; innerLoop++ )
102  {
103  DescriptorType toVertexDescriptor = labelToIndexMap.find( positionToLabelVector[ innerLoop ] )->second;
104 
105 
106  int weight( 0 );
107 
108  if( boost::edge(toVertexDescriptor, fromVertexDescriptor, boostGraph ).second )
109  {
110  weight = m_InputNetwork->GetEdge( fromVertexDescriptor , toVertexDescriptor ).weight;;
111  }
112  connectivityMatrix[outerLoop][innerLoop] = weight;
113  connectivityMatrix[innerLoop][outerLoop] = weight;
114  }
115  }
116 
117 
118  OutputImageType::SpacingType spacing;
119  spacing[0] = 1.0;
120  spacing[1] = 1.0;
122  origin[0] = 0.0;
123  origin[1] = 0.0;
124  OutputImageType::IndexType index;
125  index[0] = 0.0;
126  index[1] = 0.0;
127  OutputImageType::SizeType size;
128  size[0] = numberOfVertices;
129  size[1] = numberOfVertices;
130  OutputImageType::RegionType region;
131  region.SetIndex( index );
132  region.SetSize( size );
133 
134  output->SetSpacing( spacing ); // Set the image spacing
135  output->SetOrigin( origin ); // Set the image origin
136  output->SetRegions( region );
137  output->Allocate();
138  output->FillBuffer(0);
139 
140 
141  itk::ImageRegionIterator< OutputImageType > it_connect(output, output->GetLargestPossibleRegion());
142 
143  int counter( 0 );
144 
145  double rescaleFactor( 1.0 );
146  double rescaleMax( 255.0 );
147 
148  if( m_RescaleConnectivity )
149  {
150  rescaleFactor = rescaleMax / m_InputNetwork->GetMaximumWeight();
151  }
152 
153  // Colour pixels according to connectivity
154  if( m_BinaryConnectivity )
155  {
156  // binary connectivity
157  while( !it_connect.IsAtEnd() )
158  {
159  if( connectivityMatrix[ ( counter - counter % numberOfVertices ) / numberOfVertices][ counter % numberOfVertices ] )
160  {
161  it_connect.Set( 1 );
162  }
163  else
164  {
165  it_connect.Set( 0 );
166  }
167  ++it_connect;
168  counter++;
169  }
170  }
171  else
172  {
173  // if desired rescale to the 0-255 range
174  while( !it_connect.IsAtEnd() )
175  {
176  it_connect.Set( ( unsigned short ) rescaleFactor *
177  connectivityMatrix[ ( counter - counter % numberOfVertices ) / numberOfVertices][ counter % numberOfVertices ]
178  );
179  ++it_connect;
180  counter++;
181  }
182  }
183 
184 }
185 
186 #endif
mitk::Point3D PointType
itk::SmartPointer< Self > Pointer
#define MITK_ERROR
Definition: mitkLogMacros.h:24
boost::adjacency_list< boost::vecS, boost::vecS, boost::undirectedS, NetworkNode, NetworkEdge > NetworkType