OTB  9.0.0
Orfeo Toolbox
otbPrintableImageFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1999-2011 Insight Software Consortium
3  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
4  *
5  * This file is part of Orfeo Toolbox
6  *
7  * https://www.orfeo-toolbox.org/
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 #ifndef otbPrintableImageFilter_hxx
23 #define otbPrintableImageFilter_hxx
24 
26 
27 namespace otb
28 {
29 
30 template <class TInputImage, class TMaskImage>
32 {
33 
34  m_Rescaler = VectorRescalerType::New();
35  m_Extractor = ChannelExtractorType::New();
36 
37  m_Rescaler->SetInput(m_Extractor->GetOutput());
38  m_MaskFilter = FunctorFilterType::New();
39 
40  m_ChannelList = ChannelsType(3, 2);
41  m_ChannelList[1] = 3;
42  m_ChannelList[2] = 4;
43  m_ObjectColor.SetSize(3);
44  m_ObjectColor.Fill(255);
45  m_UseMask = false;
46  m_BackgroundMaskValue = 0;
47 }
48 
49 template <class TInputImage, class TMaskImage>
51 {
52  m_ChannelList.push_back(channel);
53  m_Extractor->SetChannel(channel);
54  this->Modified();
55 }
56 
57 template <class TInputImage, class TMaskImage>
59 {
60  return m_ChannelList;
61 }
62 
63 template <class TInputImage, class TMaskImage>
65 {
66  this->itk::ProcessObject::SetNthInput(1, const_cast<MaskImageType*>(mask));
67  m_UseMask = true;
68 }
69 
70 template <class TInputImage, class TMaskImage>
72 {
73  if (this->GetNumberOfInputs() < 2)
74  {
75  return nullptr;
76  }
77  return static_cast<MaskImageType*>(this->itk::ProcessObject::GetInput(1));
78 }
79 
80 template <class TInputImage, class TMaskImage>
82 {
83  // Call to the superclass implementation
84  Superclass::GenerateOutputInformation();
85 
86  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
87  outputPtr->SetNumberOfComponentsPerPixel(3);
88 }
89 
90 template <class TInputImage, class TMaskImage>
92 {
93  if (m_Extractor->GetNbChannels() == 0 && m_ChannelList.size() > 3)
94  {
95  itkExceptionMacro(<< "Invalid channel list (must be three channels instead of " << m_ChannelList.size());
96  }
97  if (m_Extractor->GetNbChannels() == 0 && m_Extractor->GetNbChannels() > 3)
98  {
99  itkExceptionMacro(<< "Invalid channel list (must be three channels instead of " << m_Extractor->GetNbChannels());
100  }
101  if (m_ObjectColor.GetSize() != 3)
102  {
103  itkExceptionMacro(<< "Invalid m_ObjectColor pixel size");
104  }
105 
106  if (m_UseMask == true)
107  {
108  MaskImagePointerType inputMaskPtr = this->GetInputMask();
109  typename InputImageType::ConstPointer inputPtr = this->GetInput();
110 
111  if (!inputMaskPtr)
112  {
113  itkExceptionMacro(<< "No mask detected");
114  }
115  if (inputMaskPtr->GetLargestPossibleRegion().GetSize() != inputPtr->GetLargestPossibleRegion().GetSize())
116  {
117  itkExceptionMacro(<< "Input size (" << inputPtr->GetLargestPossibleRegion().GetSize() << ") and Mask size ("
118  << inputMaskPtr->GetLargestPossibleRegion().GetSize() << ") must be the same");
119  }
120  }
121 }
122 
123 /*
124  * If no mask used, just rescale the input between 0 and 255.
125  * Else, sur M%askFunctor.
126  */
127 template <class TInputImage, class TMaskImage>
129 {
130  this->BeforeGenerateData();
131 
132  // let this loop to be compliant with previous version of the class where m_ChannelList didn't exist...
133  if (m_Extractor->GetNbChannels() == 0)
134  {
135  for (unsigned int i = 0; i < m_ChannelList.size(); ++i)
136  m_Extractor->SetChannel(m_ChannelList[i]);
137  }
138 
139  m_Extractor->SetInput(this->GetInput());
140  m_Extractor->UpdateOutputInformation();
141 
142  typename TInputImage::PixelType minimum, maximum;
143  minimum.SetSize(m_Extractor->GetNbChannels());
144  maximum.SetSize(m_Extractor->GetNbChannels());
145  minimum.Fill(0);
146  maximum.Fill(255);
147 
148  m_Rescaler->SetOutputMinimum(minimum);
149  m_Rescaler->SetOutputMaximum(maximum);
150  m_Rescaler->SetClampThreshold(0.01);
151 
152  if (m_UseMask == false)
153  {
154  m_Rescaler->GraftOutput(this->GetOutput());
155  m_Rescaler->Update();
156  this->GraftOutput(m_Rescaler->GetOutput());
157  }
158  else
159  {
160  m_MaskFilter->SetInput1(m_Rescaler->GetOutput());
161  m_MaskFilter->SetInput2(this->GetInputMask());
162  m_MaskFilter->GraftOutput(this->GetOutput());
163  m_MaskFilter->Update();
164  this->GraftOutput(m_MaskFilter->GetOutput());
165  }
166 }
167 
168 template <class TInputImage, class TMaskImage>
169 void PrintableImageFilter<TInputImage, TMaskImage>::PrintSelf(std::ostream& os, itk::Indent indent) const
170 {
171  Superclass::PrintSelf(os, indent);
172 }
173 
174 } // end namespace otb
175 
176 #endif
otbPrintableImageFilter.h
otb::PrintableImageFilter::BeforeGenerateData
void BeforeGenerateData()
Definition: otbPrintableImageFilter.hxx:91
otb::PrintableImageFilter::GenerateData
void GenerateData() override
Definition: otbPrintableImageFilter.hxx:128
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::PrintableImageFilter::MaskImageType
TMaskImage MaskImageType
Definition: otbPrintableImageFilter.h:140
otb::PrintableImageFilter::MaskImagePointerType
MaskImageType::Pointer MaskImagePointerType
Definition: otbPrintableImageFilter.h:141
otb::PrintableImageFilter::GenerateOutputInformation
void GenerateOutputInformation() override
Definition: otbPrintableImageFilter.hxx:81
otb::PrintableImageFilter::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbPrintableImageFilter.hxx:169
otb::PrintableImageFilter::ChannelsType
ChannelExtractorType::ChannelsType ChannelsType
Definition: otbPrintableImageFilter.h:145
otb::PrintableImageFilter::SetInputMask
void SetInputMask(const MaskImageType *mask)
Definition: otbPrintableImageFilter.hxx:64
otb::PrintableImageFilter::GetChannels
const ChannelsType GetChannels(void) const
Definition: otbPrintableImageFilter.hxx:58
otb::PrintableImageFilter::SetChannel
void SetChannel(unsigned int channel)
Definition: otbPrintableImageFilter.hxx:50
otb::PrintableImageFilter::PrintableImageFilter
PrintableImageFilter()
Definition: otbPrintableImageFilter.hxx:31
otb::PrintableImageFilter::GetInputMask
MaskImageType * GetInputMask(void)
Definition: otbPrintableImageFilter.hxx:71