OTB  6.7.0
Orfeo Toolbox
otbLabelMapToAttributeImageFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2019 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>
35 {
36 m_ChosenAttributes.resize(0);
37 m_BackgroundValue = static_cast<OutputInternalPixelType>(0);
38 }
40 
41 template <class TInputImage, class TOutputImage , class TAttributeAccessor>
44 {
45  m_ChosenAttributes = newAttributes;
46 }
47 
48 template <class TInputImage, class TOutputImage , class TAttributeAccessor>
51 {
52  return m_ChosenAttributes.size();
53 }
54 
55 template <class TInputImage, class TOutputImage , class TAttributeAccessor>
57 ::SetAttributeForNthChannel(unsigned int channel, const char *attribute)
58 {
59  std::string strAttribute(attribute);
60  if (strAttribute.size() == 0)
61  {
62  otbWarningMacro("Attribute name is empty");
63  return false;
64  }
65 
66  if( channel<GetNumberOfComponentsPerPixel() )
67  {
68  m_ChosenAttributes[channel] = strAttribute;
69  }
70  else if( channel==GetNumberOfComponentsPerPixel() )
71  {
72  m_ChosenAttributes.push_back(strAttribute);
73  }
74  else
75  {
76  return false;
77  }
78  return true;
79 }
80 
81 template <class TInputImage, class TOutputImage , class TAttributeAccessor>
84 {
85  if (this->GetOutput() && this->GetInput())
86  {
87  this->GetOutput()->CopyInformation(this->GetInput());
88  this->GetOutput()->SetNumberOfComponentsPerPixel(GetNumberOfComponentsPerPixel());
89  }
90 }
91 
92 template <class TInputImage, class TOutputImage , class TAttributeAccessor>
95 {
96  //first call superclass method to initialize label object container and progress
97  Superclass::BeforeThreadedGenerateData();
98 
99  unsigned int nbChannels = GetNumberOfComponentsPerPixel();
100  if (nbChannels == 0)
101  {
102  itkExceptionMacro("No attribute given");
103  }
104 
105  OutputImageType *output = this->GetOutput();
106  OutputPixelType backgroundPixel;
107  backgroundPixel.SetSize(nbChannels);
108  for (unsigned int k=0; k<nbChannels; k++)
109  {
110  backgroundPixel[k] = m_BackgroundValue;
111  }
112  output->FillBuffer( backgroundPixel );
113 }
114 
115 template <class TInputImage, class TOutputImage , class TAttributeAccessor>
118 {
119  unsigned int nbChannels = GetNumberOfComponentsPerPixel();
120  OutputImageType *output = this->GetOutput();
121  AttributeAccessorType accessor;
122  OutputPixelType outPixel;
123  outPixel.SetSize(nbChannels);
124  for (unsigned int k=0; k<nbChannels; k++)
125  {
126  accessor.SetAttributeName(m_ChosenAttributes[k].c_str());
127  const AttributeValueType & attribute = accessor( labelObject );
128  outPixel[k] = static_cast<OutputInternalPixelType>(attribute);
129  }
130 
131  ConstLineIteratorType lit = ConstLineIteratorType ( labelObject );
132 
133  while( !lit.IsAtEnd() )
134  {
135  IndexType idx = lit.GetLine().GetIndex();
136  unsigned long length = lit.GetLine().GetLength();
137  for( unsigned int i=0; i<length; i++)
138  {
139  output->SetPixel( idx, outPixel );
140  idx[0]++;
141  }
142  ++lit;
143  }
144 }
145 
146 }
147 
148 #endif
void SetChosenAttributes(AttributeListType &newAttributes)
bool SetAttributeForNthChannel(unsigned int channel, const char *attribute)
LabelObjectType::ConstLineIterator ConstLineIteratorType
AttributeAccessorType::AttributeValueType AttributeValueType
#define otbWarningMacro(x)
Definition: otbMacro.h:67
void ThreadedProcessLabelObject(LabelObjectType *labelObject) override
InputImageType::LabelObjectType LabelObjectType
OutputImageType::InternalPixelType OutputInternalPixelType
TOutputImage OutputImageType