OTB  10.0.0
Orfeo Toolbox
otbConcatenateVectorImageFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2024 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 "otbMacro.h" //for
26 #include "itkImageRegionIterator.h"
27 #include "itkProgressReporter.h"
28 
29 namespace otb
30 {
34 template <class TInputImage1, class TInputImage2, class TOutputImage>
36 {
37  this->SetNumberOfRequiredInputs(2);
38  this->DynamicMultiThreadingOn();
39 }
40 
44 template <class TInputImage1, class TInputImage2, class TOutputImage>
46 {
47 }
48 
53 template <class TInputImage1, class TInputImage2, class TOutputImage>
55 {
56  this->SetNthInput(0, const_cast<TInputImage1*>(image));
57 }
58 
63 template <class TInputImage1, class TInputImage2, class TOutputImage>
65 {
66  this->SetNthInput(1, const_cast<TInputImage2*>(image));
67 }
68 
73 template <class TInputImage1, class TInputImage2, class TOutputImage>
76 {
77  return const_cast<InputImage1Type*>(this->GetInput(0));
78 }
79 
84 template <class TInputImage1, class TInputImage2, class TOutputImage>
87 {
88  return const_cast<InputImage2Type*>(this->GetInput(1));
89 }
90 
91 template <class TInputImage1, class TInputImage2, class TOutputImage>
93 {
94  // Call to the superclass implementation
95  Superclass::GenerateOutputInformation();
96 
97  typename Superclass::InputImageConstPointer inputPtr1 = this->GetInput1();
98  typename Superclass::InputImageConstPointer inputPtr2 = this->GetInput2();
99  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
100 
101  unsigned int nbComponentsPerPixel = inputPtr1->GetNumberOfComponentsPerPixel() + inputPtr2->GetNumberOfComponentsPerPixel();
102 
103  // initialize the number of channels of the output image
104  outputPtr->SetNumberOfComponentsPerPixel(nbComponentsPerPixel);
105 }
106 
107 template <class TInputImage1, class TInputImage2, class TOutputImage>
109 {
110  Superclass::BeforeThreadedGenerateData();
111 
112  typename Superclass::InputImageConstPointer inputPtr1 = this->GetInput1();
113  typename Superclass::InputImageConstPointer inputPtr2 = this->GetInput2();
114  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
115 
116  if (inputPtr1->GetLargestPossibleRegion() != inputPtr2->GetLargestPossibleRegion())
117  {
118  itkExceptionMacro(<< "InputImage1 and InputImage2 have different requested regions.");
119  }
120 }
121 
125 template <class TInputImage1, class TInputImage2, class TOutputImage>
127 {
128  // retrieves inputs and output pointer
129  InputImage1PointerType input1 = this->GetInput1();
130  InputImage2PointerType input2 = this->GetInput2();
131  OutputImagePointerType output = this->GetOutput();
133 
134  // Define the portion of the input to walk for this thread
135  typename InputImage1Type::RegionType inputRegionForThread;
136  this->CallCopyOutputRegionToInputRegion(inputRegionForThread, outputRegionForThread);
137 
138  // Iterators typedefs
139  typedef itk::ImageRegionIterator<InputImage1Type> Input1IteratorType;
140  typedef itk::ImageRegionIterator<InputImage2Type> Input2IteratorType;
141  typedef itk::ImageRegionIterator<OutputImageType> OutputIteratorType;
142 
143  // Iterators declaration
144  Input1IteratorType input1It(input1, inputRegionForThread);
145  Input2IteratorType input2It(input2, inputRegionForThread);
146  OutputIteratorType outputIt(output, outputRegionForThread);
147 
148  input1It.GoToBegin();
149  input2It.GoToBegin();
150  outputIt.GoToBegin();
151 
152  typename OutputImageType::PixelType outputPix(outputIt.Get().GetSize());
153  // Retrieve the size of each input pixel
154  const unsigned int l1 = input1It.Get().GetSize();
155  const unsigned int l2 = input2It.Get().GetSize();
156 
157  // Iterate through the pixel
158  while (!outputIt.IsAtEnd())
159  {
160 
161  // Check the size of each input pixel
162  assert(l1 == input1It.Get().GetSize());
163  assert(l2 == input2It.Get().GetSize());
164  // Check the size of the output pixel
165  assert(l1 + l2 == outputPix.GetSize());
166  // References to the input pixels
167  InputPixel1Type const& pix1 = input1It.Get();
168  InputPixel2Type const& pix2 = input2It.Get();
169 
170  // Loop through each band of the first image
171  for (unsigned int i = 0; i < l1; ++i)
172  {
173  // Fill the output pixel
174  outputPix[i] = static_cast<typename OutputImageType::InternalPixelType>(pix1[i]);
175  }
176  // Loop though each band of the second image
177  for (unsigned int i = 0; i < l2; ++i)
178  {
179  // Fill the output pixel
180  outputPix[i + l1] = static_cast<typename OutputImageType::InternalPixelType>(pix2[i]);
181  }
182  // Set the output pixel
183  outputIt.Set(outputPix);
184  // Increment the iterator
185  ++input1It;
186  ++input2It;
187  ++outputIt;
188  }
189 }
void SetInput2(const TInputImage2 *image)
InputImage2Type * GetInput2(void)
void GenerateOutputInformation() override
void BeforeThreadedGenerateData() override
InputImage1Type * GetInput1(void)
void DynamicThreadedGenerateData(const OutputImageRegionType &outputRegionForThread) override
void SetInput1(const TInputImage1 *image)
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.