Orfeo Toolbox  3.16
itkSimilarityIndexImageFilter.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkSimilarityIndexImageFilter.txx,v $
5  Language: C++
6  Date: $Date: 2008-10-17 20:49:56 $
7  Version: $Revision: 1.10 $
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 #ifndef __itkSimilarityIndexImageFilter_txx
18 #define __itkSimilarityIndexImageFilter_txx
20 
21 #include "itkImageRegionIterator.h"
23 #include "itkNumericTraits.h"
24 #include "itkProgressReporter.h"
25 
26 namespace itk {
27 
28 
29 template<class TInputImage1, class TInputImage2>
31 ::SimilarityIndexImageFilter(): m_CountOfImage1(1), m_CountOfImage2(1), m_CountOfIntersection(1)
32 {
33 
34  // this filter requires two input images
35  this->SetNumberOfRequiredInputs( 2 );
36 
37  m_SimilarityIndex = NumericTraits<RealType>::Zero;
38 }
39 
40 
41 template<class TInputImage1, class TInputImage2>
42 void
44 ::SetInput2( const TInputImage2 * image )
45 {
46  this->SetNthInput(1, const_cast<TInputImage2 *>( image ) );
47 }
48 
49 
50 template<class TInputImage1, class TInputImage2>
52 ::InputImage2Type *
55 {
56  return static_cast< const TInputImage2 * >
57  (this->ProcessObject::GetInput(1));
58 }
59 
60 template<class TInputImage1, class TInputImage2>
61 void
64 {
65  Superclass::GenerateInputRequestedRegion();
66 
67  // this filter requires:
68  // - the largeset possible region of the first image
69  // - the corresponding region of the second image
70  if ( this->GetInput1() )
71  {
72  InputImage1Pointer image1 =
73  const_cast< InputImage1Type * >( this->GetInput1() );
74  image1->SetRequestedRegionToLargestPossibleRegion();
75 
76  if ( this->GetInput2() )
77  {
78  InputImage2Pointer image2 =
79  const_cast< InputImage2Type * >( this->GetInput2() );
80  image2->SetRequestedRegion(
81  this->GetInput1()->GetRequestedRegion() );
82  }
83 
84  }
85 }
86 
87 
88 template<class TInputImage1, class TInputImage2>
89 void
92 {
93  Superclass::EnlargeOutputRequestedRegion(data);
95 }
96 
97 
98 template<class TInputImage1, class TInputImage2>
99 void
102 {
103  // Pass the first input through as the output
104  InputImage1Pointer image =
105  const_cast< TInputImage1 * >( this->GetInput1() );
106  this->GraftOutput( image );
107 }
108 
109 
110 template<class TInputImage1, class TInputImage2>
111 void
114 {
115  int numberOfThreads = this->GetNumberOfThreads();
116 
117  // Resize the thread temporaries
118  m_CountOfImage1.SetSize(numberOfThreads);
119  m_CountOfImage2.SetSize(numberOfThreads);
120  m_CountOfIntersection.SetSize(numberOfThreads);
121 
122  // Initialize the temporaries
123  m_CountOfImage1.Fill(NumericTraits<unsigned long>::Zero);
124  m_CountOfImage2.Fill(NumericTraits<unsigned long>::Zero);
125  m_CountOfIntersection.Fill(NumericTraits<unsigned long>::Zero);
126 }
127 
128 
129 template<class TInputImage1, class TInputImage2>
130 void
133 {
134  int i;
135  unsigned long countImage1, countImage2, countIntersect;
136 
137  int numberOfThreads = this->GetNumberOfThreads();
138 
139  countImage1 = 0;
140  countImage2 = 0;
141  countIntersect = 0;
142 
143  // Accumulate counts over all threads
144  for( i = 0; i < numberOfThreads; i++)
145  {
146  countImage1 += m_CountOfImage1[i];
147  countImage2 += m_CountOfImage2[i];
148  countIntersect += m_CountOfIntersection[i];
149  }
150 
151  // compute overlap
152  if ( !countImage1 && !countImage2 )
153  {
154  m_SimilarityIndex = NumericTraits<RealType>::Zero;
155  return;
156  }
157 
158  m_SimilarityIndex = 2.0 * static_cast<RealType>( countIntersect ) /
159  ( static_cast<RealType>( countImage1 ) + static_cast<RealType>( countImage2 ) );
160 
161 }
162 
163 template<class TInputImage1, class TInputImage2>
164 void
166 ::ThreadedGenerateData(const RegionType& outputRegionForThread,
167  int threadId)
168 {
169 
170  ImageRegionConstIterator<TInputImage1> it1 (this->GetInput1(), outputRegionForThread);
171  ImageRegionConstIterator<TInputImage2> it2 (this->GetInput2(), outputRegionForThread);
172 
173  // support progress methods/callbacks
174  ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
175 
176  // do the work
177  while (!it1.IsAtEnd())
178  {
179 
180  bool nonzero = false;
181  if( it1.Get() != NumericTraits<InputImage1PixelType>::Zero )
182  {
183  m_CountOfImage1[threadId]++;
184  nonzero = true;
185  }
186  if( it2.Get() != NumericTraits<InputImage2PixelType>::Zero )
187  {
188  m_CountOfImage2[threadId]++;
189  if ( nonzero )
190  {
191  m_CountOfIntersection[threadId]++;
192  }
193  }
194  ++it1;
195  ++it2;
196 
197  progress.CompletedPixel();
198  }
199 }
200 
201 
202 template<class TInputImage1, class TInputImage2>
203 void
205 ::PrintSelf(std::ostream& os, Indent indent) const
206 {
207  Superclass::PrintSelf(os,indent);
208 
209  os << indent << "SimilarityIndex: " << m_SimilarityIndex << std::endl;
210 }
211 
212 
213 }// end namespace itk
214 #endif

Generated at Sun Feb 3 2013 00:07:09 for Orfeo Toolbox with doxygen 1.8.1.1