OTB  10.0.0
Orfeo Toolbox
otbLineSegmentDetector.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2024 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>
59 class OrientationFunctor
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;
110  typedef typename InputImageType::IndexType InputIndexType;
111  typedef typename InputImageType::SizeType SizeType;
112  typedef typename InputImageType::RegionType RegionType;
113  typedef typename InputImageType::SpacingType SpacingType;
114  typedef typename InputImageType::PointType OriginType;
115 
121  typedef typename LineType::VertexType VertexType;
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 
178  {
179  return m_UsedPointImage;
180  }
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 
198 
200  void GenerateData() override;
201 
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 
270 
271  double m_Threshold;
272  double m_Prec;
274  unsigned int m_MinimumRegionSize;
275 
278 
281 
284 };
285 } // end namespace otb
286 
287 #ifndef OTB_MANUAL_INSTANTIATION
289 #endif
290 
291 #endif
This class represents a node of data in a vector data hierarchy.
Definition: otbDataNode.h:74
This functor computes the magnitude of a covariant vector.
TOutputPixel operator()(const TInputPixel &input)
This functor computes the orientation of a cavariant vector Orientation values lies between 0 and 2*...
TOutputPixel operator()(const TInputPixel &input)
Creation of an "otb" image which contains metadata.
Definition: otbImage.h:92
Superclass::PixelType PixelType
Definition: otbImage.h:107
itk::SmartPointer< Self > Pointer
Definition: otbImage.h:97
Superclass::SizeType SizeType
Definition: otbImage.h:148
Superclass::IndexType IndexType
Definition: otbImage.h:142
this class implement a fast line detector with false detection control using the a contrario method
otb::Image< unsigned char, 2 > LabelImageType
InputImageType::PointType OriginType
virtual const InputImageType * GetInput(void)
virtual void SetInput(const InputImageType *input)
DirectionVectorType::iterator DirectionVectorIteratorType
MagnitudeFilterType::OutputImageType::PixelType MagnitudePixelType
IndexVectorType::iterator IndexVectorIteratorType
virtual void SetPixelToNotIni(InputIndexType index)
virtual bool IsNotUsed(InputIndexType &index) const
GradientFilterPointerType m_GradientFilter
itk::SmartPointer< Self > Pointer
VectorOfIndexVectorType m_RegionList
OrientationFilterType::Pointer OrientationFilterPointerType
virtual double ImproveRectangle(RectangleType &rectangle) const
std::vector< IndexVectorType > VectorOfIndexVectorType
virtual bool IsUsed(InputIndexType &index) const
OutputImageType::PixelType OutputPixelType
MagnitudeFilterType::OutputImageType MagnitudeImageType
virtual RectangleType Region2Rectangle(IndexVectorType region, double regionAngle)
MagnitudeImageType::Pointer MagnitudeImagePointerType
InputImageType::IndexType InputIndexType
virtual void SetPixelToUsed(InputIndexType index)
VectorDataType::DataNodeType DataNodeType
RectangleListType::iterator RectangleListTypeIterator
InputImageType::RegionType RegionType
void GenerateData() override
OutputImageType::IndexType OutputIndexType
InputImageType::PixelType InputPixelType
virtual int ComputeRectangles()
DirectionVectorType m_DirectionVector
virtual bool IsNotIni(InputIndexType &index) const
LabelImagePointerType m_UsedPointImage
virtual void CopyRectangle(RectangleType &rDst, RectangleType &rSrc) const
VectorDataType::LineType LineType
GradientFilterType::OutputImageType GradientOutputImageType
void operator=(const Self &)=delete
virtual double angle_diff(double a, double b) const
void GenerateInputRequestedRegion() override
MagnitudeFilterType::Pointer MagnitudeFilterPointerType
RectangleType::iterator RectangleIteratorType
std::vector< double > RectangleType
virtual CoordinateHistogramType SortImageByModulusValue(MagnitudeImagePointerType modulusImage)
itk::UnaryFunctorImageFilter< GradientOutputImageType, OutputImageType, Functor::MagnitudeFunctor< typename GradientOutputImageType::PixelType, TPrecision > > MagnitudeFilterType
LineSegmentDetector(const Self &)=delete
LabelImagePointerType GetMap()
OrientationFilterType::OutputImageType OutputImageDirType
virtual double ComputeRegionOrientation(IndexVectorType region, double x, double y, double angleRegion) const
itk::SmartPointer< const Self > ConstPointer
InputImageType::SpacingType SpacingType
virtual bool IsAligned(double Angle, double regionAngle, double prec) const
GradientFilterType::Pointer GradientFilterPointerType
itk::GradientRecursiveGaussianImageFilter< OutputImageType > GradientFilterType
OutputImageType::SizeType OutputSizeType
OutputImageDirType::RegionType OutputImageDirRegionType
LabelImageType::Pointer LabelImagePointerType
MagnitudeImagePointerType GetGradMod()
Image< TPrecision, 2 > OutputImageType
CoordinateHistogramType::iterator CoordinateHistogramIteratorType
std::vector< IndexVectorType > CoordinateHistogramType
LineType::VertexType VertexType
std::vector< RectangleType > RectangleListType
itk::UnaryFunctorImageFilter< GradientOutputImageType, OutputImageType, Functor::OrientationFunctor< typename GradientOutputImageType::PixelType, TPrecision > > OrientationFilterType
void PrintSelf(std::ostream &os, itk::Indent indent) const override
MagnitudeFilterPointerType m_MagnitudeFilter
virtual void SetRegionToNotIni(IndexVectorType region)
VectorDataSource< VectorData< TPrecision > > Superclass
VectorDataType::PointType PointType
virtual bool GrowRegion(InputIndexType index, IndexVectorType &region, double &regionAngle)
InputImageType::SizeType SizeType
VectorData< TPrecision > VectorDataType
virtual double NFA(int n, int k, double p, double logNT) const
virtual void LineSegmentDetection(CoordinateHistogramType &CoordinateHistogram)
OutputImageDirType::Pointer GetGradOri()
virtual double ComputeRectNFA(const RectangleType &rec) const
OrientationFilterPointerType m_OrientationFilter
std::vector< float > DirectionVectorType
std::vector< OutputIndexType > IndexVectorType
Filter hierarchy for generating VectorData.
This class represents a hierarchy of vector data.
Definition: otbVectorData.h:62
typename DataNodeType::PointType PointType
Definition: otbVectorData.h:82
typename DataNodeType::LineType LineType
Definition: otbVectorData.h:83
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.