Orfeo ToolBox  4.2
Orfeo ToolBox is not a black box
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 
23 #include "otbMacro.h"
25 
26 namespace otb
27 {
31 template <class TPath, class TImage>
34 {
35  this->SetNumberOfRequiredInputs(2);
36  this->SetNumberOfRequiredInputs(2);
37  m_Radius = 1;
38  m_NumberOfIterations = 1;
39 }
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);
155 
156  NeighborhoodIteratorType nIt(inputImagePtr, region);
157  double maxValue = 0.0;
158  VertexType maxPoint = currentPoint;
159  nIt.GoToBegin();
160 
161  while (!nIt.IsAtEnd())
162  {
163  if (regionLargest.IsInside(nIt.GetIndex()))
164  {
165  VertexType middlePoint = static_cast<VertexType>(nIt.GetIndex());
166  double currentValue = computeValue(inputImagePtr, middlePoint, previousPoint, nextPoint);
167  if (currentValue > maxValue)
168  {
169  maxValue = currentValue;
170  maxPoint = middlePoint;
171  }
172  }
173  ++nIt;
174  }
175  currentPoint = maxPoint;
176  newPolygon->AddVertex(maxPoint);
177  }
180  previousPoint = currentPoint;
181  currentPoint = firstPoint;
182  vertexIt = newPolygon->GetVertexList()->Begin();
183  nextPoint = vertexIt.Value();
184 
188  {
189 
190  start[0] = static_cast<long int>(currentPoint[0] - m_Radius);
191  start[1] = static_cast<long int>(currentPoint[1] - m_Radius);
192  region.SetIndex(start);
193 
194  NeighborhoodIteratorType nIt(inputImagePtr, region);
195  double maxValue = 0.0;
196  VertexType maxPoint = currentPoint;
197  nIt.GoToBegin();
198  while (!nIt.IsAtEnd())
199  {
200  if (regionLargest.IsInside(nIt.GetIndex()))
201  {
202  VertexType middlePoint = static_cast<VertexType>(nIt.GetIndex());
203  double currentValue = computeValue(inputImagePtr, middlePoint, previousPoint, nextPoint);
204  if (currentValue > maxValue)
205  {
206  maxValue = currentValue;
207  maxPoint = middlePoint;
208  }
209  }
210  ++nIt;
211  }
212  currentPoint = maxPoint;
213  newPolygon->AddVertex(maxPoint);
214  }
217  polygon = newPolygon; //prepare the next iteration
218  }
219  }
220 
221  outputPtr->PushBack(polygon);
222 
223  ++it;
224  } //going through the polygon list
225 }
226 
227 template <class TPath, class TImage>
228 double
230 ::computeValue(ImageConstPointerType image, VertexType middlePoint, VertexType previousPoint,
231  VertexType nextPoint) const
232 {
233  typedef typename ImageType::IndexType IndexType;
234  IndexType middleIndex;
235  IndexType previousIndex;
236  IndexType nextIndex;
237  middleIndex[0] = static_cast<long int>(middlePoint[0]);
238  middleIndex[1] = static_cast<long int>(middlePoint[1]);
239  previousIndex[0] = static_cast<long int>(previousPoint[0]);
240  previousIndex[1] = static_cast<long int>(previousPoint[1]);
241  nextIndex[0] = static_cast<long int>(nextPoint[0]);
242  nextIndex[1] = static_cast<long int>(nextPoint[1]);
243  double currentValue = 0.0;
244  unsigned int count = 0;
245  //compute for first segment
246  LineConstIteratorType itLineFirst(image, previousIndex, middleIndex);
247  while (!itLineFirst.IsAtEnd())
248  {
249  currentValue += itLineFirst.Get();
250  ++count;
251  ++itLineFirst;
252  }
253 
254  //compute for second segment
255  LineConstIteratorType itLineSecond(image, nextIndex, middleIndex);
256  while (!itLineSecond.IsAtEnd())
257  {
258  currentValue += itLineSecond.Get();
259  ++count;
260  ++itLineSecond;
261  }
262 
263  return currentValue / count;
264 }
265 
269 template <class TPath, class TImage>
270 void
272 ::PrintSelf(std::ostream& os, itk::Indent indent) const
273 {
274  Superclass::PrintSelf(os, indent);
275 }
276 
277 } // End namespace otb
278 #endif
Slightly deform polygon to reach higher enery from the image.
Iterator End(void)
VertexListType::ConstIterator VertexListConstIteratorType
This class is a generic all-purpose wrapping around an std::vector<itk::SmartPointer<ObjectType> >...
Definition: otbObjectList.h:34
ObjectPointerType Get(void)
ConstIterator of the object list.
const PixelType Get(void) const
virtual double computeValue(ImageConstPointerType image, VertexType middlePoint, VertexType previousPoint, VertexType nextPoint) const
void PushBack(ObjectType *element)
Iterator Begin(void)
virtual void PrintSelf(std::ostream &os, itk::Indent indent) const