Orfeo Toolbox  3.16
itkGradientImageToBloxBoundaryPointImageFilter.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkGradientImageToBloxBoundaryPointImageFilter.txx,v $
5  Language: C++
6  Date: $Date: 2009-05-08 12:48:09 $
7  Version: $Revision: 1.14 $
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 __itkGradientImageToBloxBoundaryPointImageFilter_txx
18 #define __itkGradientImageToBloxBoundaryPointImageFilter_txx
19 
20 #include "itkProgressReporter.h"
23 
24 namespace itk
25 {
26 
27 template< typename TInputImage >
30 {
31  itkDebugMacro(<< "GradientImageToBloxBoundaryPointImageFilter::GradientImageToBloxBoundaryPointImageFilter() called");
32 
33  // The default threshold level is 128 (for no particular reason)
34  m_Threshold = 128;
35 
36  for( unsigned int j = 0; j < NDimensions; j++ )
37  {
38  m_BloxResolution[j] = 10.0;
39  }
40 }
41 
42 template< typename TInputImage >
43 void
45 ::SetBloxResolution(float bloxResolution[])
46 {
47  unsigned int j = 0;
48  for( j = 0; j < NDimensions; j++ )
49  {
50  if( bloxResolution[j] != m_BloxResolution[j] ) break;
51  }
52  if( j < NDimensions )
53  {
54  this->Modified();
55  for( j = 0; j < Superclass::ImageDimension; j++ )
56  {
57  m_BloxResolution[j] = bloxResolution[j];
58  if( m_BloxResolution[j] < 1 )
59  {
60  m_BloxResolution[j] = 1;
61  }
62  }
63  }
64 }
65 
66 
67 template< typename TInputImage >
68 void
70 ::SetBloxResolution(float bloxResolution)
71 {
72  unsigned int j = 0;
73  for( j = 0; j < NDimensions; j++ )
74  {
75  if( bloxResolution != m_BloxResolution[j] ) break;
76  }
77  if( j < NDimensions )
78  {
79  this->Modified();
80  for( j = 0; j < NDimensions; j++ )
81  {
82  m_BloxResolution[j] = bloxResolution;
83  if( m_BloxResolution[j] < 1 )
84  {
85  m_BloxResolution[j] = 1;
86  }
87  }
88  }
89 }
90 
91 template< typename TInputImage >
92 void
95 {
96  itkDebugMacro(<< "GradientImageToBloxBoundaryPointImageFilter::GenerateInputRequestedRegion() called");
97 
98  // call the superclass' implementation of this method
99  Superclass::GenerateInputRequestedRegion();
100 
101  // get pointers to the input and output
102  InputImagePointer inputPtr =
103  const_cast< TInputImage * >( this->GetInput());
104  OutputImagePointer outputPtr = this->GetOutput();
105 
106  if ( !inputPtr || !outputPtr )
107  {
108  return;
109  }
110 
111  // we need to compute the input requested region (size and start index)
112  const typename TOutputImage::SizeType& outputRequestedRegionSize
113  = outputPtr->GetRequestedRegion().GetSize();
114  const typename TOutputImage::IndexType& outputRequestedRegionStartIndex
115  = outputPtr->GetRequestedRegion().GetIndex();
116 
117  typedef typename SizeType::SizeValueType SizeValueType;
118  typedef typename IndexType::IndexValueType IndexValueType;
119 
120  SizeType inputRequestedRegionSize;
121  IndexType inputRequestedRegionStartIndex;
122 
123  for (unsigned int i = 0; i < TInputImage::ImageDimension; i++)
124  {
125  inputRequestedRegionSize[i] = static_cast<SizeValueType>(
126  outputRequestedRegionSize[i] * m_BloxResolution[i] );
127  inputRequestedRegionStartIndex[i] = static_cast<IndexValueType>(
128  outputRequestedRegionStartIndex[i] * m_BloxResolution[i] );
129  }
130 
131  typename TInputImage::RegionType inputRequestedRegion;
132  inputRequestedRegion.SetSize( inputRequestedRegionSize );
133  inputRequestedRegion.SetIndex( inputRequestedRegionStartIndex );
134 
135  inputPtr->SetRequestedRegion( inputRequestedRegion );
136 }
137 
138 template< typename TInputImage >
139 void
142 {
143  // call the superclass' implementation of this method
144  Superclass::GenerateOutputInformation();
145 
146  // get pointers to the input and output
147  InputImageConstPointer inputPtr = this->GetInput();
148  OutputImagePointer outputPtr = this->GetOutput();
149 
150  if ( !inputPtr || !outputPtr )
151  {
152  return;
153  }
154 
155  // we need to compute the output spacing, the output image size, and the
156  // output image start index
157  const typename TInputImage::SpacingType& inputSpacing = inputPtr->GetSpacing();
158  const typename TInputImage::SizeType& inputSize = inputPtr->GetLargestPossibleRegion().GetSize();
159  const typename TInputImage::IndexType& inputStartIndex = inputPtr->GetLargestPossibleRegion().GetIndex();
160 
161  typename TOutputImage::SpacingType outputSpacing;
162  typedef typename SizeType::SizeValueType SizeValueType;
163  typedef typename IndexType::IndexValueType IndexValueType;
164 
165  SizeType outputSize;
166  IndexType outputStartIndex;
167 
168  for (unsigned int i = 0; i < TOutputImage::ImageDimension; i++)
169  {
170  outputSpacing[i] = inputSpacing[i] * m_BloxResolution[i];
171 
172  outputSize[i] = static_cast<SizeValueType>(
173  vcl_floor(static_cast<float>( inputSize[i] )/ m_BloxResolution[i]));
174 
175  if( outputSize[i] < 1 )
176  {
177  outputSize[i] = 1;
178  }
179 
180  outputStartIndex[i] = static_cast<IndexValueType>(
181  vcl_ceil(static_cast<float>( inputStartIndex[i] ) / m_BloxResolution[i] ));
182  }
183 
184  outputPtr->SetSpacing( outputSpacing );
185 
186 #ifdef ITK_USE_CENTERED_PIXEL_COORDINATES_CONSISTENTLY
187  // compute origin offset
188  // The physical center's of the input and output should be the same
191  for( unsigned int j = 0; j < TOutputImage::ImageDimension; j++ )
192  {
193  inputCenterIndex[j] = inputStartIndex[j] + (inputSize[j] - 1) / 2.0;
194  outputCenterIndex[j] = outputStartIndex[j] + (outputSize[j] - 1) / 2.0;
195  }
196 
197  typename TOutputImage::PointType inputCenterPoint;
198  typename TOutputImage::PointType outputCenterPoint;
199  inputPtr->TransformContinuousIndexToPhysicalPoint(inputCenterIndex, inputCenterPoint);
200  outputPtr->TransformContinuousIndexToPhysicalPoint(outputCenterIndex, outputCenterPoint);
201 
202  typename TOutputImage::PointType outputOrigin = outputPtr->GetOrigin();
203  outputOrigin = outputOrigin + (inputCenterPoint - outputCenterPoint);
204  outputPtr->SetOrigin(outputOrigin);
205 #endif
206 
207  typename TOutputImage::RegionType outputLargestPossibleRegion;
208  outputLargestPossibleRegion.SetSize( outputSize );
209  outputLargestPossibleRegion.SetIndex( outputStartIndex );
210 
211  outputPtr->SetLargestPossibleRegion( outputLargestPossibleRegion );
212 }
213 
214 template< typename TInputImage >
215 void
218 {
219  itkDebugMacro(<< "GradientImageToBloxBoundaryPointImageFilter::GenerateData() called");
220 
221  // Get the input and output pointers
222  InputImageConstPointer inputPtr = this->GetInput(0);
223  OutputImagePointer outputPtr = this->GetOutput(0);
224 
225  // Allocate the output
226  typename TOutputImage::RegionType outputRequestedRegion = outputPtr->GetRequestedRegion();
227  outputPtr->SetBufferedRegion( outputRequestedRegion );
228  outputPtr->Allocate();
229 
230  typename TInputImage::RegionType inputRequestedRegion = inputPtr->GetRequestedRegion();
231 
232  // Create a progress reporter
233  ProgressReporter progress(this, 0, inputRequestedRegion.GetNumberOfPixels());
234 
235  // Position to figure out pixel location
236  TPositionType inputPosition;
237 
238  // Create an iterator to walk the input image
239  typedef ImageRegionConstIterator<TInputImage> TInputIterator;
240 
241  TInputIterator inputIt = TInputIterator(inputPtr, inputRequestedRegion );
242 
243  // Keep track of how many boundary points we found (for debugging)
244  unsigned long int numBP = 0;
245  unsigned long int numBPadded = 0;
246 
247  // Get the index of the pixel
248  IndexType bloxIndex;
249 
250  for ( inputIt.GoToBegin(); !inputIt.IsAtEnd(); ++inputIt)
251  {
252  // Figure out the magnitude of the gradient
253  double mag = 0;
254 
255  for(unsigned int i = 0; i < NDimensions; i++)
256  {
257  const double component = inputIt.Get()[i];
258  mag += component * component;
259  }
260 
261  mag = vcl_sqrt(mag);
262 
263  // If the pixel meets threshold requirements, add it to the image
264  if( mag >= m_Threshold)
265  {
266  numBP++;
267 
268  // Convert the index of the input pixel to the physical location of the
269  // boundary point in the input image
270  inputPtr->TransformIndexToPhysicalPoint( inputIt.GetIndex(), inputPosition );
271 
272  // Transform the physical location to a blox index
273  outputPtr->TransformPhysicalPointToIndex( inputPosition, bloxIndex );
274 
275 #ifdef ITK_USE_CENTERED_PIXEL_COORDINATES_CONSISTENTLY
276  // check if it is inside of the output image.
277  if( outputRequestedRegion.IsInside( bloxIndex ) )
278  {
279  // Create a new boundary point item and set its parameters
281  pItem->SetPhysicalPosition(inputPosition);
282  pItem->SetGradient( inputIt.Get() );
283 
284  outputPtr->GetPixel(bloxIndex).push_back(pItem);
285  }
286 #else
287  // Create a new boundary point item and set its parameters
289  pItem->SetPhysicalPosition(inputPosition);
290  pItem->SetGradient( inputIt.Get() );
291 
292  outputPtr->GetPixel(bloxIndex).push_back(pItem);
293 #endif
294 
295  numBPadded++;
296  }
297 
298  progress.CompletedPixel();
299  }
300 
301  outputPtr->SetNumBoundaryPoints(numBP);
302 
303  itkDebugMacro(<< "Finished looking for boundary points\n"
304  << "I found " << numBP << " points\n"
305  << "I added " << numBPadded << " points\n");
306 }
307 
308 template< typename TInputImage >
309 void
311 ::PrintSelf(std::ostream& os, Indent indent) const
312 {
313  Superclass::PrintSelf(os,indent);
314 
315  os << indent << "Threshold level: " << m_Threshold << std::endl;
316 }
317 
318 } // end namespace
319 
320 #endif

Generated at Sat Feb 2 2013 23:39:27 for Orfeo Toolbox with doxygen 1.8.1.1