OTB  9.0.0
Orfeo Toolbox
otbStreamingConnectedComponentSegmentationOBIAToVectorDataFilter.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 otbStreamingConnectedComponentSegmentationOBIAToVectorDataFilter_hxx
22 #define otbStreamingConnectedComponentSegmentationOBIAToVectorDataFilter_hxx
23 
26 #include "itkAffineTransform.h"
27 
28 namespace otb
29 {
30 
31 template <class TVImage, class TLabelImage, class TMaskImage, class TOutputVectorData>
32 PersistentConnectedComponentSegmentationOBIAToVectorDataFilter<TVImage, TLabelImage, TMaskImage,
34  : m_MinimumObjectSize(2),
35  m_ShapeReducedSetOfAttributes(false),
36  m_StatsReducedSetOfAttributes(false),
37  m_ComputeFlusser(false),
38  m_ComputePolygon(false),
39  m_ComputeFeretDiameter(false),
40  m_ComputePerimeter(false)
41 {
42 }
43 
44 template <class TVImage, class TLabelImage, class TMaskImage, class TOutputVectorData>
46  TOutputVectorData>::~PersistentConnectedComponentSegmentationOBIAToVectorDataFilter()
47 {
48 }
49 
50 template <class TVImage, class TLabelImage, class TMaskImage, class TOutputVectorData>
52 {
53  Superclass::GenerateInputRequestedRegion();
54 }
55 
56 template <class TVImage, class TLabelImage, class TMaskImage, class TOutputVectorData>
59 {
60  // Apply an ExtractImageFilter to avoid problems with filters asking for the LargestPossibleRegion
61  typename ExtractImageFilterType::Pointer extract = ExtractImageFilterType::New();
62  extract->SetInput(this->GetInput());
63  extract->SetExtractionRegion(this->GetOutput()->GetRequestedRegion());
64  // WARNING: itk::ExtractImageFilter does not copy the MetadataDictionary
65 
66  typename MaskImageType::Pointer mask;
67  if (!m_MaskExpression.empty())
68  {
69  // Compute the mask
70  typename MaskMuParserFilterType::Pointer maskFilter;
71  maskFilter = MaskMuParserFilterType::New();
72  maskFilter->SetInput(extract->GetOutput());
73  maskFilter->SetExpression(m_MaskExpression);
74  maskFilter->Update();
75  mask = maskFilter->GetOutput();
76  }
77 
78  // Perform connected components segmentation
79  typename ConnectedComponentFilterType::Pointer connected = ConnectedComponentFilterType::New();
80  connected->SetInput(extract->GetOutput());
81 
82  if (mask.IsNotNull())
83  connected->SetMaskImage(mask);
84  connected->GetFunctor().SetExpression(m_ConnectedComponentExpression);
85  connected->Update();
86 
87  // Relabel connected component output
88  typename RelabelComponentFilterType::Pointer relabel = RelabelComponentFilterType::New();
89  relabel->SetInput(connected->GetOutput());
90  relabel->SetMinimumObjectSize(m_MinimumObjectSize);
91  relabel->Update();
92 
93  // Attributes computation
94  // LabelImage to Label Map transformation
95  typename LabelImageToLabelMapFilterType::Pointer labelImageToLabelMap = LabelImageToLabelMapFilterType::New();
96  labelImageToLabelMap->SetInput(relabel->GetOutput());
97  labelImageToLabelMap->SetBackgroundValue(0);
98  labelImageToLabelMap->Update();
99 
100  typename AttributesLabelMapType::Pointer labelMap = labelImageToLabelMap->GetOutput();
101 
102  if (!m_OBIAExpression.empty())
103  {
104  // shape attributes computation
105  typename ShapeLabelMapFilterType::Pointer shapeLabelMapFilter = ShapeLabelMapFilterType::New();
106  shapeLabelMapFilter->SetInput(labelImageToLabelMap->GetOutput());
107  shapeLabelMapFilter->SetReducedAttributeSet(m_ShapeReducedSetOfAttributes);
108  shapeLabelMapFilter->SetComputePolygon(m_ComputePolygon);
109  shapeLabelMapFilter->SetComputePerimeter(m_ComputePerimeter);
110  shapeLabelMapFilter->SetComputeFeretDiameter(m_ComputeFeretDiameter);
111  shapeLabelMapFilter->SetComputeFlusser(m_ComputeFlusser);
112 
113  // band stat attributes computation
114  typename RadiometricLabelMapFilterType::Pointer radiometricLabelMapFilter = RadiometricLabelMapFilterType::New();
115  radiometricLabelMapFilter->SetInput(shapeLabelMapFilter->GetOutput());
116  radiometricLabelMapFilter->SetFeatureImage(extract->GetOutput());
117  radiometricLabelMapFilter->SetReducedAttributeSet(m_StatsReducedSetOfAttributes);
118 
119  // OBIA Filtering using shape and radiometric object characteristics
120  typename LabelObjectOpeningFilterType::Pointer opening = LabelObjectOpeningFilterType::New();
121  opening->SetExpression(m_OBIAExpression);
122  opening->SetInput(radiometricLabelMapFilter->GetOutput());
123  opening->Update();
124 
125  labelMap = opening->GetOutput();
126  }
127 
128  // Transformation to VectorData
129  typename LabelMapToVectorDataFilterType::Pointer labelMapToVectorDataFilter = LabelMapToVectorDataFilterType::New();
130  labelMapToVectorDataFilter->SetInput(labelMap);
131  labelMapToVectorDataFilter->Update();
132 
133  // The VectorData in output of the chain is in image index coordinate,
134  // and the projection information is lost
135  // Apply an affine transform to apply image origin and spacing,
136  // and arbitrarily set the ProjectionRef to the input image ProjectionRef
137 
138  typedef itk::AffineTransform<typename VectorDataType::PrecisionType, 2> TransformType;
140 
141  typename TransformType::ParametersType params;
142  params.SetSize(6);
143  params[0] = this->GetInput()->GetSignedSpacing()[0];
144  params[1] = 0;
145  params[2] = 0;
146  params[3] = this->GetInput()->GetSignedSpacing()[1];
147  params[4] = this->GetInput()->GetOrigin()[0];
148  params[5] = this->GetInput()->GetOrigin()[1];
149 
150  typename TransformType::Pointer transform = TransformType::New();
151  transform->SetParameters(params);
152 
153  typename VDTransformType::Pointer vdTransform = VDTransformType::New();
154  vdTransform->SetTransform(transform);
155  vdTransform->SetInput(labelMapToVectorDataFilter->GetOutput());
156  vdTransform->Update();
157  vdTransform->GetOutput()->SetProjectionRef(this->GetInput()->GetProjectionRef());
158 
159  return vdTransform->GetOutput();
160 }
161 
162 } // end namespace otb
163 #endif
otb::VectorDataTransformFilter
Apply a Transform To a VectorData.
Definition: otbVectorDataTransformFilter.h:44
otb::BandsStatisticsAttributesLabelMapFilter::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbBandsStatisticsAttributesLabelMapFilter.h:159
otb::PersistentConnectedComponentSegmentationOBIAToVectorDataFilter::ProcessTile
VectorDataPointerType ProcessTile() override
Definition: otbStreamingConnectedComponentSegmentationOBIAToVectorDataFilter.hxx:58
otb::LabelImageToLabelMapWithAdjacencyFilter::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbLabelImageToLabelMapWithAdjacencyFilter.h:45
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::LabelObjectOpeningMuParserFilter::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbLabelObjectOpeningMuParserFilter.h:68
otb::PersistentConnectedComponentSegmentationOBIAToVectorDataFilter::VectorDataPointerType
VectorDataType::Pointer VectorDataPointerType
Definition: otbStreamingConnectedComponentSegmentationOBIAToVectorDataFilter.h:79
otbVectorDataTransformFilter.h
otbStreamingConnectedComponentSegmentationOBIAToVectorDataFilter.h
otb::LabelMapWithAdjacency::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbLabelMapWithAdjacency.h:47
otb::PersistentConnectedComponentSegmentationOBIAToVectorDataFilter
[internal] Helper class to perform connected component segmentation on an input image,...
Definition: otbStreamingConnectedComponentSegmentationOBIAToVectorDataFilter.h:64
otb::LabelMapToVectorDataFilter::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbLabelMapToVectorDataFilter.h:64
otb::MaskMuParserFilter::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbMaskMuParserFilter.h:79
otb::ShapeAttributesLabelMapFilter::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbShapeAttributesLabelMapFilter.h:226
otb::PersistentConnectedComponentSegmentationOBIAToVectorDataFilter::GenerateInputRequestedRegion
void GenerateInputRequestedRegion() override
Definition: otbStreamingConnectedComponentSegmentationOBIAToVectorDataFilter.hxx:51