OTB  9.0.0
Orfeo Toolbox
otbLabelObjectOpeningMuParserFilter.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 otbLabelObjectOpeningMuParserFilter_hxx
22 #define otbLabelObjectOpeningMuParserFilter_hxx
23 
25 #include <iostream>
26 #include <string>
27 
28 #include "itkImageRegionIterator.h"
29 #include "itkNumericTraits.h"
30 
31 namespace otb
32 {
33 
34 // constructor
35 template <class TImage, class TFunction>
37 {
38  // create the output image for the removed objects
39  this->SetNumberOfRequiredOutputs(2);
40  this->SetNthOutput(1, static_cast<TImage*>(this->MakeOutput(1).GetPointer()));
41 }
42 
43 // Destructor
44 template <class TImage, class TFunction>
46 {
47 }
48 
49 template <class TImage, class TFunction>
50 void LabelObjectOpeningMuParserFilter<TImage, TFunction>::PrintSelf(std::ostream& os, itk::Indent indent) const
51 {
52  Superclass::PrintSelf(os, indent);
53  os << indent << "Expression: " << m_Expression << std::endl;
54 }
55 
56 template <class TImage, class TFunction>
58 {
59  if (m_Expression != expression)
60  m_Expression = expression;
61  m_Functor.SetExpression(m_Expression);
62  this->Modified();
63 }
64 
65 template <class TImage, class TFunction>
67 {
68  return m_Expression;
69 }
70 
71 template <class TImage, class TFunction>
72 const std::map<std::string, double*>& LabelObjectOpeningMuParserFilter<TImage, TFunction>::GetVar() const
73 {
74  return this->m_Functor.GetVar();
75 }
76 
77 template <class TImage, class TFunction>
79 {
80  return this->m_Functor.GetFunList();
81 }
82 
83 template <class TImage, class TFunction>
85 {
86  const std::map<std::string, double*>& variables = this->m_Functor.GetVar();
87 
88  // Get the number of variables
89  std::map<std::string, double*>::const_iterator item = variables.begin();
90 
91  // Query the variables
92  for (; item != variables.end(); ++item)
93  {
94  std::cout << "Name: " << item->first << " Address: [0x" << item->second << "]\n";
95  }
96 }
97 
98 template <class TImage, class TFunction>
100 {
101  return this->m_Functor.CheckExpression();
102 }
103 
104 template <class TImage, class TFunction>
105 void LabelObjectOpeningMuParserFilter<TImage, TFunction>::SetAttributes(std::vector<std::string> shapeAttributes, std::vector<std::string> statAttributes,
106  int nbOfBands)
107 {
108  this->m_Functor.SetAttributes(shapeAttributes, statAttributes, nbOfBands);
109 }
110 
112 template <class TImage, class TFunction>
114 {
115  // if told to run in place and the types support it,
116  if (this->GetInPlace() && this->CanRunInPlace())
117  {
118  // Graft this first input to the output. Later, we'll need to
119  // remove the input's hold on the bulk data.
120  //
121  ImagePointer inputAsOutput = dynamic_cast<TImage*>(const_cast<TImage*>(this->GetInput()));
122 
123  if (inputAsOutput)
124  {
125 
126  this->GraftOutput(inputAsOutput);
127  this->GetOutput()->SetLargestPossibleRegion(this->GetOutput()->GetLargestPossibleRegion());
128  this->GetOutput()->SetRequestedRegion(this->GetOutput()->GetRequestedRegion());
129  this->GetOutput()->SetBufferedRegion(this->GetOutput()->GetBufferedRegion());
130  }
131 
132  // If there are more than one outputs, allocate the remaining outputs
133  for (unsigned int i = 1; i < this->GetNumberOfOutputs(); ++i)
134  {
135  ImagePointer outputPtr;
136 
137  outputPtr = this->GetOutput(i);
138  outputPtr->SetBufferedRegion(outputPtr->GetRequestedRegion());
139  outputPtr->Allocate();
140  }
141  }
142  else
143  {
144  Superclass::AllocateOutputs();
145  // copy the content of the input image to the output image (will be done by ImageSource AllocateOutputs Method)
146  // would never occur : inputasoutput condition is always true, since output and input type is TImage for
147  // LabelObjectOpeningMuParserFilter class
148  }
149 }
150 
151 template <class TImage, class TFunction>
153 {
154  itk::ImageToImageFilter<TImage, TImage>::GenerateInputRequestedRegion();
155 }
156 
157 template <class TImage, class TFunction>
159 {
160 
161  ImageConstPointer inputPtr = this->GetInput();
162  // Allocate the output
163  this->AllocateOutputs();
164 
165  ImageType* output = this->GetOutput();
166  ImageType* output2 = this->GetOutput(1);
167  assert(this->GetNumberOfOutputs() == 2);
168  assert(output2 != NULL);
169 
170  // set the background value for the second output - this is not done in the superclasses
171  output2->SetBackgroundValue(output->GetBackgroundValue());
172 
173  itk::ProgressReporter progress(this, 0, output->GetNumberOfLabelObjects());
174  typename ImageType::Iterator it(output);
175 
176  while (!it.IsAtEnd())
177  {
178  typename LabelObjectType::LabelType label = it.GetLabel();
179  LabelObjectType* labelObject = it.GetLabelObject();
180 
181  if (!m_Functor(*labelObject))
182  {
183  // must increment the iterator before removing the object to avoid invalidating the iterator
184  ++it;
185  output2->AddLabelObject(labelObject);
186  output->RemoveLabel(label);
187  }
188  else
189  {
190  ++it;
191  }
192 
193  progress.CompletedPixel();
194  }
195 }
196 
197 } // end namespace otb
198 
199 #endif
otb::LabelObjectOpeningMuParserFilter::DisplayVar
void DisplayVar() const
Definition: otbLabelObjectOpeningMuParserFilter.hxx:84
otb::LabelObjectOpeningMuParserFilter::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbLabelObjectOpeningMuParserFilter.hxx:50
otb::LabelObjectOpeningMuParserFilter::CheckExpression
bool CheckExpression()
Definition: otbLabelObjectOpeningMuParserFilter.hxx:99
otb::LabelObjectOpeningMuParserFilter::GetVar
const std::map< std::string, double * > & GetVar() const
Definition: otbLabelObjectOpeningMuParserFilter.hxx:72
otb::LabelObjectOpeningMuParserFilter::~LabelObjectOpeningMuParserFilter
~LabelObjectOpeningMuParserFilter() override
Definition: otbLabelObjectOpeningMuParserFilter.hxx:45
otb::LabelObjectOpeningMuParserFilter::AllocateOutputs
void AllocateOutputs() override
Definition: otbLabelObjectOpeningMuParserFilter.hxx:113
otb::LabelObjectOpeningMuParserFilter::ImageConstPointer
ImageType::ConstPointer ImageConstPointer
Definition: otbLabelObjectOpeningMuParserFilter.h:74
otb::LabelObjectOpeningMuParserFilter::GetFunList
Parser::FunctionMapType GetFunList() const
Definition: otbLabelObjectOpeningMuParserFilter.hxx:78
otb::LabelObjectOpeningMuParserFilter::GenerateInputRequestedRegion
void GenerateInputRequestedRegion() override
Definition: otbLabelObjectOpeningMuParserFilter.hxx:152
otb::Parser::FunctionMapType
std::map< std::string, int > FunctionMapType
Definition: otbParser.h:64
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::LabelObjectOpeningMuParserFilter::ImageType
TImage ImageType
Definition: otbLabelObjectOpeningMuParserFilter.h:72
otb::LabelObjectOpeningMuParserFilter::LabelObjectOpeningMuParserFilter
LabelObjectOpeningMuParserFilter()
Definition: otbLabelObjectOpeningMuParserFilter.hxx:36
otb::LabelObjectOpeningMuParserFilter::SetExpression
void SetExpression(const std::string expression)
Definition: otbLabelObjectOpeningMuParserFilter.hxx:57
otb::LabelObjectOpeningMuParserFilter::SetAttributes
void SetAttributes(std::vector< std::string > shapeAttributes, std::vector< std::string > statAttributes, int nbOfBands)
Definition: otbLabelObjectOpeningMuParserFilter.hxx:105
otb::LabelObjectOpeningMuParserFilter::GenerateData
void GenerateData() override
Definition: otbLabelObjectOpeningMuParserFilter.hxx:158
otbLabelObjectOpeningMuParserFilter.h
otb::LabelObjectOpeningMuParserFilter::ImagePointer
ImageType::Pointer ImagePointer
Definition: otbLabelObjectOpeningMuParserFilter.h:73
otb::LabelObjectOpeningMuParserFilter::GetExpression
std::string GetExpression() const
Definition: otbLabelObjectOpeningMuParserFilter.hxx:66
otb::LabelObjectOpeningMuParserFilter::LabelObjectType
ImageType::LabelObjectType LabelObjectType
Definition: otbLabelObjectOpeningMuParserFilter.h:78