OTB  5.0.0
Orfeo Toolbox
otbImageFittingPolygonListFilter.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 #ifndef __otbImageFittingPolygonListFilter_txx
19 #define __otbImageFittingPolygonListFilter_txx
20 
24 
25 namespace otb
26 {
30 template <class TPath, class TImage>
33 {
34  this->SetNumberOfRequiredInputs(2);
35  this->SetNumberOfRequiredInputs(2);
36  m_Radius = 1;
37  m_NumberOfIterations = 1;
38 }
40 
41 template <class TPath, class TImage>
42 void
44 ::SetInputImage(const ImageType * image)
45 {
46  this->itk::ProcessObject::SetNthInput(1, const_cast<ImageType *>(image));
47 }
48 
49 template <class TPath, class TImage>
51 ::ImageType *
54 {
55  if (this->GetNumberOfInputs() < 1)
56  {
57  return 0;
58  }
59  return static_cast<const ImageType *>(this->itk::ProcessObject::GetInput(1));
60 }
61 
62 //FIXME
63 //There is an issue here with integer and continous indexes
64 //maybe we should use the itk::LineConstIterator
65 template <class TPath, class TImage>
66 void
69 {
70  // I/O wiring
71  ImageConstPointerType inputImagePtr = this->GetInputImage();
72  const PathListType * inputPtr = this->GetInput();
73  PathListType * outputPtr = this->GetOutput();
74 
75  typename ImageType::RegionType regionLargest = inputImagePtr->GetLargestPossibleRegion();
76 
77  typedef itk::ImageRegionConstIteratorWithIndex<ImageType> NeighborhoodIteratorType;
78 
79  typename ImageType::SizeType size;
80  size[0] = 2 * m_Radius + 1;
81  size[1] = 2 * m_Radius + 1;
82  typename ImageType::RegionType region;
83  region.SetSize(size);
84  typename ImageType::IndexType start;
85 
86  //go through all the polygons in the list
87  IteratorType it = inputPtr->Begin();
88  while (it != inputPtr->End())
89  {
90  PathPointerType polygon = it.Get();
91  if (polygon->GetVertexList()->Size() > 2)
92  {
93  for (unsigned int iteration = 0; iteration < m_NumberOfIterations; ++iteration)
94  {
95  PathPointerType newPolygon = PathType::New();
96  VertexListConstIteratorType vertexIt = polygon->GetVertexList()->Begin();
97  //We are now going to go through all the vertex, we won't start to process
98  // first as we need to know the last one for that.
99  VertexType firstPoint = vertexIt.Value();
100  VertexType previousPoint = vertexIt.Value();
101  ++vertexIt;
102  VertexType currentPoint = vertexIt.Value();
103  ++vertexIt;
104  while (vertexIt != polygon->GetVertexList()->End())
105  {
106  VertexType nextPoint = vertexIt.Value();
110  {
111 
112  start[0] = static_cast<long int>(currentPoint[0] - m_Radius);
113  start[1] = static_cast<long int>(currentPoint[1] - m_Radius);
114  region.SetIndex(start);
115  region.Crop(inputImagePtr->GetLargestPossibleRegion());
116 
117  NeighborhoodIteratorType nIt(inputImagePtr, region);
118  double maxValue = 0.0;
119  VertexType maxPoint = currentPoint;
120  nIt.GoToBegin();
121  while (!nIt.IsAtEnd())
122  {
123  if (regionLargest.IsInside(nIt.GetIndex()))
124  {
125  VertexType middlePoint = static_cast<VertexType>(nIt.GetIndex());
126  double currentValue = computeValue(inputImagePtr, middlePoint, previousPoint, nextPoint);
127  if (currentValue > maxValue)
128  {
129  maxValue = currentValue;
130  maxPoint = middlePoint;
131  }
132  }
133  ++nIt;
134  }
135  currentPoint = maxPoint;
136  newPolygon->AddVertex(maxPoint);
137  }
140  ++vertexIt;
141  previousPoint = currentPoint;
142  currentPoint = nextPoint;
143 
144  }
145  //We now need to process the last and the first point
146 
147  VertexType nextPoint = firstPoint;
151  {
152  start[0] = static_cast<long int>(currentPoint[0] - m_Radius);
153  start[1] = static_cast<long int>(currentPoint[1] - m_Radius);
154  region.SetIndex(start);
156 
157  NeighborhoodIteratorType nIt(inputImagePtr, region);
158  double maxValue = 0.0;
159  VertexType maxPoint = currentPoint;
160  nIt.GoToBegin();
161 
162  while (!nIt.IsAtEnd())
163  {
164  if (regionLargest.IsInside(nIt.GetIndex()))
165  {
166  VertexType middlePoint = static_cast<VertexType>(nIt.GetIndex());
167  double currentValue = computeValue(inputImagePtr, middlePoint, previousPoint, nextPoint);
168  if (currentValue > maxValue)
169  {
170  maxValue = currentValue;
171  maxPoint = middlePoint;
172  }
173  }
174  ++nIt;
175  }
176  currentPoint = maxPoint;
177  newPolygon->AddVertex(maxPoint);
178  }
181  previousPoint = currentPoint;
182  currentPoint = firstPoint;
183  vertexIt = newPolygon->GetVertexList()->Begin();
184  nextPoint = vertexIt.Value();
185 
189  {
190 
191  start[0] = static_cast<long int>(currentPoint[0] - m_Radius);
192  start[1] = static_cast<long int>(currentPoint[1] - m_Radius);
193  region.SetIndex(start);
194 
195  NeighborhoodIteratorType nIt(inputImagePtr, region);
196  double maxValue = 0.0;
197  VertexType maxPoint = currentPoint;
198  nIt.GoToBegin();
199  while (!nIt.IsAtEnd())
200  {
201  if (regionLargest.IsInside(nIt.GetIndex()))
202  {
203  VertexType middlePoint = static_cast<VertexType>(nIt.GetIndex());
204  double currentValue = computeValue(inputImagePtr, middlePoint, previousPoint, nextPoint);
205  if (currentValue > maxValue)
206  {
207  maxValue = currentValue;
208  maxPoint = middlePoint;
209  }
210  }
211  ++nIt;
212  }
213  currentPoint = maxPoint;
214  newPolygon->AddVertex(maxPoint);
215  }
218  polygon = newPolygon; //prepare the next iteration
219  }
220  }
221 
222  outputPtr->PushBack(polygon);
223 
224  ++it;
225  } //going through the polygon list
226 }
227 
228 template <class TPath, class TImage>
229 double
231 ::computeValue(ImageConstPointerType image, VertexType middlePoint, VertexType previousPoint,
232  VertexType nextPoint) const
233 {
234  typedef typename ImageType::IndexType IndexType;
235  IndexType middleIndex;
236  IndexType previousIndex;
237  IndexType nextIndex;
238  middleIndex[0] = static_cast<long int>(middlePoint[0]);
239  middleIndex[1] = static_cast<long int>(middlePoint[1]);
240  previousIndex[0] = static_cast<long int>(previousPoint[0]);
241  previousIndex[1] = static_cast<long int>(previousPoint[1]);
242  nextIndex[0] = static_cast<long int>(nextPoint[0]);
243  nextIndex[1] = static_cast<long int>(nextPoint[1]);
244  double currentValue = 0.0;
245  unsigned int count = 0;
246  //compute for first segment
247  LineConstIteratorType itLineFirst(image, previousIndex, middleIndex);
248  while (!itLineFirst.IsAtEnd())
249  {
250  currentValue += itLineFirst.Get();
251  ++count;
252  ++itLineFirst;
253  }
254 
255  //compute for second segment
256  LineConstIteratorType itLineSecond(image, nextIndex, middleIndex);
257  while (!itLineSecond.IsAtEnd())
258  {
259  currentValue += itLineSecond.Get();
260  ++count;
261  ++itLineSecond;
262  }
263 
264  return currentValue / count;
265 }
266 
270 template <class TPath, class TImage>
271 void
273 ::PrintSelf(std::ostream& os, itk::Indent indent) const
274 {
275  Superclass::PrintSelf(os, indent);
276 }
277 
278 } // End namespace otb
279 #endif
PixelType Get(void) const
Slightly deform polygon to reach higher enery from the image.
VertexListType::ConstIterator VertexListConstIteratorType
DataObject * GetInput(const DataObjectIdentifierType &key)
const PixelType Get(void) const
virtual double computeValue(ImageConstPointerType image, VertexType middlePoint, VertexType previousPoint, VertexType nextPoint) const
virtual void SetNthInput(DataObjectPointerArraySizeType num, DataObject *input)
virtual void PrintSelf(std::ostream &os, itk::Indent indent) const