OTB  9.0.0
Orfeo Toolbox
otbMeanShiftConnectedComponentSegmentationFilter.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 otbMeanShiftConnectedComponentSegmentationFilter_hxx
22 #define otbMeanShiftConnectedComponentSegmentationFilter_hxx
23 
25 #include "itkExtractImageFilter.h"
26 
27 namespace otb
28 {
29 
30 template <class TVImage, class TMaskImage, class TLabelImage>
32 {
33  m_MinimumObjectSize = 2;
34  m_MeanShiftFilter = MeanShiftFilterType::New();
35 }
36 
37 template <class TVImage, class TMaskImage, class TLabelImage>
39 {
40 }
41 
42 template <class TVImage, class TMaskImage, class TLabelImage>
44 {
45  Superclass::GenerateInputRequestedRegion();
46 }
47 
48 template <class TVImage, class TMaskImage, class TLabelImage>
50 {
51  // Apply an ExtractImageFilter to avoid problems with filters asking for the LargestPossibleRegion
52  typedef itk::ExtractImageFilter<TVImage, TVImage> ExtractImageFilterType;
53  typename ExtractImageFilterType::Pointer extract = ExtractImageFilterType::New();
54  extract->SetInput(this->GetInput());
55  extract->SetExtractionRegion(this->GetOutput()->GetRequestedRegion());
56  // WARNING: itk::ExtractImageFilter does not copy the MetadataDictionary
57 
58  // meanshift filtering
59 
60  // not necessary
61  /* if (this->m_MeanShiftFilter.IsNotNull())
62  {
63  itkExceptionMacro("MeanShiftFilter is null");
64  }*/
65  this->m_MeanShiftFilter->SetInput(extract->GetOutput());
66 
67  // this->m_MeanShiftFilter->SetInput(this->GetInput());
68 
69 
70  typename MaskImageType::Pointer mask;
71  if (!m_MaskExpression.empty())
72  {
73  // Compute the mask
74  typename MaskMuParserFilterType::Pointer maskFilter;
75  maskFilter = MaskMuParserFilterType::New();
76  maskFilter->SetInput(this->m_MeanShiftFilter->GetOutput());
77  maskFilter->SetExpression(m_MaskExpression);
78  maskFilter->Update();
79  mask = maskFilter->GetOutput();
80  }
81 
82  // Perform connected components segmentation
83  typename ConnectedComponentFilterType::Pointer connected = ConnectedComponentFilterType::New();
84  connected->SetInput(this->m_MeanShiftFilter->GetOutput());
85 
86  if (mask.IsNotNull())
87  connected->SetMaskImage(mask);
88  connected->GetFunctor().SetExpression(m_ConnectedComponentExpression);
89  connected->Update();
90 
91  // Relabel connected component output
92  typename RelabelComponentFilterType::Pointer relabel = RelabelComponentFilterType::New();
93  relabel->SetInput(connected->GetOutput());
94  relabel->SetMinimumObjectSize(m_MinimumObjectSize);
95  relabel->Update();
96 
97  this->GraftOutput(relabel->GetOutput());
98 }
99 
100 } // end namespace otb
101 #endif
otbMeanShiftConnectedComponentSegmentationFilter.h
otb::MeanShiftConnectedComponentSegmentationFilter::GenerateInputRequestedRegion
void GenerateInputRequestedRegion() override
Definition: otbMeanShiftConnectedComponentSegmentationFilter.hxx:43
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::MeanShiftConnectedComponentSegmentationFilter::MeanShiftConnectedComponentSegmentationFilter
MeanShiftConnectedComponentSegmentationFilter()
Definition: otbMeanShiftConnectedComponentSegmentationFilter.hxx:31
otb::MeanShiftConnectedComponentSegmentationFilter::~MeanShiftConnectedComponentSegmentationFilter
~MeanShiftConnectedComponentSegmentationFilter() override
Definition: otbMeanShiftConnectedComponentSegmentationFilter.hxx:38
otb::MeanShiftConnectedComponentSegmentationFilter::GenerateData
void GenerateData() override
Definition: otbMeanShiftConnectedComponentSegmentationFilter.hxx:49
otb::MaskMuParserFilter::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbMaskMuParserFilter.h:79