ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkFrequencyHalfHermitianFFTLayoutImageRegionConstIteratorWithIndex.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkFrequencyHalfHermitianFFTLayoutImageRegionConstIteratorWithIndex_h
19 #define itkFrequencyHalfHermitianFFTLayoutImageRegionConstIteratorWithIndex_h
20 
22 
23 namespace itk
24 {
118 template< typename TImage >
120  public ImageRegionConstIteratorWithIndex< TImage >
121 {
122 public:
126 
129  using SizeType = typename Superclass::SizeType;
138 
139  using FrequencyType = typename ImageType::SpacingType;
140  using FrequencyValueType = typename ImageType::SpacingValueType;
144 
145  {
146  this->Init();
147  }
148 
152  const TImage *ptr, const RegionType & region) :
153  ImageRegionConstIteratorWithIndex< TImage >(ptr, region),
155  {
156  this->Init();
157  }
158 
166  const Superclass & it) :
167  ImageRegionConstIteratorWithIndex< TImage >(it),
169  {
170  this->Init();
171  }
172 
173  /*
174  * Image Index [0, N - 1] returns [0 to N/2] (positive) union [-N/2 + 1, -1] (negative).
175  * So index N/2 + 1 returns the bin -N/2 + 1.
176  * If first index of the image is not zero, it stills returns values in the same range.
177  * f = [0, 1, ..., N/2-1, -N/2, ..., -1] if N is even
178  * f = [0, 1, ..., (N-1)/2, -(N-1)/2, ..., -1] if N is odd
179  */
181  {
182  IndexType freqInd;
183  freqInd.Fill(0);
184  for (unsigned int dim = 0; dim < TImage::ImageDimension; dim++)
185  {
186  if (this->m_PositionIndex[dim] <= m_LargestPositiveFrequencyIndex[dim])
187  {
188  freqInd[dim] = this->m_PositionIndex[dim] - this->m_MinIndex[dim];
189  }
190  else // -. From -N/2 + 1 (Nyquist if even) to -1 (-df in frequency)
191  {
192  freqInd[dim] = this->m_PositionIndex[dim] - (this->m_MaxIndex[dim] + 1);
193  }
194  }
195  return freqInd;
196  }
197 
218  {
219  FrequencyType freq;
220  IndexType freqInd = this->GetFrequencyBin();
222 
223  for (unsigned int dim = 0; dim < TImage::ImageDimension; dim++)
224  {
225  freq[dim] = this->m_FrequencyOrigin[dim]
226  + this->m_FrequencySpacing[dim] * freqInd[dim];
227  }
228  return freq;
229  }
230 
232  {
233  FrequencyValueType w2(0);
234  FrequencyType w( this->GetFrequency() );
235 
236  for (unsigned int dim = 0; dim < TImage::ImageDimension; dim++)
237  {
238  w2 += w[dim] * w[dim];
239  }
240  return w2;
241  }
242 
252  itkGetConstReferenceMacro(LargestPositiveFrequencyIndex, IndexType);
253 
255  itkGetConstReferenceMacro(MinIndex, IndexType);
256 
258  itkGetConstReferenceMacro(MaxIndex, IndexType);
259 
261  itkGetConstReferenceMacro(FrequencyOrigin, FrequencyType);
262 
269  itkGetConstReferenceMacro(FrequencySpacing, FrequencyType);
270 
276  void SetActualXDimensionIsOdd(bool value)
277  {
278  this->m_ActualXDimensionIsOdd = value;
279  SizeType sizeImage =
280  this->m_Image->GetLargestPossibleRegion().GetSize();
281  auto size_estimated = 2 * ( sizeImage[0] - 1);
282  size_estimated += this->GetActualXDimensionIsOdd() ? 1 : 0;
283  this->m_FrequencySpacing[0] = 1.0 / (this->m_Image->GetSpacing()[0] * size_estimated);
284  }
285  itkGetMacro(ActualXDimensionIsOdd, bool);
286  itkBooleanMacro(ActualXDimensionIsOdd);
288 
289 private:
293  void Init()
294  {
295  SizeType sizeImage =
296  this->m_Image->GetLargestPossibleRegion().GetSize();
297  this->m_MinIndex =
298  this->m_Image->GetLargestPossibleRegion().GetIndex();
299  this->m_MaxIndex =
300  this->m_Image->GetLargestPossibleRegion().GetUpperIndex();
301  for (unsigned int dim = 0; dim < ImageType::ImageDimension; dim++)
302  {
303  this->m_LargestPositiveFrequencyIndex[dim] = static_cast<FrequencyValueType>(
304  this->m_MinIndex[dim] + sizeImage[dim] / 2 );
305  // Set frequency metadata.
306  // Origin of frequencies is zero after a FFT.
307  this->m_FrequencyOrigin[dim] = 0.0;
308  // SamplingFrequency = 1.0 / SpatialImageSpacing
309  // Freq_BinSize = SamplingFrequency / Size
310  this->m_FrequencySpacing[dim] = 1.0 / (this->m_Image->GetSpacing()[dim] * sizeImage[dim]);
311  }
312  // Corrections for Hermitian
313  // The fastest index has no negative frequencies.
314  this->m_LargestPositiveFrequencyIndex[0] = static_cast<FrequencyValueType>(
315  this->m_MaxIndex[0]);
316  // In fastest dimension only:
317  // Size_estimated_original = 2 * (Size_current - 1 )
318  // Where size_current is the size of the output of RealToHalfHermitianFFT
319  // Ex: Size Original = 10, Current = 6, Estimated = 10.
320  // Size Original = 11, Current = 6, Estimated = 10.
321  auto size_estimated = 2 * ( sizeImage[0] - 1);
322  size_estimated += this->GetActualXDimensionIsOdd() ? 1 : 0;
323  this->m_FrequencySpacing[0] = 1.0 / (this->m_Image->GetSpacing()[0] * size_estimated);
324  }
326 
333 };
334 } // end namespace itk
335 #endif
A multi-dimensional iterator templated over image type that walks pixels within a region and is speci...
typename PixelContainer::Pointer PixelContainerPointer
typename TImage::InternalPixelType InternalPixelType
A multi-dimensional iterator templated over image type that walks an image region and is specialized ...
A base class for multi-dimensional iterators templated over image type that are designed to efficient...
typename Superclass::PixelContainerPointer PixelContainerPointer
typename TImage::PixelContainer PixelContainer
typename Superclass::InternalPixelType InternalPixelType