Orfeo Toolbox  3.16
itkBinomialBlurImageFilter.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkBinomialBlurImageFilter.txx,v $
5  Language: C++
6  Date: $Date: 2008-10-09 15:31:37 $
7  Version: $Revision: 1.26 $
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 __itkBinomialBlurImageFilter_txx
18 #define __itkBinomialBlurImageFilter_txx
19 
20 #include "vnl/vnl_vector_fixed.h"
21 #include "itkProgressReporter.h"
22 #include "itkSize.h"
23 #include "itkImageRegion.h"
24 #include "itkImageRegionIterator.h"
28 
29 namespace itk
30 {
31 
32 template< class TInputImage, class TOutputImage >
35 {
36  itkDebugMacro(<< "BinomialBlurImageFilter::BinomialBlurImageFilter() called");
37 
38  // The default is to just do one repetition
39  m_Repetitions = 1;
40 }
41 
42 template< class TInputImage, class TOutputImage >
43 void
46 {
47  itkDebugMacro(<< "BinomialBlurImageFilter::GenerateInputRequestedRegion() called");
48 
49  Superclass::GenerateInputRequestedRegion();
50 
51  InputImagePointer inputPtr =
52  const_cast< TInputImage * >( this->GetInput(0));
53  OutputImagePointer outputPtr = this->GetOutput(0);
54 
55  if( !inputPtr || !outputPtr )
56  {
57  return;
58  }
59 
60  typename TOutputImage::RegionType outputRegion;
61  typename TInputImage::RegionType inputRegion;
62  typename TInputImage::RegionType::SizeType inputSize;
63  typename TInputImage::RegionType::IndexType inputIndex;
64 
65  outputRegion = outputPtr->GetRequestedRegion();
66 
67  // This filter needs a m_Repetitions pixel border about the output
68  // (clamped of course at the true boundaries of the input image)
69  inputRegion = outputRegion;
70 
71  inputSize = inputRegion.GetSize();
72  inputIndex = inputRegion.GetIndex();
73 
74  typename TInputImage::RegionType::IndexType inputLargestPossibleRegionIndex
75  = inputPtr->GetLargestPossibleRegion().GetIndex();
76  typename TInputImage::RegionType::SizeType inputLargestPossibleRegionSize
77  = inputPtr->GetLargestPossibleRegion().GetSize();
78 
79  for (unsigned int i=0; i < inputPtr->GetImageDimension(); ++i)
80  {
81  inputIndex[i] -= m_Repetitions;
82  if (inputIndex[i] < inputLargestPossibleRegionIndex[i])
83  {
84  inputIndex[i] = inputLargestPossibleRegionIndex[i];
85  }
86 
87  inputSize[i] += m_Repetitions;
88  if (inputSize[i] > inputLargestPossibleRegionSize[i])
89  {
90  inputSize[i] = inputLargestPossibleRegionSize[i];
91  }
92  }
93 
94  inputRegion.SetIndex( inputIndex );
95  inputRegion.SetSize( inputSize );
96 
97  inputPtr->SetRequestedRegion( inputRegion );
98 }
99 
100 
101 template< class TInputImage, class TOutputImage >
102 void
105 {
106  itkDebugMacro(<< "BinomialBlurImageFilter::GenerateData() called");
107 
108  // Get the input and output pointers
109  InputImageConstPointer inputPtr = this->GetInput(0);
110  OutputImagePointer outputPtr = this->GetOutput(0);
111 
112  // Allocate the output
113  outputPtr->SetBufferedRegion( outputPtr->GetRequestedRegion() );
114  outputPtr->Allocate();
115 
116  // Create a temporary image used while processing the image
117  // Processing with doubles eliminates possible rounding artifacts which may
118  // accumulate over repeated integer division
119  typedef Image<double, NDimensions> TTempImage;
120  typename TTempImage::Pointer tempPtr = TTempImage::New();
121 
122  typename TTempImage::RegionType tempRegion;
123  tempRegion = inputPtr->GetRequestedRegion();
124 
125  tempPtr->SetLargestPossibleRegion( tempRegion );
126  tempPtr->SetBufferedRegion( tempRegion );
127  tempPtr->SetRequestedRegion( tempRegion );
128  tempPtr->Allocate();
129 
130  // How big is the input image?
131  typename TInputImage::SizeType size=inputPtr->GetRequestedRegion().GetSize();
132  typename TInputImage::IndexType startIndex=inputPtr->GetRequestedRegion().GetIndex();
133 
134  // Iterator Typedefs for this routine
135  typedef ImageRegionIterator<TTempImage> TempIterator;
136  typedef ImageRegionReverseIterator<TTempImage> TempReverseIterator;
137  typedef ImageRegionConstIterator<TInputImage> InputIterator;
138  typedef ImageRegionIterator<TOutputImage> OutputIterator;
139 
140  // Create a progress reporter
141  ProgressReporter progress(this, 0, (outputPtr->GetRequestedRegion().GetNumberOfPixels()) * m_Repetitions * 2 * NDimensions);
142 
143  // Copy the input image to the temporary image
144  TempIterator tempIt = TempIterator(tempPtr,
145  tempPtr->GetRequestedRegion());
146  InputIterator inputIt = InputIterator(inputPtr, inputPtr->GetRequestedRegion());
147 
148  for ( inputIt.GoToBegin(), tempIt.GoToBegin(); !tempIt.IsAtEnd();++tempIt, ++inputIt)
149  {
150  tempIt.Set( static_cast<double>(inputIt.Get()) );
151  }
152 
153  // Define a few indices that will be used to translate from an input pixel
154  // to an output pixel
155  typename TTempImage::IndexType index;
156  typename TTempImage::IndexType indexShift;
157 
158  // How many times has the algorithm executed? (for debug)
159  int num_reps = 0;
160 
161  // Temporary pixel storage
162  double pixelA, pixelB;
163 
164  // walk the output image forwards and compute blur
165  for (unsigned int rep = 0; rep < m_Repetitions; rep++)
166  {
167  num_reps++;
168 
169  itkDebugMacro(<< "Repetition # " << rep);
170 
171  // blur each dimension
172  for (unsigned int dim = 0; dim < NDimensions; dim ++)
173  {
174  TempIterator tempItDir = TempIterator(tempPtr,
175  tempPtr->GetRequestedRegion());
176  tempItDir.GoToBegin();
177  while( !tempItDir.IsAtEnd() )
178  {
179  // determine the index of the output pixel
180  index = tempItDir.GetIndex();
181 
182  if ( index[dim] <
183  ( startIndex[dim] + static_cast<long>(size[dim]) - 1))
184  {
185  // Figure out the location of the "neighbor" pixel
186  for (unsigned int i = 0; i < NDimensions; i++)
187  {
188  if ( i == dim )
189  {
190  indexShift.m_Index[i] = index.m_Index[i] + 1;
191  }
192  else
193  {
194  indexShift.m_Index[i] = index.m_Index[i];
195  }
196  }
197 
198  // Average the pixel of interest and shifted pixel
199  pixelA = tempPtr->GetPixel(index);
200  pixelB = tempPtr->GetPixel(indexShift);
201 
202  pixelA += pixelB;
203  pixelA = pixelA / 2.0;
204 
205  tempPtr->SetPixel(index, pixelA);
206  progress.CompletedPixel();
207  }
208 
209  ++tempItDir;
210 
211  } // end walk the image forwards
212 
213  itkDebugMacro(<< "End processing forward dimension " << dim);
214 
215  //----------------------Reverse pass----------------------
216  TempReverseIterator tempReverseIt
217  = TempReverseIterator(tempPtr, tempPtr->GetRequestedRegion());
218 
219  tempReverseIt.GoToBegin();
220 
221  while ( !tempReverseIt.IsAtEnd() )
222  {
223  // determine the index of the output pixel
224  index = tempReverseIt.GetIndex();
225 
226  if (index[dim] > startIndex[dim])
227  {
228  // Figure out the location of the "neighbor" pixel
229  for (unsigned int i = 0; i < NDimensions; i++)
230  {
231  if ( i == dim )
232  {
233  indexShift.m_Index[i] = index.m_Index[i] - 1;
234  }
235  else
236  {
237  indexShift.m_Index[i] = index.m_Index[i];
238  }
239  }
240 
241  // Average the pixel of interest and shifted pixel
242  pixelA = tempPtr->GetPixel(index);
243  pixelB = tempPtr->GetPixel(indexShift);
244 
245  pixelA += pixelB;
246  pixelA = pixelA / 2;
247 
248  tempPtr->SetPixel(index, pixelA);
249  progress.CompletedPixel();
250  }
251 
252  ++tempReverseIt;
253 
254  } // end walk the image backwards
255 
256  itkDebugMacro(<< "End processing reverse dimension " << dim);
257  } // end dimension loop
258 
259  } // end number of repetitions loop
260 
261  // Now, copy the temporary image to the output image. Note that the temp
262  // buffer iterator walks a region defined by the output
263  typedef ImageRegionIterator<TOutputImage> OutputIterator;
264 
265  OutputIterator outIt = OutputIterator(outputPtr,
266  outputPtr->GetRequestedRegion());
267  TempIterator tempIt2 = TempIterator(tempPtr,
268  outputPtr->GetRequestedRegion());
269 
270  for ( outIt.GoToBegin(), tempIt2.GoToBegin(); !outIt.IsAtEnd();
271  ++outIt, ++tempIt2)
272  {
273  outIt.Set( static_cast<PixelType>(tempIt2.Get()) );
274  }
275 
276  itkDebugMacro(<< "Binomial blur filter executed " << num_reps << " times");
277 }
278 
279 template< class TInputImage, class TOutputImage >
280 void
282 ::PrintSelf(std::ostream& os, Indent indent) const
283 {
284  Superclass::PrintSelf(os,indent);
285 
286  os << indent << "Number of repetitions: " << m_Repetitions << std::endl;
287 
288 }
289 
290 } // end namespace
291 
292 #endif

Generated at Sat Feb 2 2013 23:29:09 for Orfeo Toolbox with doxygen 1.8.1.1