OTB  9.0.0
Orfeo Toolbox
otbLineSegmentDetector.h
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 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 
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 
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 
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
otb::LineSegmentDetector::m_MagnitudeFilter
MagnitudeFilterPointerType m_MagnitudeFilter
Definition: otbLineSegmentDetector.h:280
otb::LineSegmentDetector::GradientOutputImageType
GradientFilterType::OutputImageType GradientOutputImageType
Definition: otbLineSegmentDetector.h:143
otb::LineSegmentDetector::DataNodeType
VectorDataType::DataNodeType DataNodeType
Definition: otbLineSegmentDetector.h:118
otb::LineSegmentDetector::m_DirectionVector
DirectionVectorType m_DirectionVector
Definition: otbLineSegmentDetector.h:267
otb::LineSegmentDetector::OrientationFilterPointerType
OrientationFilterType::Pointer OrientationFilterPointerType
Definition: otbLineSegmentDetector.h:156
otb::LineSegmentDetector::OrientationFilterType
itk::UnaryFunctorImageFilter< GradientOutputImageType, OutputImageType, Functor::OrientationFunctor< typename GradientOutputImageType::PixelType, TPrecision > > OrientationFilterType
Definition: otbLineSegmentDetector.h:155
otb::LineSegmentDetector::m_OrientationFilter
OrientationFilterPointerType m_OrientationFilter
Definition: otbLineSegmentDetector.h:283
otb::LineSegmentDetector::RegionType
InputImageType::RegionType RegionType
Definition: otbLineSegmentDetector.h:112
otb::LineSegmentDetector::GetGradOri
OutputImageDirType::Pointer GetGradOri()
Definition: otbLineSegmentDetector.h:185
otb::LineSegmentDetector::PointType
VectorDataType::PointType PointType
Definition: otbLineSegmentDetector.h:120
otb::Functor::OrientationFunctor
This functor computes the orientation of a cavariant vector Orientation values lies between 0 and 2*...
Definition: otbImageToSIFTKeyPointSetFilter.h:69
otb::LineSegmentDetector::RectangleListTypeIterator
RectangleListType::iterator RectangleListTypeIterator
Definition: otbLineSegmentDetector.h:168
otbImage.h
otb::LineSegmentDetector::Superclass
VectorDataSource< VectorData< TPrecision > > Superclass
Definition: otbLineSegmentDetector.h:97
otb::LineSegmentDetector::MagnitudeImagePointerType
MagnitudeImageType::Pointer MagnitudeImagePointerType
Definition: otbLineSegmentDetector.h:151
otb::LineSegmentDetector::m_Prec
double m_Prec
Definition: otbLineSegmentDetector.h:272
otb::Functor::MagnitudeFunctor::operator()
TOutputPixel operator()(const TInputPixel &input)
Definition: otbLineSegmentDetector.h:46
otb::LineSegmentDetector::InputImageType
TInputImage InputImageType
Definition: otbLineSegmentDetector.h:105
otb::LineSegmentDetector::DirectionVectorType
std::vector< float > DirectionVectorType
Definition: otbLineSegmentDetector.h:137
otbLineSegmentDetector.hxx
otb::LineSegmentDetector::CoordinateHistogramIteratorType
CoordinateHistogramType::iterator CoordinateHistogramIteratorType
Definition: otbLineSegmentDetector.h:133
otb::LineSegmentDetector
this class implement a fast line detector with false detection control using the a contrario method
Definition: otbLineSegmentDetector.h:92
otb::LineSegmentDetector::MagnitudeImageType
MagnitudeFilterType::OutputImageType MagnitudeImageType
Definition: otbLineSegmentDetector.h:150
otb::VectorData
This class represents a hierarchy of vector data.
Definition: otbVectorData.h:58
otb::LineSegmentDetector::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbLineSegmentDetector.h:98
otb::Image::IndexType
Superclass::IndexType IndexType
Definition: otbImage.h:142
otb::DataNode
This class represents a node of data in a vector data hierarchy.
Definition: otbDataNode.h:73
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::LineSegmentDetector::GradientFilterType
itk::GradientRecursiveGaussianImageFilter< OutputImageType > GradientFilterType
Definition: otbLineSegmentDetector.h:141
otb::Image
Creation of an "otb" image which contains metadata.
Definition: otbImage.h:89
otb::LineSegmentDetector::OutputIndexType
OutputImageType::IndexType OutputIndexType
Definition: otbLineSegmentDetector.h:126
otb::LineSegmentDetector::GetGradMod
MagnitudeImagePointerType GetGradMod()
Definition: otbLineSegmentDetector.h:181
otb::LineSegmentDetector::LabelImageType
otb::Image< unsigned char, 2 > LabelImageType
Definition: otbLineSegmentDetector.h:161
otb::LineSegmentDetector::SizeType
InputImageType::SizeType SizeType
Definition: otbLineSegmentDetector.h:111
otb::LineSegmentDetector::m_MinimumRegionSize
unsigned int m_MinimumRegionSize
Definition: otbLineSegmentDetector.h:274
otb::LineSegmentDetector::OutputImageType
Image< TPrecision, 2 > OutputImageType
Definition: otbLineSegmentDetector.h:124
otb::LineSegmentDetector::SpacingType
InputImageType::SpacingType SpacingType
Definition: otbLineSegmentDetector.h:113
otb::LineSegmentDetector::VectorOfIndexVectorType
std::vector< IndexVectorType > VectorOfIndexVectorType
Definition: otbLineSegmentDetector.h:136
otb::LineSegmentDetector::LabelImagePointerType
LabelImageType::Pointer LabelImagePointerType
Definition: otbLineSegmentDetector.h:162
otb::LineSegmentDetector::RectangleListType
std::vector< RectangleType > RectangleListType
Definition: otbLineSegmentDetector.h:167
otb::LineSegmentDetector::OutputSizeType
OutputImageType::SizeType OutputSizeType
Definition: otbLineSegmentDetector.h:127
otb::LineSegmentDetector::InputPixelType
InputImageType::PixelType InputPixelType
Definition: otbLineSegmentDetector.h:109
otb::LineSegmentDetector::OriginType
InputImageType::PointType OriginType
Definition: otbLineSegmentDetector.h:114
otb::LineSegmentDetector::GradientFilterPointerType
GradientFilterType::Pointer GradientFilterPointerType
Definition: otbLineSegmentDetector.h:142
otb::LineSegmentDetector::VertexType
LineType::VertexType VertexType
Definition: otbLineSegmentDetector.h:121
otb::LineSegmentDetector::VectorDataType
VectorData< TPrecision > VectorDataType
Definition: otbLineSegmentDetector.h:117
otb::LineSegmentDetector::Self
LineSegmentDetector Self
Definition: otbLineSegmentDetector.h:96
otb::LineSegmentDetector::MagnitudeFilterType
itk::UnaryFunctorImageFilter< GradientOutputImageType, OutputImageType, Functor::MagnitudeFunctor< typename GradientOutputImageType::PixelType, TPrecision > > MagnitudeFilterType
Definition: otbLineSegmentDetector.h:147
otb::LineSegmentDetector::m_Threshold
double m_Threshold
Definition: otbLineSegmentDetector.h:271
otb::VectorData::PointType
DataNodeType::PointType PointType
Definition: otbVectorData.h:82
otb::LineSegmentDetector::m_RegionList
VectorOfIndexVectorType m_RegionList
Definition: otbLineSegmentDetector.h:266
otb::LineSegmentDetector::RectangleIteratorType
RectangleType::iterator RectangleIteratorType
Definition: otbLineSegmentDetector.h:166
otbVectorDataSource.h
otb::LineSegmentDetector::m_UsedPointImage
LabelImagePointerType m_UsedPointImage
Definition: otbLineSegmentDetector.h:268
otb::LineSegmentDetector::m_GradientFilter
GradientFilterPointerType m_GradientFilter
Definition: otbLineSegmentDetector.h:277
otb::LineSegmentDetector::LineType
VectorDataType::LineType LineType
Definition: otbLineSegmentDetector.h:119
otb::LineSegmentDetector::InputIndexType
InputImageType::IndexType InputIndexType
Definition: otbLineSegmentDetector.h:110
otb::LineSegmentDetector::m_DirectionsAllowed
double m_DirectionsAllowed
Definition: otbLineSegmentDetector.h:273
otb::PolyLineParametricPathWithValue
This class implement a PolyLineParametricPath for which a value can be set. The value is stored in th...
Definition: otbPolyLineParametricPathWithValue.h:50
otb::LineSegmentDetector::DirectionVectorIteratorType
DirectionVectorType::iterator DirectionVectorIteratorType
Definition: otbLineSegmentDetector.h:138
otb::Functor::OrientationFunctor::operator()
TOutputPixel operator()(const TInputPixel &input)
Definition: otbLineSegmentDetector.h:62
otbVectorData.h
otb::Functor::MagnitudeFunctor
This functor computes the magnitude of a covariant vector.
Definition: otbImageToSIFTKeyPointSetFilter.h:53
otb::LineSegmentDetector::OutputPixelType
OutputImageType::PixelType OutputPixelType
Definition: otbLineSegmentDetector.h:125
otb::LineSegmentDetector::~LineSegmentDetector
~LineSegmentDetector() override
Definition: otbLineSegmentDetector.h:193
otb::LineSegmentDetector::OutputImageDirType
OrientationFilterType::OutputImageType OutputImageDirType
Definition: otbLineSegmentDetector.h:157
otb::PolyLineParametricPathWithValue::VertexType
Superclass::VertexType VertexType
Definition: otbPolyLineParametricPathWithValue.h:69
otb::LineSegmentDetector::OutputImageDirRegionType
OutputImageDirType::RegionType OutputImageDirRegionType
Definition: otbLineSegmentDetector.h:158
otb::LineSegmentDetector::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: otbLineSegmentDetector.h:99
otb::LineSegmentDetector::m_RectangleList
RectangleListType m_RectangleList
Definition: otbLineSegmentDetector.h:269
otb::Image::PixelType
Superclass::PixelType PixelType
Definition: otbImage.h:107
otb::VectorDataSource
Filter hierarchy for generating VectorData.
Definition: otbVectorDataSource.h:42
otb::Image::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbImage.h:97
otb::LineSegmentDetector::MagnitudePixelType
MagnitudeFilterType::OutputImageType::PixelType MagnitudePixelType
Definition: otbLineSegmentDetector.h:149
otb::LineSegmentDetector::RectangleType
std::vector< double > RectangleType
Definition: otbLineSegmentDetector.h:165
otb::LineSegmentDetector::IndexVectorType
std::vector< OutputIndexType > IndexVectorType
Definition: otbLineSegmentDetector.h:130
otb::LineSegmentDetector::CoordinateHistogramType
std::vector< IndexVectorType > CoordinateHistogramType
Definition: otbLineSegmentDetector.h:132
otb::LineSegmentDetector::IndexVectorIteratorType
IndexVectorType::iterator IndexVectorIteratorType
Definition: otbLineSegmentDetector.h:131
otb::LineSegmentDetector::MagnitudeFilterPointerType
MagnitudeFilterType::Pointer MagnitudeFilterPointerType
Definition: otbLineSegmentDetector.h:148
otb::LineSegmentDetector::GetMap
LabelImagePointerType GetMap()
Definition: otbLineSegmentDetector.h:177
otb::Image::SizeType
Superclass::SizeType SizeType
Definition: otbImage.h:148