OTB  9.0.0
Orfeo Toolbox
otbExtractROIBase.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 otbExtractROIBase_hxx
22 #define otbExtractROIBase_hxx
23 
24 #include "otbExtractROIBase.h"
25 #include "itkImageRegionIterator.h"
26 #include "itkObjectFactory.h"
27 #include "itkProgressReporter.h"
28 #include "otbMacro.h"
29 
30 namespace otb
31 {
32 
36 template <class TInputImage, class TOutputImage>
38  : itk::ImageToImageFilter<TInputImage, TOutputImage>(), m_StartX(0), m_StartY(0), m_SizeX(0), m_SizeY(0)
39 {
40 }
41 
45 template <class TInputImage, class TOutputImage>
46 void ExtractROIBase<TInputImage, TOutputImage>::PrintSelf(std::ostream& os, itk::Indent indent) const
47 {
48  Superclass::PrintSelf(os, indent);
49 
50  os << indent << "ExtractionRegion: " << m_ExtractionRegion << std::endl;
51  os << indent << "OutputImageRegion: " << m_OutputImageRegion << std::endl;
52 }
53 
54 template <class TInputImage, class TOutputImage>
56 {
57  destRegion = srcRegion;
58 
59  OutputImageIndexType index = destRegion.GetIndex();
60 
61  for (unsigned int i = 0; i < InputImageDimension; ++i)
62  {
63  index[i] += m_ExtractionRegion.GetIndex()[i];
64  }
65  destRegion.SetIndex(index);
66 }
67 
68 template <class TInputImage, class TOutputImage>
70 {
71  m_ExtractionRegion = extractRegion;
72 
73  unsigned int nonzeroSizeCount = 0;
74  InputImageSizeType inputSize = extractRegion.GetSize();
75  OutputImageSizeType outputSize;
76  OutputImageIndexType outputIndex;
77 
82  for (unsigned int i = 0; i < InputImageDimension; ++i)
83  {
84  if (inputSize[i])
85  {
86  outputSize[nonzeroSizeCount] = inputSize[i];
87  outputIndex[nonzeroSizeCount] = 0;
88  nonzeroSizeCount++;
89  }
90  }
92 
93  if (nonzeroSizeCount != OutputImageDimension)
94  {
95  itkExceptionMacro("Extraction Region not consistent with output image");
96  }
97 
98  m_OutputImageRegion.SetSize(outputSize);
99  m_OutputImageRegion.SetIndex(outputIndex);
100 
101  this->Modified();
102 }
103 
104 template <class TInputImage, class TOutputImage>
106 {
107  m_SizeX = roi.GetSize()[0];
108  m_SizeY = roi.GetSize()[1];
109  m_StartX = roi.GetIndex()[0];
110  m_StartY = roi.GetIndex()[1];
111 
112  this->Modified();
113 }
114 
115 template <class TInputImage, class TOutputImage>
117 {
118  Superclass::GenerateInputRequestedRegion();
119 
120  typename Superclass::InputImagePointer inputPtr = const_cast<InputImageType*>(this->GetInput());
121  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
122 
123  if (!inputPtr)
124  {
125  return;
126  }
127  if (!outputPtr)
128  {
129  return;
130  }
131  InputImageRegionType requestedRegion = outputPtr->GetRequestedRegion();
132  InputImageIndexType index = requestedRegion.GetIndex();
133  InputImageIndexType offset = m_ExtractionRegion.GetIndex();
134 
135  for (unsigned int i = 0; i < InputImageDimension; ++i)
136  {
137  index[i] += offset[i];
138  }
139  requestedRegion.SetIndex(index);
140  inputPtr->SetRequestedRegion(requestedRegion);
141 }
142 
152 template <class TInputImage, class TOutputImage>
154 {
155  Superclass::GenerateOutputInformation();
156 
157  // Compute the area to extract
158  // If SizeX/Y == 0, then set SizeX/Y to image size
159  typename Superclass::InputImageConstPointer inputPtr = this->GetInput();
160 
161  // Check if input exists or not before doing anything
162  if (!inputPtr)
163  {
164  return;
165  }
166 
167  // Get the input image
168  // const InputImageRegionType& inputRegion = inputPtr->GetRequestedRegion();
169  const InputImageRegionType& inputRegion = inputPtr->GetLargestPossibleRegion();
170 
171  if ((m_SizeX == 0) || (m_SizeX > (inputRegion.GetSize()[0] - m_StartX)))
172  {
173  m_SizeX = inputRegion.GetSize()[0] - m_StartX;
174  }
175  if ((m_SizeY == 0) || (m_SizeY > (inputRegion.GetSize()[1] - m_StartY)))
176  {
177  m_SizeY = inputRegion.GetSize()[1] - m_StartY;
178  }
179 
180  InputImageIndexType start;
181  start[0] = m_StartX;
182  start[1] = m_StartY;
183  InputImageSizeType size;
184  size[0] = m_SizeX;
185  size[1] = m_SizeY;
186  InputImageRegionType desiredRegion;
187  desiredRegion.SetSize(size);
188  desiredRegion.SetIndex(start);
189  // Call to the base class method for region initialization
190  this->SetInternalExtractionRegion(desiredRegion);
191 
192  // do not call the superclass' implementation of this method since
193  // this filter allows the input the output to be of different dimensions
194 
195  // get pointers to the input and output
196  typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
197  // typename Superclass::InputImageConstPointer inputPtr = this->GetInput();
198 
199  if (!inputPtr)
200  {
201  return;
202  }
203  if (!outputPtr)
204  {
205  return;
206  }
207 
208  // Set the output image size to the same value as the extraction region.
209  outputPtr->SetLargestPossibleRegion(m_OutputImageRegion);
210 
211  // Set the output spacing and origin
212  const itk::ImageBase<InputImageDimension>* phyData;
213 
214  phyData = dynamic_cast<const itk::ImageBase<InputImageDimension>*>(this->GetInput());
215 
216  if (!phyData)
217  {
218  // pointer could not be cast back down
219  itkExceptionMacro(<< "otb::ExtractROIBase::GenerateOutputInformation "
220  << "cannot cast input to " << typeid(itk::ImageBase<InputImageDimension>*).name());
221  }
222 
223  // Copy what we can from the image from spacing and origin of the input
224  // This logic needs to be augmented with logic that select which
225  // dimensions to copy
226  const typename InputImageType::SpacingType& inputSpacing = inputPtr->GetSignedSpacing();
227  const typename InputImageType::DirectionType& inputDirection = inputPtr->GetDirection();
228  const typename InputImageType::PointType& inputOrigin = inputPtr->GetOrigin();
229 
230  typename OutputImageType::SpacingType outputSpacing;
231  typename OutputImageType::DirectionType outputDirection;
232  typename OutputImageType::PointType outputOrigin;
233 
234  if (static_cast<unsigned int>(OutputImageDimension) > static_cast<unsigned int>(InputImageDimension))
235  {
236  unsigned int i;
237  // copy the input to the output and fill the rest of the
238  // output with zeros.
239  for (i = 0; i < InputImageDimension; ++i)
240  {
241  outputSpacing[i] = inputSpacing[i];
242  outputOrigin[i] = inputOrigin[i] + static_cast<double>(m_ExtractionRegion.GetIndex()[i]) * outputSpacing[i];
243  for (unsigned int dim = 0; dim < InputImageDimension; ++dim)
244  {
245  outputDirection[i][dim] = inputDirection[i][dim];
246  }
247  }
248  for (; i < OutputImageDimension; ++i)
249  {
250  outputSpacing[i] = 1.0;
251  outputOrigin[i] = 0.0;
252  for (unsigned int dim = 0; dim < InputImageDimension; ++dim)
253  {
254  outputDirection[i][dim] = 0.0;
255  }
256  outputDirection[i][i] = 1.0;
257  }
258  }
259  else
260  {
261  // copy the non-collapsed part of the input spacing and origing to the output
262  int nonZeroCount = 0;
263  for (unsigned int i = 0; i < InputImageDimension; ++i)
264  {
265  if (m_ExtractionRegion.GetSize()[i] == 0)
266  continue;
267 
268  outputSpacing[nonZeroCount] = inputSpacing[i];
269  outputOrigin[nonZeroCount] = inputOrigin[i] + static_cast<double>(m_ExtractionRegion.GetIndex()[i]) * outputSpacing[i];
270  for (unsigned int dim = 0; dim < OutputImageDimension; ++dim)
271  {
272  outputDirection[nonZeroCount][dim] = inputDirection[nonZeroCount][dim];
273  }
274  nonZeroCount++;
275  }
276  }
277 
278  // set the spacing and origin
279  outputPtr->SetSignedSpacing(outputSpacing);
280  outputPtr->SetDirection(outputDirection);
281  outputPtr->SetOrigin(outputOrigin);
282 
283  // Thomas Feuvrier:
284  // ITK has this code. But not in our case, because the number of component per pixel depends on the bands selected by the user.
285  // This parameter is given in the underlying classes.
286  // outputPtr->SetNumberOfComponentsPerPixel(
287  // inputPtr->GetNumberOfComponentsPerPixel() );
288  //
289 }
290 
291 } // end namespace otb
292 
293 #endif
otb::VectorImage< TInputPixelType, 2 >::SpacingType
Superclass::SpacingType SpacingType
Definition: otbVectorImage.h:117
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::ExtractROIBase< VectorImage< TInputPixelType, 2 >, Image< TOutputPixelType, 2 > >::OutputImageIndexType
Image< TOutputPixelType, 2 > ::IndexType OutputImageIndexType
Definition: otbExtractROIBase.h:76
otbMacro.h
otb::VectorImage< TInputPixelType, 2 >::PointType
Superclass::PointType PointType
Definition: otbVectorImage.h:121
otb::ExtractROIBase< VectorImage< TInputPixelType, 2 >, Image< TOutputPixelType, 2 > >::InputImageIndexType
VectorImage< TInputPixelType, 2 > ::IndexType InputImageIndexType
Definition: otbExtractROIBase.h:77
otb::ExtractROIBase< VectorImage< TInputPixelType, 2 >, Image< TOutputPixelType, 2 > >::InputImageSizeType
VectorImage< TInputPixelType, 2 > ::SizeType InputImageSizeType
Definition: otbExtractROIBase.h:79
otb::ExtractROIBase
Base class to extract area of images.
Definition: otbExtractROIBase.h:48
otb::ExtractROIBase< VectorImage< TInputPixelType, 2 >, Image< TOutputPixelType, 2 > >::OutputImageSizeType
Image< TOutputPixelType, 2 > ::SizeType OutputImageSizeType
Definition: otbExtractROIBase.h:78
otb::ExtractROIBase< VectorImage< TInputPixelType, 2 >, Image< TOutputPixelType, 2 > >::OutputImageRegionType
Image< TOutputPixelType, 2 > ::RegionType OutputImageRegionType
Definition: otbExtractROIBase.h:68
otb::ExtractROIBase::ExtractROIBase
ExtractROIBase()
Definition: otbExtractROIBase.hxx:37
itk
Definition: otbNoDataHelper.h:31
otbExtractROIBase.h
otb::Image< TOutputPixelType, 2 >::DirectionType
Superclass::DirectionType DirectionType
Definition: otbImage.h:151
otb::Image< TOutputPixelType, 2 >::SpacingType
Superclass::SpacingType SpacingType
Definition: otbImage.h:158
otb::Image< TOutputPixelType, 2 >::PointType
Superclass::PointType PointType
Definition: otbImage.h:162
otb::VectorImage< TInputPixelType, 2 >::DirectionType
Superclass::DirectionType DirectionType
Definition: otbVectorImage.h:110
otb::ExtractROIBase< VectorImage< TInputPixelType, 2 >, Image< TOutputPixelType, 2 > >::InputImageRegionType
VectorImage< TInputPixelType, 2 > ::RegionType InputImageRegionType
Definition: otbExtractROIBase.h:69
otb::VectorImage< TInputPixelType, 2 >