OTB  9.0.0
Orfeo Toolbox
otbImageFittingPolygonListFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbImageFittingPolygonListFilter_hxx
22 #define otbImageFittingPolygonListFilter_hxx
23 
26 #include "itkImageRegionConstIteratorWithIndex.h"
27 
28 namespace otb
29 {
33 template <class TPath, class TImage>
35 {
36  this->SetNumberOfRequiredInputs(2);
37  this->SetNumberOfRequiredInputs(2);
38  m_Radius = 1;
39  m_NumberOfIterations = 1;
40 }
42 
43 template <class TPath, class TImage>
45 {
46  this->itk::ProcessObject::SetNthInput(1, const_cast<ImageType*>(image));
47 }
48 
49 template <class TPath, class TImage>
51 {
52  if (this->GetNumberOfInputs() < 1)
53  {
54  return nullptr;
55  }
56  return static_cast<const ImageType*>(this->itk::ProcessObject::GetInput(1));
57 }
58 
59 // FIXME
60 // There is an issue here with integer and continuous indexes
61 // maybe we should use the itk::LineConstIterator
62 template <class TPath, class TImage>
64 {
65  // I/O wiring
66  ImageConstPointerType inputImagePtr = this->GetInputImage();
67  const PathListType* inputPtr = this->GetInput();
68  PathListType* outputPtr = this->GetOutput();
69 
70  typename ImageType::RegionType regionLargest = inputImagePtr->GetLargestPossibleRegion();
71 
72  typedef itk::ImageRegionConstIteratorWithIndex<ImageType> NeighborhoodIteratorType;
73 
74  typename ImageType::SizeType size;
75  size[0] = 2 * m_Radius + 1;
76  size[1] = 2 * m_Radius + 1;
77  typename ImageType::RegionType region;
78  region.SetSize(size);
79  typename ImageType::IndexType start;
80 
81  // go through all the polygons in the list
82  IteratorType it = inputPtr->Begin();
83  while (it != inputPtr->End())
84  {
85  PathPointerType polygon = it.Get();
86  if (polygon->GetVertexList()->Size() > 2)
87  {
88  for (unsigned int iteration = 0; iteration < m_NumberOfIterations; ++iteration)
89  {
90  PathPointerType newPolygon = PathType::New();
91  VertexListConstIteratorType vertexIt = polygon->GetVertexList()->Begin();
92  // We are now going to go through all the vertex, we won't start to process
93  // first as we need to know the last one for that.
94  VertexType firstPoint = vertexIt.Value();
95  VertexType previousPoint = vertexIt.Value();
96  ++vertexIt;
97  VertexType currentPoint = vertexIt.Value();
98  ++vertexIt;
99  while (vertexIt != polygon->GetVertexList()->End())
100  {
101  VertexType nextPoint = vertexIt.Value();
105  {
106 
107  start[0] = static_cast<long int>(currentPoint[0] - m_Radius);
108  start[1] = static_cast<long int>(currentPoint[1] - m_Radius);
109  region.SetIndex(start);
110  region.Crop(inputImagePtr->GetLargestPossibleRegion());
111 
112  NeighborhoodIteratorType nIt(inputImagePtr, region);
113  double maxValue = 0.0;
114  VertexType maxPoint = currentPoint;
115  nIt.GoToBegin();
116  while (!nIt.IsAtEnd())
117  {
118  if (regionLargest.IsInside(nIt.GetIndex()))
119  {
120  VertexType middlePoint = static_cast<VertexType>(nIt.GetIndex());
121  double currentValue = computeValue(inputImagePtr, middlePoint, previousPoint, nextPoint);
122  if (currentValue > maxValue)
123  {
124  maxValue = currentValue;
125  maxPoint = middlePoint;
126  }
127  }
128  ++nIt;
129  }
130  currentPoint = maxPoint;
131  newPolygon->AddVertex(maxPoint);
132  }
135  ++vertexIt;
136  previousPoint = currentPoint;
137  currentPoint = nextPoint;
138  }
139  // We now need to process the last and the first point
140 
141  VertexType nextPoint = firstPoint;
145  {
146  start[0] = static_cast<long int>(currentPoint[0] - m_Radius);
147  start[1] = static_cast<long int>(currentPoint[1] - m_Radius);
148  region.SetIndex(start);
150 
151  NeighborhoodIteratorType nIt(inputImagePtr, region);
152  double maxValue = 0.0;
153  VertexType maxPoint = currentPoint;
154  nIt.GoToBegin();
155 
156  while (!nIt.IsAtEnd())
157  {
158  if (regionLargest.IsInside(nIt.GetIndex()))
159  {
160  VertexType middlePoint = static_cast<VertexType>(nIt.GetIndex());
161  double currentValue = computeValue(inputImagePtr, middlePoint, previousPoint, nextPoint);
162  if (currentValue > maxValue)
163  {
164  maxValue = currentValue;
165  maxPoint = middlePoint;
166  }
167  }
168  ++nIt;
169  }
170  currentPoint = maxPoint;
171  newPolygon->AddVertex(maxPoint);
172  }
175  previousPoint = currentPoint;
176  currentPoint = firstPoint;
177  vertexIt = newPolygon->GetVertexList()->Begin();
178  nextPoint = vertexIt.Value();
179 
183  {
184 
185  start[0] = static_cast<long int>(currentPoint[0] - m_Radius);
186  start[1] = static_cast<long int>(currentPoint[1] - m_Radius);
187  region.SetIndex(start);
188 
189  NeighborhoodIteratorType nIt(inputImagePtr, region);
190  double maxValue = 0.0;
191  VertexType maxPoint = currentPoint;
192  nIt.GoToBegin();
193  while (!nIt.IsAtEnd())
194  {
195  if (regionLargest.IsInside(nIt.GetIndex()))
196  {
197  VertexType middlePoint = static_cast<VertexType>(nIt.GetIndex());
198  double currentValue = computeValue(inputImagePtr, middlePoint, previousPoint, nextPoint);
199  if (currentValue > maxValue)
200  {
201  maxValue = currentValue;
202  maxPoint = middlePoint;
203  }
204  }
205  ++nIt;
206  }
207  currentPoint = maxPoint;
208  newPolygon->AddVertex(maxPoint);
209  }
212  polygon = newPolygon; // prepare the next iteration
213  }
214  }
215 
216  outputPtr->PushBack(polygon);
217 
218  ++it;
219  } // going through the polygon list
220 }
221 
222 template <class TPath, class TImage>
224  VertexType nextPoint) const
225 {
226  typedef typename ImageType::IndexType IndexType;
227  IndexType middleIndex;
228  IndexType previousIndex;
229  IndexType nextIndex;
230  middleIndex[0] = static_cast<long int>(middlePoint[0]);
231  middleIndex[1] = static_cast<long int>(middlePoint[1]);
232  previousIndex[0] = static_cast<long int>(previousPoint[0]);
233  previousIndex[1] = static_cast<long int>(previousPoint[1]);
234  nextIndex[0] = static_cast<long int>(nextPoint[0]);
235  nextIndex[1] = static_cast<long int>(nextPoint[1]);
236  double currentValue = 0.0;
237  unsigned int count = 0;
238  // compute for first segment
239  LineConstIteratorType itLineFirst(image, previousIndex, middleIndex);
240  while (!itLineFirst.IsAtEnd())
241  {
242  currentValue += itLineFirst.Get();
243  ++count;
244  ++itLineFirst;
245  }
246 
247  // compute for second segment
248  LineConstIteratorType itLineSecond(image, nextIndex, middleIndex);
249  while (!itLineSecond.IsAtEnd())
250  {
251  currentValue += itLineSecond.Get();
252  ++count;
253  ++itLineSecond;
254  }
255 
256  return currentValue / count;
257 }
258 
262 template <class TPath, class TImage>
263 void ImageFittingPolygonListFilter<TPath, TImage>::PrintSelf(std::ostream& os, itk::Indent indent) const
264 {
265  Superclass::PrintSelf(os, indent);
266 }
267 
268 } // End namespace otb
269 #endif
otb::ObjectList::Begin
Iterator Begin(void)
Definition: otbObjectList.hxx:237
otb::ImageFittingPolygonListFilter::LineConstIteratorType
itk::LineConstIterator< ImageType > LineConstIteratorType
Definition: otbImageFittingPolygonListFilter.h:70
otb::ObjectList::PushBack
void PushBack(ObjectType *element)
Definition: otbObjectList.hxx:82
otb::ImageFittingPolygonListFilter::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbImageFittingPolygonListFilter.hxx:263
otb::ImageFittingPolygonListFilter::ImageFittingPolygonListFilter
ImageFittingPolygonListFilter()
Definition: otbImageFittingPolygonListFilter.hxx:34
otb::ImageFittingPolygonListFilter::GenerateData
void GenerateData() override
Definition: otbImageFittingPolygonListFilter.hxx:63
otb::ImageFittingPolygonListFilter::computeValue
virtual double computeValue(ImageConstPointerType image, VertexType middlePoint, VertexType previousPoint, VertexType nextPoint) const
Definition: otbImageFittingPolygonListFilter.hxx:223
otb::ObjectList::ConstIterator
ConstIterator of the object list.
Definition: otbObjectList.h:300
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::ImageFittingPolygonListFilter::ImageType
TImage ImageType
Definition: otbImageFittingPolygonListFilter.h:66
otb::ImageFittingPolygonListFilter::VertexListConstIteratorType
VertexListType::ConstIterator VertexListConstIteratorType
Definition: otbImageFittingPolygonListFilter.h:63
otbImageFittingPolygonListFilter.h
otb::ImageFittingPolygonListFilter::GetInputImage
const ImageType * GetInputImage(void)
Definition: otbImageFittingPolygonListFilter.hxx:50
otb::ImageFittingPolygonListFilter::ImageConstPointerType
ImageType::ConstPointer ImageConstPointerType
Definition: otbImageFittingPolygonListFilter.h:68
otb::ImageFittingPolygonListFilter::VertexType
PathType::VertexType VertexType
Definition: otbImageFittingPolygonListFilter.h:61
otb::ImageFittingPolygonListFilter::PathPointerType
Superclass::PathPointerType PathPointerType
Definition: otbImageFittingPolygonListFilter.h:58
otb::ObjectList::End
Iterator End(void)
Definition: otbObjectList.hxx:281
otb::ObjectList::ConstIterator::Get
ObjectPointerType Get(void)
Definition: otbObjectList.h:321
otb::ObjectList
This class is a generic all-purpose wrapping around an std::vector<itk::SmartPointer<ObjectType> >.
Definition: otbObjectList.h:40
otbPolyLineImageConstIterator.h
otb::ImageFittingPolygonListFilter::SetInputImage
void SetInputImage(const ImageType *image)
Definition: otbImageFittingPolygonListFilter.hxx:44