Orfeo Toolbox  3.16
itkCoreAtomImageToUnaryCorrespondenceMatrixProcess.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkCoreAtomImageToUnaryCorrespondenceMatrixProcess.txx,v $
5  Language: C++
6  Date: $Date: 2009-05-12 20:21:45 $
7  Version: $Revision: 1.7 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 
18 #ifndef __itkCoreAtomImageToUnaryCorrespondenceMatrixProcess_txx
19 #define __itkCoreAtomImageToUnaryCorrespondenceMatrixProcess_txx
20 
22 
23 namespace itk
24 {
25 
29 template< typename TSourceImage >
32 {
33  itkDebugMacro(<< "itkCoreAtomImageToUnaryCorrespondenceMatrixProcess::itkCoreAtomImageToUnaryCorrespondenceMatrixProcess() called");
34 
35  // Setting the output.
37  output = static_cast<typename CoreAtomImageToUnaryCorrespondenceMatrixProcess::CorrespondenceMatrixType*>(this->MakeOutput(0).GetPointer());
39  this->ProcessObject::SetNthOutput(0, output.GetPointer());
40 
41  // Initialize unary metric object.
42  //m_Metric = UnaryMetricType::New();
43 
44  // Initialize flag to off by default.
45  m_OutputPNG = false;
46 }
47 
51 template< typename TSourceImage >
54 ::MakeOutput(unsigned int)
55 {
56  return static_cast<DataObject*>(CorrespondenceMatrixType::New().GetPointer());
57 }
58 
62 template< typename TSourceImage >
66 {
67  if (this->GetNumberOfOutputs() < 1)
68  {
69  return 0;
70  }
71 
72  return static_cast< CorrespondenceMatrixType * >
73  (this->ProcessObject::GetOutput(0));
74 }
75 
79 template< typename TSourceImage >
80 void
82 ::SetInput1(const TSourceImage * image1 )
83 {
84  itkDebugMacro(<< "CoreAtomImageToUnaryCorrespondenceMatrixProcess: Setting first core atom image input")
85 
86  // Process object is not const-correct so the const casting is required.
87  SetNthInput(1, const_cast<TSourceImage *>( image1 ) );
88 }
89 
93 template< typename TSourceImage >
94 void
96 ::SetInput2(const TSourceImage * image2 )
97 {
98  itkDebugMacro(<< "CoreAtomImageToUnaryCorrespondenceMatrixProcess: Setting second core atom image input")
99  // Process object is not const-correct so the const casting is required.
100  SetNthInput(0, const_cast<TSourceImage *>( image2 ) );
101 }
102 
106 template< typename TSourceImage >
107 TSourceImage *
110 {
111  // Process object is not const-correct so the const casting is required.
112  return const_cast<TSourceImage *>(this->GetNthInput(0));
113 }
114 
118 template< typename TSourceImage >
119 TSourceImage *
122 {
123  // Process object is not const-correct so the const casting is required.
124  return const_cast<TSourceImage *>(this->GetNthInput(1));
125 }
126 
130 template< typename TSourceImage >
131 void
134 {
135  itkDebugMacro(<< "itkCoreAtomImageToUnaryCorrespondenceMatrixProcess::GenerateData() called");
136 
137  m_Metric = UnaryMetricType::New();
138 
139  // Pointers to the input core atom images, output matrix object.
140  m_CoreAtomImageA = dynamic_cast<CoreAtomImageType*>(ProcessObject::GetInput(0));
141  m_CoreAtomImageB = dynamic_cast<CoreAtomImageType*>(ProcessObject::GetInput(1));
142  m_CorrespondenceMatrix = dynamic_cast<CorrespondenceMatrixType*>(ProcessObject::GetOutput(0));
143 
144  m_Rows = m_CoreAtomImageA->GetMedialNodeCount();
145  m_Columns = m_CoreAtomImageB->GetMedialNodeCount();
146 
147  if(m_CorrespondenceMatrix->set_size(m_Rows,m_Columns))
148  {
149  itkDebugMacro(<< "m_CorrespondenceMatrix resized successfully");
150  }
151  else
152  {
153  itkDebugMacro(<< "m_CorrespondenceMatrix resize failed");
154  }
155 
156  // Here we iterate through all of the core atoms and get their metric values.
157 
158  // Create iterators that will walk the blox core atom images.
159  typedef itk::ImageRegionIterator<CoreAtomImageType> BloxIterator;
160 
161  BloxIterator bloxItA = BloxIterator(m_CoreAtomImageA,
162  m_CoreAtomImageA->GetRequestedRegion() );
163 
164  BloxIterator bloxItB = BloxIterator(m_CoreAtomImageB,
165  m_CoreAtomImageB->GetRequestedRegion() );
166 
167  int counterA = 0;
168  int counterB = 0;
169  double MetricValue;
170 
171  // Initialize necessary components to write-out a PNG image of correspondance matrix.
172  // The image only gets created is the flag is set.
173  typedef unsigned char CorrespondencePixelType;
174  typedef Image<CorrespondencePixelType, 2> CorrespondenceImageType;
175 
176  CorrespondenceImageType::IndexType pixelIndex;
177  CorrespondenceImageType::SizeType size;
178  size[0] = m_Rows;
179  size[1] = m_Columns;
180 
181  CorrespondenceImageType::RegionType region;
182  CorrespondenceImageType::IndexType index;
183  index.Fill(0);
184  region.SetIndex( index );
185  region.SetSize(size);
186 
187  CorrespondenceImageType::Pointer CorrespondenceImage;
188  CorrespondenceImage = CorrespondenceImageType::New();
189 
190  CorrespondenceImage->SetRegions( region );
191  CorrespondenceImage->Allocate();
192  CorrespondenceImage->FillBuffer(0);
193 
194  typedef ImageFileWriter<CorrespondenceImageType> FileWriterType;
195  FileWriterType::Pointer imageWriter;
196  imageWriter = FileWriterType::New();
197 
199  io = PNGImageIO::New();
200 
201  // Iterate through nodes in m_CoreAtomImageA (rows).
202  for ( bloxItA.GoToBegin(); !bloxItA.IsAtEnd(); ++bloxItA)
203  {
204  MedialNodeType* pPixelA = &bloxItA.Value();
205 
206  if( pPixelA->empty() )
207  {
208  continue;
209  }
210 
211  // Iterate through nodes in m_CoreAtomImageB (columns)
212  for ( bloxItB.GoToBegin(); !bloxItB.IsAtEnd(); ++bloxItB) //iterate through nodes in m_MedialWindowA
213  {
214  MedialNodeType* pPixelB = &bloxItB.Value();
215 
216  if( pPixelB->empty() )
217  {
218  continue;
219  }
220 
221  m_Metric->SetMedialNodes(pPixelA, pPixelB);
222  m_Metric->Initialize();
223  MetricValue = m_Metric->GetResult();
224  //MetricValue = 1;
225 
226  // Use m_CorrespondenceMatrix->put(row, column, value) to set matrix.
227  m_CorrespondenceMatrix->put(counterA, counterB, MetricValue);
228 
229  pixelIndex[0] = counterA;
230  pixelIndex[1] = counterB;
231 
232  if(m_OutputPNG)
233  {
234  CorrespondenceImage->SetPixel(pixelIndex,
235  static_cast<CorrespondencePixelType>(100*m_CorrespondenceMatrix->get(counterA,counterB)) );
236  }
237 
238  counterB++;
239  }
240  counterB = 0;
241  counterA++;
242  }
243 
244  // Write voting PNG image.
245  if(m_OutputPNG)
246  {
247  std::cerr << "WROTE CORRESPONDANCE IMAGE TO CorrespondenceImage1.png" << std::endl;
248  imageWriter->SetInput(CorrespondenceImage);
249  imageWriter->SetFileName("CorrespondenceImage1.png");
250  imageWriter->SetImageIO(io);
251  imageWriter->Write();
252  }
253  itkDebugMacro(<< "Finished CoreAtomImageToUnaryCorrespondenceMatrixProcess\n");
254 }
255 
259 template< typename TSourceImage >
260 void
262 ::PrintSelf(std::ostream& os, Indent indent) const
263 {
264  Superclass::PrintSelf(os,indent);
265  os << indent << "Columns: " << this->GetColumns() << std::endl;
266  os << indent << "Rows: " << this->GetRows() << std::endl;
267 }
268 
269 } // end namespace
270 
271 #endif

Generated at Sat Feb 2 2013 23:33:44 for Orfeo Toolbox with doxygen 1.8.1.1