OTB  9.0.0
Orfeo Toolbox
otbConcatenateVectorImageFilter.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 otbConcatenateVectorImageFilter_hxx
22 #define otbConcatenateVectorImageFilter_hxx
23 
25 #include "itkImageRegionIterator.h"
26 #include "itkProgressReporter.h"
27 
28 namespace otb
29 {
33 template <class TInputImage1, class TInputImage2, class TOutputImage>
35 {
36  this->SetNumberOfRequiredInputs(2);
37 }
38 
42 template <class TInputImage1, class TInputImage2, class TOutputImage>
44 {
45 }
46 
51 template <class TInputImage1, class TInputImage2, class TOutputImage>
53 {
54  this->SetNthInput(0, const_cast<TInputImage1*>(image));
55 }
56 
61 template <class TInputImage1, class TInputImage2, class TOutputImage>
63 {
64  this->SetNthInput(1, const_cast<TInputImage2*>(image));
65 }
66 
71 template <class TInputImage1, class TInputImage2, class TOutputImage>
74 {
75  return const_cast<InputImage1Type*>(this->GetInput(0));
76 }
77 
82 template <class TInputImage1, class TInputImage2, class TOutputImage>
85 {
86  return const_cast<InputImage2Type*>(this->GetInput(1));
87 }
88 
89 template <class TInputImage1, class TInputImage2, class TOutputImage>
91 {
92  // Call to the superclass implementation
93  Superclass::GenerateOutputInformation();
94 
95  typename Superclass::InputImageConstPointer inputPtr1 = this->GetInput1();
96  typename Superclass::InputImageConstPointer inputPtr2 = this->GetInput2();
97  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
98 
99  unsigned int nbComponentsPerPixel = inputPtr1->GetNumberOfComponentsPerPixel() + inputPtr2->GetNumberOfComponentsPerPixel();
100 
101  // initialize the number of channels of the output image
102  outputPtr->SetNumberOfComponentsPerPixel(nbComponentsPerPixel);
103 }
104 
105 template <class TInputImage1, class TInputImage2, class TOutputImage>
107 {
108  Superclass::BeforeThreadedGenerateData();
109 
110  typename Superclass::InputImageConstPointer inputPtr1 = this->GetInput1();
111  typename Superclass::InputImageConstPointer inputPtr2 = this->GetInput2();
112  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
113 
114  if (inputPtr1->GetLargestPossibleRegion() != inputPtr2->GetLargestPossibleRegion())
115  {
116  itkExceptionMacro(<< "InputImage1 and InputImage2 have different requested regions.");
117  }
118 }
119 
123 template <class TInputImage1, class TInputImage2, class TOutputImage>
124 void ConcatenateVectorImageFilter<TInputImage1, TInputImage2, TOutputImage>::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread,
125  itk::ThreadIdType threadId)
126 {
127  // retrieves inputs and output pointer
128  InputImage1PointerType input1 = this->GetInput1();
129  InputImage2PointerType input2 = this->GetInput2();
130  OutputImagePointerType output = this->GetOutput();
132 
133  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
134 
135  // Define the portion of the input to walk for this thread
136  typename InputImage1Type::RegionType inputRegionForThread;
137  this->CallCopyOutputRegionToInputRegion(inputRegionForThread, outputRegionForThread);
138 
139  // Iterators typedefs
140  typedef itk::ImageRegionIterator<InputImage1Type> Input1IteratorType;
141  typedef itk::ImageRegionIterator<InputImage2Type> Input2IteratorType;
142  typedef itk::ImageRegionIterator<OutputImageType> OutputIteratorType;
143 
144  // Iterators declaration
145  Input1IteratorType input1It(input1, inputRegionForThread);
146  Input2IteratorType input2It(input2, inputRegionForThread);
147  OutputIteratorType outputIt(output, outputRegionForThread);
148 
149  input1It.GoToBegin();
150  input2It.GoToBegin();
151  outputIt.GoToBegin();
152 
153  typename OutputImageType::PixelType outputPix(outputIt.Get().GetSize());
154  // Retrieve the size of each input pixel
155  const unsigned int l1 = input1It.Get().GetSize();
156  const unsigned int l2 = input2It.Get().GetSize();
157 
158  // Iterate through the pixel
159  while (!outputIt.IsAtEnd())
160  {
161 
162  // Check the size of each input pixel
163  assert(l1 == input1It.Get().GetSize());
164  assert(l2 == input2It.Get().GetSize());
165  // Check the size of the output pixel
166  assert(l1 + l2 == outputPix.GetSize());
167  // References to the input pixels
168  InputPixel1Type const& pix1 = input1It.Get();
169  InputPixel2Type const& pix2 = input2It.Get();
170 
171  // Loop through each band of the first image
172  for (unsigned int i = 0; i < l1; ++i)
173  {
174  // Fill the output pixel
175  outputPix[i] = static_cast<typename OutputImageType::InternalPixelType>(pix1[i]);
176  }
177  // Loop though each band of the second image
178  for (unsigned int i = 0; i < l2; ++i)
179  {
180  // Fill the output pixel
181  outputPix[i + l1] = static_cast<typename OutputImageType::InternalPixelType>(pix2[i]);
182  }
183  // Set the output pixel
184  outputIt.Set(outputPix);
185  // Increment the iterator
186  ++input1It;
187  ++input2It;
188  ++outputIt;
189  }
190 }
otb::ConcatenateVectorImageFilter::SetInput1
void SetInput1(const TInputImage1 *image)
otb::ConcatenateVectorImageFilter::GenerateOutputInformation
void GenerateOutputInformation() override
otb::ConcatenateVectorImageFilter::InputImage2Type
TInputImage2 InputImage2Type
Definition: otbConcatenateVectorImageFilter.h:53
otbConcatenateVectorImageFilter.h
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::ConcatenateVectorImageFilter::BeforeThreadedGenerateData
void BeforeThreadedGenerateData() override
otb::ConcatenateVectorImageFilter::ThreadedGenerateData
void ThreadedGenerateData(const OutputImageRegionType &outputRegionForThread, itk::ThreadIdType threadId) override
otb::ConcatenateVectorImageFilter::GetInput1
InputImage1Type * GetInput1(void)
otb::ConcatenateVectorImageFilter::ConcatenateVectorImageFilter
ConcatenateVectorImageFilter()
otb::ConcatenateVectorImageFilter::GetInput2
InputImage2Type * GetInput2(void)
otb::ConcatenateVectorImageFilter::~ConcatenateVectorImageFilter
~ConcatenateVectorImageFilter() override
otb::ConcatenateVectorImageFilter::InputImage1Type
TInputImage1 InputImage1Type
Definition: otbConcatenateVectorImageFilter.h:49
otb::ConcatenateVectorImageFilter::SetInput2
void SetInput2(const TInputImage2 *image)