OTB  9.0.0
Orfeo Toolbox
otbImageListToVectorImageFilter.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 otbImageListToVectorImageFilter_hxx
22 #define otbImageListToVectorImageFilter_hxx
23 
25 #include "itkImageRegionIterator.h"
26 #include <vector>
27 #include "otbMacro.h"
28 #include "itkProgressReporter.h"
29 #include "otbImageMetadata.h"
30 
31 namespace otb
32 {
36 template <class TImageList, class TVectorImage>
38 {
39  if (this->GetOutput())
40  {
41  if (this->GetInput()->Size() > 0)
42  {
43  this->GetOutput()->CopyInformation(this->GetInput()->GetNthElement(0));
44  this->GetOutput()->SetNumberOfComponentsPerPixel(this->GetInput()->Size());
45  this->GetOutput()->SetLargestPossibleRegion(this->GetInput()->GetNthElement(0)->GetLargestPossibleRegion());
47 
48  // Copy band specific metadata from the inputs to the output
50  InputImageListPointerType inputPtr = this->GetInput();
51  typename InputImageListType::ConstIterator inputListIt = inputPtr->Begin();
52 
53  while (inputListIt != inputPtr->End())
54  {
55  const auto & imd = inputListIt.Get()->GetImageMetadata();
56  if (imd.Bands.size())
57  {
58  bandsImd.push_back(imd.Bands[0]);
59  }
60  else
61  {
62  bandsImd.push_back({});
63  }
64  ++inputListIt;
65  }
66 
67  this->GetOutput()->SetBandImageMetadata(bandsImd);
68  }
69  }
70 }
74 template <class TImageList, class TVectorImage>
76 {
77  InputImageListPointerType inputPtr = this->GetInput();
78  typename InputImageListType::ConstIterator inputListIt = inputPtr->Begin();
79  while (inputListIt != inputPtr->End())
80  {
81  inputListIt.Get()->SetRequestedRegion(this->GetOutput()->GetRequestedRegion());
82  ++inputListIt;
83  }
84 }
85 
89 template <class TImageList, class TVectorImage>
91 {
92 
93  InputImageListPointerType inputPtr = this->GetInput();
94  OutputVectorImagePointerType outputPtr = this->GetOutput();
95 
96  itk::ProgressReporter progress(this, 0, outputPtr->GetRequestedRegion().GetNumberOfPixels());
97 
98  // Output image initializations
99  typename OutputVectorImageType::PixelType black;
100  black.SetSize(inputPtr->Size());
101  black.Fill(0);
102  outputPtr->SetBufferedRegion(outputPtr->GetRequestedRegion());
103  outputPtr->Allocate();
104  outputPtr->FillBuffer(black);
105 
106  // defines input and output iterators
107  typedef itk::ImageRegionConstIterator<InputImageType> InputIteratorType;
108  typedef itk::ImageRegionIterator<OutputVectorImageType> OutputIteratorType;
109 
110  typename InputImageListType::ConstIterator inputListIt = inputPtr->Begin();
111 
112  // defines a vector of input iterators
113  typedef std::vector<InputIteratorType> InputIteratorListType;
114  InputIteratorListType inputIteratorList;
115 
116  // fills the vector of input iterators
117  for (; inputListIt != inputPtr->End(); ++inputListIt)
118  {
119  inputIteratorList.push_back(InputIteratorType(inputListIt.Get(), outputPtr->GetRequestedRegion()));
120  inputIteratorList.back().GoToBegin();
121  }
122 
123  // walk through the output image
124  OutputIteratorType outputIt(outputPtr, outputPtr->GetRequestedRegion());
125  outputIt.GoToBegin();
126 
127  while (!outputIt.IsAtEnd())
128  {
129  typename OutputVectorImageType::PixelType pixel = outputIt.Get();
130  unsigned int counter = 0;
131  // for each input iterator, fill the right component
132  for (typename InputIteratorListType::iterator it = inputIteratorList.begin(); it != inputIteratorList.end(); ++it)
133  {
134  if (!it->IsAtEnd())
135  {
136  pixel[counter] = static_cast<typename OutputVectorImageType::InternalPixelType>(it->Get());
137  ++(*it);
138  ++counter;
139  }
140  }
141  outputIt.Set(pixel);
142  progress.CompletedPixel();
143  ++outputIt;
144  }
145 }
otbImageListToVectorImageFilter.h
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::ImageListToVectorImageFilter::GenerateData
void GenerateData(void) override
otbMacro.h
otb::ImageListToVectorImageFilter::GenerateOutputInformation
void GenerateOutputInformation(void) override
otbImageMetadata.h
otb::ImageListToVectorImageFilter::GenerateInputRequestedRegion
void GenerateInputRequestedRegion(void) override
otb::ImageMetadata::ImageMetadataBandsType
std::vector< ImageMetadataBase > ImageMetadataBandsType
Definition: otbImageMetadata.h:277