OTB  9.0.0
Orfeo Toolbox
otbLabelMapToAttributeImageFilter.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 
22 #ifndef otbLabelMapToAttributeImageFilter_hxx
23 #define otbLabelMapToAttributeImageFilter_hxx
24 
26 #include "otbMacro.h"
27 
28 namespace otb
29 {
30 
32 template <class TInputImage, class TOutputImage, class TAttributeAccessor>
34 {
35  m_ChosenAttributes.resize(0);
36  m_BackgroundValue = static_cast<OutputInternalPixelType>(0);
37 }
39 
40 template <class TInputImage, class TOutputImage, class TAttributeAccessor>
42 {
43  m_ChosenAttributes = newAttributes;
44 }
45 
46 template <class TInputImage, class TOutputImage, class TAttributeAccessor>
48 {
49  return m_ChosenAttributes.size();
50 }
51 
52 template <class TInputImage, class TOutputImage, class TAttributeAccessor>
54 {
55  std::string strAttribute(attribute);
56  if (strAttribute.size() == 0)
57  {
58  otbWarningMacro("Attribute name is empty");
59  return false;
60  }
61 
62  if (channel < GetNumberOfComponentsPerPixel())
63  {
64  m_ChosenAttributes[channel] = strAttribute;
65  }
66  else if (channel == GetNumberOfComponentsPerPixel())
67  {
68  m_ChosenAttributes.push_back(strAttribute);
69  }
70  else
71  {
72  return false;
73  }
74  return true;
75 }
76 
77 template <class TInputImage, class TOutputImage, class TAttributeAccessor>
79 {
80  if (this->GetOutput() && this->GetInput())
81  {
82  this->GetOutput()->CopyInformation(this->GetInput());
83  this->GetOutput()->SetNumberOfComponentsPerPixel(GetNumberOfComponentsPerPixel());
84  }
85 }
86 
87 template <class TInputImage, class TOutputImage, class TAttributeAccessor>
89 {
90  // first call superclass method to initialize label object container and progress
91  Superclass::BeforeThreadedGenerateData();
92 
93  unsigned int nbChannels = GetNumberOfComponentsPerPixel();
94  if (nbChannels == 0)
95  {
96  itkExceptionMacro("No attribute given");
97  }
98 
99  OutputImageType* output = this->GetOutput();
100  OutputPixelType backgroundPixel;
101  backgroundPixel.SetSize(nbChannels);
102  for (unsigned int k = 0; k < nbChannels; k++)
103  {
104  backgroundPixel[k] = m_BackgroundValue;
105  }
106  output->FillBuffer(backgroundPixel);
107 }
108 
109 template <class TInputImage, class TOutputImage, class TAttributeAccessor>
111 {
112  unsigned int nbChannels = GetNumberOfComponentsPerPixel();
113  OutputImageType* output = this->GetOutput();
114  AttributeAccessorType accessor;
115  OutputPixelType outPixel;
116  outPixel.SetSize(nbChannels);
117  for (unsigned int k = 0; k < nbChannels; k++)
118  {
119  accessor.SetAttributeName(m_ChosenAttributes[k].c_str());
120  const AttributeValueType& attribute = accessor(labelObject);
121  outPixel[k] = static_cast<OutputInternalPixelType>(attribute);
122  }
123 
124  ConstLineIteratorType lit = ConstLineIteratorType(labelObject);
125 
126  while (!lit.IsAtEnd())
127  {
128  IndexType idx = lit.GetLine().GetIndex();
129  unsigned long length = lit.GetLine().GetLength();
130  for (unsigned int i = 0; i < length; i++)
131  {
132  output->SetPixel(idx, outPixel);
133  idx[0]++;
134  }
135  ++lit;
136  }
137 }
138 }
139 
140 #endif
otb::LabelMapToAttributeImageFilter::AttributeAccessorType
TAttributeAccessor AttributeAccessorType
Definition: otbLabelMapToAttributeImageFilter.h:72
otbLabelMapToAttributeImageFilter.h
otb::LabelMapToAttributeImageFilter::BeforeThreadedGenerateData
void BeforeThreadedGenerateData() override
Definition: otbLabelMapToAttributeImageFilter.hxx:88
otb::LabelMapToAttributeImageFilter::GetNumberOfComponentsPerPixel
unsigned int GetNumberOfComponentsPerPixel()
Definition: otbLabelMapToAttributeImageFilter.hxx:47
otb::LabelMapToAttributeImageFilter::AttributeListType
std::vector< std::string > AttributeListType
Definition: otbLabelMapToAttributeImageFilter.h:71
otb::LabelMapToAttributeImageFilter::GenerateOutputInformation
void GenerateOutputInformation() override
Definition: otbLabelMapToAttributeImageFilter.hxx:78
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::LabelMapToAttributeImageFilter::OutputImageType
TOutputImage OutputImageType
Definition: otbLabelMapToAttributeImageFilter.h:65
otb::LabelMapToAttributeImageFilter::SetChosenAttributes
void SetChosenAttributes(AttributeListType &newAttributes)
Definition: otbLabelMapToAttributeImageFilter.hxx:41
otbMacro.h
otb::LabelMapToAttributeImageFilter::SetAttributeForNthChannel
bool SetAttributeForNthChannel(unsigned int channel, const char *attribute)
Definition: otbLabelMapToAttributeImageFilter.hxx:53
otbWarningMacro
#define otbWarningMacro(x)
Definition: otbMacro.h:65
otb::LabelMapToAttributeImageFilter::ConstLineIteratorType
LabelObjectType::ConstLineIterator ConstLineIteratorType
Definition: otbLabelMapToAttributeImageFilter.h:62
otb::LabelMapToAttributeImageFilter::AttributeValueType
AttributeAccessorType::AttributeValueType AttributeValueType
Definition: otbLabelMapToAttributeImageFilter.h:73
otb::LabelMapToAttributeImageFilter::OutputPixelType
OutputImageType::PixelType OutputPixelType
Definition: otbLabelMapToAttributeImageFilter.h:66
otb::LabelMapToAttributeImageFilter::LabelObjectType
InputImageType::LabelObjectType LabelObjectType
Definition: otbLabelMapToAttributeImageFilter.h:61
otb::LabelMapToAttributeImageFilter::OutputInternalPixelType
OutputImageType::InternalPixelType OutputInternalPixelType
Definition: otbLabelMapToAttributeImageFilter.h:67
otb::LabelMapToAttributeImageFilter::LabelMapToAttributeImageFilter
LabelMapToAttributeImageFilter()
Definition: otbLabelMapToAttributeImageFilter.hxx:33
otb::LabelMapToAttributeImageFilter::ThreadedProcessLabelObject
void ThreadedProcessLabelObject(LabelObjectType *labelObject) override
Definition: otbLabelMapToAttributeImageFilter.hxx:110
otb::LabelMapToAttributeImageFilter::IndexType
OutputImageType::IndexType IndexType
Definition: otbLabelMapToAttributeImageFilter.h:68