21 #ifndef otbImageToEdgePathFilter_hxx
22 #define otbImageToEdgePathFilter_hxx
26 #include "itkConstantPadImageFilter.h"
27 #include "itkConstShapedNeighborhoodIterator.h"
28 #include "itkImageRegionConstIteratorWithIndex.h"
35 template <
class TInputImage,
class TOutputPath>
43 template <
class TInputImage,
class TOutputPath>
53 if (initPadConstant == m_ForegroundValue)
58 typedef itk::ConstantPadImageFilter<InputImageType, InputImageType> PadFilterType;
59 typedef itk::ConstShapedNeighborhoodIterator<InputImageType> IteratorType;
60 typedef itk::ImageRegionConstIteratorWithIndex<InputImageType> LinearIteratorType;
63 typename PadFilterType::Pointer pad = PadFilterType::New();
64 pad->SetInput(inputImage);
65 pad->SetConstant(initPadConstant);
69 pad->SetPadUpperBound(padSize);
70 pad->SetPadLowerBound(padSize);
73 LinearIteratorType linIter(pad->GetOutput(), pad->GetOutput()->GetLargestPossibleRegion());
76 while (flag && !linIter.IsAtEnd())
78 if (linIter.Get() == m_ForegroundValue)
87 typename InputImageType::IndexType start = linIter.GetIndex();
91 typename IteratorType::RadiusType radius;
93 IteratorType it(radius, pad->GetOutput(), pad->GetOutput()->GetLargestPossibleRegion());
94 const typename IteratorType::OffsetType LEFT = {{-1, 0}};
95 const typename IteratorType::OffsetType RIGHT = {{1, 0}};
96 const typename IteratorType::OffsetType UP = {{0, -1}};
97 const typename IteratorType::OffsetType DOWN = {{0, 1}};
98 const typename IteratorType::OffsetType LEFTUP = {{-1, -1}};
99 const typename IteratorType::OffsetType RIGHTDOWN = {{1, 1}};
100 const typename IteratorType::OffsetType RIGHTUP = {{1, -1}};
101 const typename IteratorType::OffsetType LEFTDOWN = {{-1, 1}};
102 const typename IteratorType::OffsetType CENTER = {{0, 0}};
103 it.ClearActiveList();
104 it.ActivateOffset(LEFT);
105 it.ActivateOffset(RIGHT);
106 it.ActivateOffset(UP);
107 it.ActivateOffset(DOWN);
108 it.ActivateOffset(CENTER);
109 it.ActivateOffset(RIGHTUP);
110 it.ActivateOffset(RIGHTDOWN);
111 it.ActivateOffset(LEFTUP);
112 it.ActivateOffset(LEFTDOWN);
114 std::vector<typename IteratorType::OffsetType> rotation;
115 rotation.push_back(RIGHT);
116 rotation.push_back(RIGHTDOWN);
117 rotation.push_back(DOWN);
118 rotation.push_back(LEFTDOWN);
119 rotation.push_back(LEFT);
120 rotation.push_back(LEFTUP);
121 rotation.push_back(UP);
122 rotation.push_back(RIGHTUP);
124 it.SetLocation(start);
127 if (it.GetPixel(RIGHT) == m_ForegroundValue)
129 if (it.GetPixel(LEFT) == m_ForegroundValue)
131 if (it.GetPixel(UP) == m_ForegroundValue)
133 if (it.GetPixel(DOWN) == m_ForegroundValue)
135 outputPath->AddVertex(newVertex);
147 int move = nextStart;
149 bool EdgeFound =
false;
151 bool LastWasPositive(
false);
153 while ((move < nextStart + 8) && (!EdgeFound))
157 if ((!LastWasPositive) && (it.GetPixel(rotation[move % 8]) == m_ForegroundValue))
164 LastWasPositive = (it.GetPixel(rotation[move % 8]) == m_ForegroundValue);
172 it += rotation[move % 8];
173 nextStart = (move + 5) % 8;
174 newVertex = it.GetIndex(CENTER);
175 if (it.GetPixel(RIGHT) == m_ForegroundValue)
177 if (it.GetPixel(LEFT) == m_ForegroundValue)
179 if (it.GetPixel(UP) == m_ForegroundValue)
181 if (it.GetPixel(DOWN) == m_ForegroundValue)
183 outputPath->AddVertex(newVertex);
186 if ((it.GetIndex(CENTER) == start) && (nbMove >= 2))
194 for (
int i = 0; i < 8; ++i)
197 if ((it.GetIndex(rotation[i]) == start) && (nbMove >= 2))
214 template <
class TInputImage,
class TOutputPath>
217 Superclass::PrintSelf(os, indent);
218 os <<
"Foreground value : " << m_ForegroundValue << std::endl;