OTB  7.2.0
Orfeo Toolbox
otbLineSegmentDetector.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2020 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 otbLineSegmentDetector_h
22 #define otbLineSegmentDetector_h
23 
24 #include "otbImage.h"
25 #include "otbVectorDataSource.h"
26 #include "otbVectorData.h"
27 
28 #include "itkUnaryFunctorImageFilter.h"
29 #include "itkGradientRecursiveGaussianImageFilter.h"
30 #include "itkGradientImageFilter.h"
31 
32 namespace otb
33 {
34 
35 namespace Functor
36 {
42 template <class TInputPixel, class TOutputPixel>
43 class MagnitudeFunctor
44 {
45 public:
46  inline TOutputPixel operator()(const TInputPixel& input)
47  {
48  return static_cast<TOutputPixel>(2 * std::sqrt(input[0] * input[0] + input[1] * input[1]));
49  }
50 };
51 
58 template <class TInputPixel, class TOutputPixel>
60 {
61 public:
62  inline TOutputPixel operator()(const TInputPixel& input)
63  {
64  return static_cast<TOutputPixel>(std::atan2(input[0], -input[1]));
65  }
66 };
67 } // end namespace Functor
68 
91 template <class TInputImage, class TPrecision = double>
92 class ITK_EXPORT LineSegmentDetector : public VectorDataSource<otb::VectorData<TPrecision>>
93 {
94 public:
98  typedef itk::SmartPointer<Self> Pointer;
99  typedef itk::SmartPointer<const Self> ConstPointer;
100 
102  itkNewMacro(Self);
103 
106 
108  typedef TInputImage InputImageType;
109  typedef typename InputImageType::PixelType InputPixelType;
112  typedef typename InputImageType::RegionType RegionType;
115 
122 
128 
130  typedef std::vector<OutputIndexType> IndexVectorType;
131  typedef typename IndexVectorType::iterator IndexVectorIteratorType;
132  typedef std::vector<IndexVectorType> CoordinateHistogramType;
133  typedef typename CoordinateHistogramType::iterator CoordinateHistogramIteratorType;
134 
136  typedef std::vector<IndexVectorType> VectorOfIndexVectorType;
137  typedef std::vector<float> DirectionVectorType;
138  typedef typename DirectionVectorType::iterator DirectionVectorIteratorType;
139 
141  typedef itk::GradientRecursiveGaussianImageFilter<OutputImageType> GradientFilterType;
142  typedef typename GradientFilterType::Pointer GradientFilterPointerType;
143  typedef typename GradientFilterType::OutputImageType GradientOutputImageType;
144 
145  typedef itk::UnaryFunctorImageFilter<GradientOutputImageType, OutputImageType,
148  typedef typename MagnitudeFilterType::Pointer MagnitudeFilterPointerType;
149  typedef typename MagnitudeFilterType::OutputImageType::PixelType MagnitudePixelType;
150  typedef typename MagnitudeFilterType::OutputImageType MagnitudeImageType;
151  typedef typename MagnitudeImageType::Pointer MagnitudeImagePointerType;
152 
153  typedef itk::UnaryFunctorImageFilter<GradientOutputImageType, OutputImageType,
156  typedef typename OrientationFilterType::Pointer OrientationFilterPointerType;
157  typedef typename OrientationFilterType::OutputImageType OutputImageDirType;
158  typedef typename OutputImageDirType::RegionType OutputImageDirRegionType;
159 
163 
165  typedef std::vector<double> RectangleType;
166  typedef typename RectangleType::iterator RectangleIteratorType;
167  typedef std::vector<RectangleType> RectangleListType;
168  typedef typename RectangleListType::iterator RectangleListTypeIterator;
169 
171  using Superclass::SetInput;
172  virtual void SetInput(const InputImageType* input);
173  virtual const InputImageType* GetInput(void);
175 
177  LabelImagePointerType GetMap()
178  {
179  return m_UsedPointImage;
180  }
181  MagnitudeImagePointerType GetGradMod()
182  {
183  return m_MagnitudeFilter->GetOutput();
184  }
185  typename OutputImageDirType::Pointer GetGradOri()
186  {
187  return m_OrientationFilter->GetOutput();
188  }
190 
191 protected:
194  {
195  }
196 
197  void GenerateInputRequestedRegion() override;
198 
200  void GenerateData() override;
201 
206  virtual CoordinateHistogramType SortImageByModulusValue(MagnitudeImagePointerType modulusImage);
207 
209  virtual void LineSegmentDetection(CoordinateHistogramType& CoordinateHistogram);
210 
212  virtual bool IsNotUsed(InputIndexType& index) const;
213 
215  virtual bool IsUsed(InputIndexType& index) const;
216 
218  virtual bool IsNotIni(InputIndexType& index) const;
219 
221  virtual void SetPixelToUsed(InputIndexType index);
222 
224  virtual void SetPixelToNotIni(InputIndexType index);
225 
227  virtual void SetRegionToNotIni(IndexVectorType region);
228 
230  virtual bool GrowRegion(InputIndexType index, IndexVectorType& region, double& regionAngle);
231 
233  virtual bool IsAligned(double Angle, double regionAngle, double prec) const;
234 
236  virtual int ComputeRectangles();
237 
239  virtual RectangleType Region2Rectangle(IndexVectorType region, double regionAngle);
240 
242  virtual double ComputeRegionOrientation(IndexVectorType region, double x, double y, double angleRegion) const;
243 
245  virtual double angle_diff(double a, double b) const;
246 
248  virtual double ComputeRectNFA(const RectangleType& rec) const;
249 
251  virtual double ImproveRectangle(RectangleType& rectangle) const;
252 
254  virtual double NFA(int n, int k, double p, double logNT) const;
255 
257  virtual void CopyRectangle(RectangleType& rDst, RectangleType& rSrc) const;
258 
260  void PrintSelf(std::ostream& os, itk::Indent indent) const override;
261 
262 private:
263  LineSegmentDetector(const Self&) = delete;
264  void operator=(const Self&) = delete;
265 
266  VectorOfIndexVectorType m_RegionList;
267  DirectionVectorType m_DirectionVector;
268  LabelImagePointerType m_UsedPointImage;
269  RectangleListType m_RectangleList;
270 
271  double m_Threshold;
272  double m_Prec;
274  unsigned int m_MinimumRegionSize;
275 
277  GradientFilterPointerType m_GradientFilter;
278 
280  MagnitudeFilterPointerType m_MagnitudeFilter;
281 
283  OrientationFilterPointerType m_OrientationFilter;
284 };
285 } // end namespace otb
286 
287 #ifndef OTB_MANUAL_INSTANTIATION
289 #endif
290 
291 #endif
itk::Size< Monteverdi_DIMENSION > SizeType
Definition: mvdTypes.h:137
itk::Index< Monteverdi_DIMENSION > IndexType
Definition: mvdTypes.h:133
GradientFilterPointerType m_GradientFilter
Image< TPrecision, 2 > OutputImageType
std::vector< IndexVectorType > VectorOfIndexVectorType
OutputImageType::PixelType OutputPixelType
RectangleType::iterator RectangleIteratorType
MagnitudeFilterType::Pointer MagnitudeFilterPointerType
This class implement a PolyLineParametricPath for which a value can be set. The value is stored in th...
DataNodeType::PointType PointType
Definition: otbVectorData.h:82
TOutputPixel operator()(const TInputPixel &input)
VectorOfIndexVectorType m_RegionList
Superclass::IndexType IndexType
Definition: otbImage.h:142
itk::UnaryFunctorImageFilter< GradientOutputImageType, OutputImageType, Functor::OrientationFunctor< typename GradientOutputImageType::PixelType, TPrecision > > OrientationFilterType
itk::GradientRecursiveGaussianImageFilter< OutputImageType > GradientFilterType
This class represents a node of data in a vector data hierarchy.
Definition: otbDataNode.h:73
LabelImagePointerType GetMap()
VectorDataType::PointType PointType
VectorDataSource< VectorData< TPrecision > > Superclass
OutputImageType::IndexType OutputIndexType
Filter hierarchy for generating VectorData.
OutputImageDirType::RegionType OutputImageDirRegionType
itk::SmartPointer< const Self > ConstPointer
MagnitudeFilterType::OutputImageType::PixelType MagnitudePixelType
InputImageType::RegionType RegionType
DirectionVectorType::iterator DirectionVectorIteratorType
VectorDataType::LineType LineType
VectorDataType::DataNodeType DataNodeType
MagnitudeImageType::Pointer MagnitudeImagePointerType
InputImageType::PointType OriginType
std::vector< float > DirectionVectorType
TOutputPixel operator()(const TInputPixel &input)
MagnitudeImagePointerType GetGradMod()
Creation of an "otb" image which contains metadata.
Definition: otbImage.h:89
RectangleListType::iterator RectangleListTypeIterator
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
OrientationFilterType::OutputImageType OutputImageDirType
std::vector< RectangleType > RectangleListType
std::vector< OutputIndexType > IndexVectorType
OrientationFilterPointerType m_OrientationFilter
InputImageType::PixelType InputPixelType
itk::UnaryFunctorImageFilter< GradientOutputImageType, OutputImageType, Functor::MagnitudeFunctor< typename GradientOutputImageType::PixelType, TPrecision > > MagnitudeFilterType
CoordinateHistogramType::iterator CoordinateHistogramIteratorType
DirectionVectorType m_DirectionVector
this class implement a fast line detector with false detection control using the a contrario method ...
std::vector< IndexVectorType > CoordinateHistogramType
OrientationFilterType::Pointer OrientationFilterPointerType
This functor computes the magnitude of a covariant vector.
LabelImageType::Pointer LabelImagePointerType
This functor computes the orientation of a cavariant vector Orientation values lies between 0 and 2*...
This class represents a hierarchy of vector data.
Definition: otbVectorData.h:58
MagnitudeFilterType::OutputImageType MagnitudeImageType
InputImageType::IndexType InputIndexType
IndexVectorType::iterator IndexVectorIteratorType
InputImageType::SizeType SizeType
GradientFilterType::OutputImageType GradientOutputImageType
GradientFilterType::Pointer GradientFilterPointerType
VectorData< TPrecision > VectorDataType
itk::SmartPointer< Self > Pointer
Superclass::PixelType PixelType
Definition: otbImage.h:107
InputImageType::SpacingType SpacingType
VectorImageType::SpacingType SpacingType
Definition: mvdTypes.h:175
std::vector< double > RectangleType
itk::SmartPointer< Self > Pointer
Definition: otbImage.h:95
VectorImageType::PointType PointType
Definition: mvdTypes.h:183
OutputImageDirType::Pointer GetGradOri()
OutputImageType::SizeType OutputSizeType
otb::Image< unsigned char, 2 > LabelImageType
LabelImagePointerType m_UsedPointImage
LineType::VertexType VertexType
Superclass::SizeType SizeType
Definition: otbImage.h:148
MagnitudeFilterPointerType m_MagnitudeFilter