Orfeo Toolbox  3.16
itkExtractImageFilter.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkExtractImageFilter.txx,v $
5  Language: C++
6  Date: $Date: 2008-11-09 16:51:04 $
7  Version: $Revision: 1.28 $
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 __itkExtractImageFilter_txx
18 #define __itkExtractImageFilter_txx
19 
20 #include "itkExtractImageFilter.h"
21 #include "itkImageRegionIterator.h"
23 #include "itkObjectFactory.h"
25 #include "itkProgressReporter.h"
26 
27 
28 namespace itk
29 {
30 
34 template <class TInputImage, class TOutputImage>
37 {
38 }
39 
40 
44 template <class TInputImage, class TOutputImage>
45 void
47 ::PrintSelf(std::ostream& os, Indent indent) const
48 {
49  Superclass::PrintSelf(os,indent);
50 
51  os << indent << "ExtractionRegion: " << m_ExtractionRegion << std::endl;
52  os << indent << "OutputImageRegion: " << m_OutputImageRegion << std::endl;
53 }
54 
55 
56 template<class TInputImage, class TOutputImage>
57 void
60  const OutputImageRegionType &srcRegion)
61 {
62  ExtractImageFilterRegionCopierType extractImageRegionCopier;
63  extractImageRegionCopier(destRegion, srcRegion, m_ExtractionRegion);
64 }
65 
66 
67 template <class TInputImage, class TOutputImage>
68 void
71 {
72  m_ExtractionRegion = extractRegion;
73 
74  unsigned int nonzeroSizeCount = 0;
75  InputImageSizeType inputSize = extractRegion.GetSize();
76  OutputImageSizeType outputSize;
77  OutputImageIndexType outputIndex;
78 
83  for (unsigned int i = 0; i < InputImageDimension; ++i)
84  {
85  if (inputSize[i])
86  {
87  outputSize[nonzeroSizeCount] = inputSize[i];
88  outputIndex[nonzeroSizeCount] = extractRegion.GetIndex()[i];
89  nonzeroSizeCount++;
90  }
91  }
92 
93  if (nonzeroSizeCount != OutputImageDimension)
94  {
95  itkExceptionMacro("Extraction Region not consistent with output image");
96  }
97 
98  m_OutputImageRegion.SetSize(outputSize);
99  m_OutputImageRegion.SetIndex(outputIndex);
100  this->Modified();
101 }
102 
112 template <class TInputImage, class TOutputImage>
113 void
116 {
117  // do not call the superclass' implementation of this method since
118  // this filter allows the input and the output to be of different dimensions
119 
120  // get pointers to the input and output
121  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
122  typename Superclass::InputImageConstPointer inputPtr = this->GetInput();
123 
124  if ( !outputPtr || !inputPtr)
125  {
126  return;
127  }
128 
129  // Set the output image size to the same value as the extraction region.
130  outputPtr->SetLargestPossibleRegion( m_OutputImageRegion );
131 
132  // Set the output spacing and origin
133  const ImageBase<InputImageDimension> *phyData;
134 
135  phyData
136  = dynamic_cast<const ImageBase<InputImageDimension>*>(this->GetInput());
137 
138  if (phyData)
139  {
140  // Copy what we can from the image from spacing and origin of the input
141  // This logic needs to be augmented with logic that select which
142  // dimensions to copy
143 
144  unsigned int i;
145  const typename InputImageType::SpacingType&
146  inputSpacing = inputPtr->GetSpacing();
147  const typename InputImageType::DirectionType&
148  inputDirection = inputPtr->GetDirection();
149  const typename InputImageType::PointType&
150  inputOrigin = inputPtr->GetOrigin();
151 
152  typename OutputImageType::SpacingType outputSpacing;
153  typename OutputImageType::DirectionType outputDirection;
154  typename OutputImageType::PointType outputOrigin;
155 
156  if ( static_cast<unsigned int>(OutputImageDimension) >
157  static_cast<unsigned int>(InputImageDimension ) )
158  {
159  // copy the input to the output and fill the rest of the
160  // output with zeros.
161  for (i=0; i < InputImageDimension; ++i)
162  {
163  outputSpacing[i] = inputSpacing[i];
164  outputOrigin[i] = inputOrigin[i];
165  for (unsigned int dim = 0; dim < InputImageDimension; ++dim)
166  {
167  outputDirection[i][dim] = inputDirection[i][dim];
168  }
169  }
170  for (; i < OutputImageDimension; ++i)
171  {
172  outputSpacing[i] = 1.0;
173  outputOrigin[i] = 0.0;
174  for (unsigned int dim = 0; dim < InputImageDimension; ++dim)
175  {
176  outputDirection[i][dim] = 0.0;
177  }
178  outputDirection[i][i] = 1.0;
179  }
180  }
181  else
182  {
183  // copy the non-collapsed part of the input spacing and origing to the output
184  outputDirection.SetIdentity();
185  int nonZeroCount = 0;
186  for (i=0; i < InputImageDimension; ++i)
187  {
188  if (m_ExtractionRegion.GetSize()[i])
189  {
190  outputSpacing[nonZeroCount] = inputSpacing[i];
191  outputOrigin[nonZeroCount] = inputOrigin[i];
192  int nonZeroCount2 = 0;
193  for (unsigned int dim = 0; dim < InputImageDimension; ++dim)
194  {
195  if (m_ExtractionRegion.GetSize()[dim])
196  {
197  outputDirection[nonZeroCount][nonZeroCount2] =
198  inputDirection[nonZeroCount][dim];
199  ++nonZeroCount2;
200  }
201  }
202  nonZeroCount++;
203  }
204  }
205  }
206  // This is a temporary fix to get over the problems with using OrientedImages to
207  // a non-degenerate extracted image to be created. It still needs to be determined
208  // how to compute the correct outputDirection from all possible input directions.
209  if (vnl_determinant(outputDirection.GetVnlMatrix()) == 0.0)
210  {
211  outputDirection.SetIdentity();
212  }
213 
214  // set the spacing and origin
215  outputPtr->SetSpacing( outputSpacing );
216  outputPtr->SetDirection( outputDirection );
217  outputPtr->SetOrigin( outputOrigin );
218  outputPtr->SetNumberOfComponentsPerPixel(
219  inputPtr->GetNumberOfComponentsPerPixel() );
220  }
221  else
222  {
223  // pointer could not be cast back down
224  itkExceptionMacro(<< "itk::ExtractImageFilter::GenerateOutputInformation "
225  << "cannot cast input to "
226  << typeid(ImageBase<InputImageDimension>*).name() );
227  }
228 }
229 
242 template <class TInputImage, class TOutputImage>
243 void
245 ::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread,
246  int threadId)
247 {
248  itkDebugMacro(<<"Actually executing");
249 
250  // Get the input and output pointers
251  typename Superclass::InputImageConstPointer inputPtr = this->GetInput();
252  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
253 
254  // support progress methods/callbacks
255  ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
256 
257  // Define the portion of the input to walk for this thread
258  InputImageRegionType inputRegionForThread;
259  this->CallCopyOutputRegionToInputRegion(inputRegionForThread, outputRegionForThread);
260 
261  // Define the iterators.
262  typedef ImageRegionIterator<TOutputImage> OutputIterator;
263  typedef ImageRegionConstIterator<TInputImage> InputIterator;
264 
265  OutputIterator outIt(outputPtr, outputRegionForThread);
266  InputIterator inIt(inputPtr, inputRegionForThread);
267 
268  // walk the output region, and sample the input image
269  while( !outIt.IsAtEnd() )
270  {
271  // copy the input pixel to the output
272  outIt.Set( static_cast<OutputImagePixelType>(inIt.Get()));
273  ++outIt;
274  ++inIt;
275  progress.CompletedPixel();
276  }
277 }
278 
279 } // end namespace itk
280 
281 #endif

Generated at Sat May 18 2013 23:37:33 for Orfeo Toolbox with doxygen 1.8.3.1