ANIMA  4.0
animaNonLocalPatchBaseSearcher.hxx
Go to the documentation of this file.
1 #pragma once
3 
4 #include <itkImageRegionConstIterator.h>
5 #include <itkImageRegionConstIteratorWithIndex.h>
6 
7 namespace anima
8 {
9 
10 template <class ImageType>
11 NonLocalPatchBaseSearcher <ImageType>
13 {
14  m_PatchHalfSize = 1;
15  m_SearchStepSize = 1;
16  m_MaxAbsDisp = 3;
17  m_WeightThreshold = 0.0;
18 
19  m_InputImage = 0;
20 }
21 
22 template <class ImageType>
23 void
25 ::AddComparisonImage(ImageType *arg)
26 {
27  m_ComparisonImages.push_back(arg);
28 }
29 
30 template <class ImageType>
31 ImageType *
33 ::GetComparisonImage(unsigned int index)
34 {
35  if (index >= m_ComparisonImages.size())
36  return 0;
37 
38  return m_ComparisonImages[index];
39 }
40 
41 template <class ImageType>
42 void
44 ::UpdateAtPosition(const IndexType &dataIndex)
45 {
46  if (m_ComparisonImages.size() == 0)
47  this->AddComparisonImage(m_InputImage);
48  unsigned int numComparisonImages = m_ComparisonImages.size();
49 
50  m_DatabaseWeights.clear();
51  m_DatabaseSamples.clear();
52 
53  IndexType blockIndex, dispIndex, movingIndex;
54  SizeType blockSize, dispSize;
55  ImageRegionType largestImageRegion = this->GetInputImage()->GetLargestPossibleRegion();
56  ImageRegionType blockRegionMoving;
57 
58  for (unsigned int d = 0; d < ImageType::ImageDimension;++d)
59  {
60  int tmpIndex = dataIndex[d] - m_PatchHalfSize;
61  blockIndex[d] = std::max(0, tmpIndex);
62 
63  int maxSize = largestImageRegion.GetSize()[d] - 1;
64  int tmpSize = dataIndex[d] + m_PatchHalfSize;
65  blockSize[d] = std::min(maxSize, tmpSize) - blockIndex[d] + 1;
66 
67  tmpIndex = dataIndex[d] - m_MaxAbsDisp;
68  dispIndex[d] = std::max(0, tmpIndex);
69 
70  tmpSize = dataIndex[d] + m_MaxAbsDisp;
71  dispSize[d] = std::min(maxSize,tmpSize) - dispIndex[d] + 1;
72  }
73 
74  ImageRegionType blockRegion;
75  blockRegion.SetIndex(blockIndex);
76  blockRegion.SetSize(blockSize);
77 
78  this->ComputeInputProperties(blockIndex,blockRegion);
79 
80  ImageRegionType dispRegion;
81  dispRegion.SetIndex(dispIndex);
82  dispRegion.SetSize(dispSize);
83 
84  typedef itk::ImageRegionConstIteratorWithIndex <ImageType> InIteratorType;
85  InIteratorType dispIt(m_InputImage, dispRegion);
86 
87  typedef itk::ImageRegionConstIterator <ImageType> ComparisonIteratorType;
88  std::vector <ComparisonIteratorType> comparisonIt(numComparisonImages);
89  for (unsigned int k = 0;k < numComparisonImages;++k)
90  comparisonIt[k] = ComparisonIteratorType(m_ComparisonImages[k],dispRegion);
91 
92  IndexType dispBaseIndex = dispIt.GetIndex();
93  IndexType dispCurIndex;
94  while (!dispIt.IsAtEnd())
95  {
96  dispCurIndex = dispIt.GetIndex();
97 
98  bool onSearchStepSize(true), movingRegionIsValid(true), isCentralIndex(true);
99  for (unsigned int d = 0; d < ImageType::ImageDimension && onSearchStepSize && movingRegionIsValid; ++d)
100  {
101  //if the iterator isn't at a search step we won't do anything
102  if ((dispCurIndex[d] - dispBaseIndex[d]) % m_SearchStepSize)
103  {
104  onSearchStepSize = false;
105  break;
106  }
107  else
108  {
109  movingIndex[d] = blockIndex[d] + (dispCurIndex[d] - dataIndex[d]);
110  unsigned int maxBlock = movingIndex[d] + blockSize[d];
111  //if movingRegion overfill largestRegion, we won't compute it
112  if (maxBlock > largestImageRegion.GetSize()[d] || movingIndex[d] < 0)
113  {
114  movingRegionIsValid = false;
115  break;
116  }
117  }
118 
119  if (dispCurIndex[d] != dataIndex[d])
120  isCentralIndex = false;
121  }
122 
123  if (movingRegionIsValid && onSearchStepSize && (!isCentralIndex))
124  {
125  blockRegionMoving.SetIndex(movingIndex);
126  blockRegionMoving.SetSize(blockRegion.GetSize());
127 
128  for (unsigned int k = 0;k < numComparisonImages;++k)
129  {
130  this->ComputeComparisonProperties(k,blockRegionMoving);
131 
132  // Should we compute the weight value of this patch ?
133  if (this->TestPatchConformity(k,dataIndex,dispCurIndex))
134  {
135  double weightValue = this->ComputeWeightValue(k,blockRegion,blockRegionMoving);
136  if (weightValue > m_WeightThreshold)
137  {
138  m_DatabaseWeights.push_back(weightValue);
139  // Getting center index value
140  m_DatabaseSamples.push_back(comparisonIt[k].Get());
141  }
142  }
143  }
144  }
145 
146  ++dispIt;
147  for (unsigned int k = 0;k < numComparisonImages;++k)
148  ++comparisonIt[k];
149  }
150 }
151 
152 } // end namespace anima
itk::VectorImage< ImageScalarType, DataImageType::ImageDimension > ::RegionType ImageRegionType
itk::VectorImage< ImageScalarType, DataImageType::ImageDimension > ::SizeType SizeType
itk::VectorImage< ImageScalarType, DataImageType::ImageDimension > ::IndexType IndexType