21 #ifndef otbExtractROIBase_hxx
22 #define otbExtractROIBase_hxx
25 #include "itkImageRegionIterator.h"
26 #include "itkObjectFactory.h"
27 #include "itkProgressReporter.h"
36 template <
class TInputImage,
class TOutputImage>
38 :
itk::ImageToImageFilter<TInputImage, TOutputImage>(), m_StartX(0), m_StartY(0), m_SizeX(0), m_SizeY(0)
45 template <
class TInputImage,
class TOutputImage>
48 Superclass::PrintSelf(os, indent);
50 os << indent <<
"ExtractionRegion: " << m_ExtractionRegion << std::endl;
51 os << indent <<
"OutputImageRegion: " << m_OutputImageRegion << std::endl;
54 template <
class TInputImage,
class TOutputImage>
57 destRegion = srcRegion;
61 for (
unsigned int i = 0; i < InputImageDimension; ++i)
63 index[i] += m_ExtractionRegion.GetIndex()[i];
65 destRegion.SetIndex(index);
68 template <
class TInputImage,
class TOutputImage>
71 m_ExtractionRegion = extractRegion;
73 unsigned int nonzeroSizeCount = 0;
82 for (
unsigned int i = 0; i < InputImageDimension; ++i)
86 outputSize[nonzeroSizeCount] = inputSize[i];
87 outputIndex[nonzeroSizeCount] = 0;
93 if (nonzeroSizeCount != OutputImageDimension)
95 itkExceptionMacro(
"Extraction Region not consistent with output image");
98 m_OutputImageRegion.SetSize(outputSize);
99 m_OutputImageRegion.SetIndex(outputIndex);
104 template <
class TInputImage,
class TOutputImage>
107 m_SizeX = roi.GetSize()[0];
108 m_SizeY = roi.GetSize()[1];
109 m_StartX = roi.GetIndex()[0];
110 m_StartY = roi.GetIndex()[1];
115 template <
class TInputImage,
class TOutputImage>
118 Superclass::GenerateInputRequestedRegion();
120 typename Superclass::InputImagePointer inputPtr =
const_cast<InputImageType*
>(this->GetInput());
121 typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
135 for (
unsigned int i = 0; i < InputImageDimension; ++i)
137 index[i] += offset[i];
139 requestedRegion.SetIndex(index);
140 inputPtr->SetRequestedRegion(requestedRegion);
152 template <
class TInputImage,
class TOutputImage>
155 Superclass::GenerateOutputInformation();
159 typename Superclass::InputImageConstPointer inputPtr = this->GetInput();
171 if ((m_SizeX == 0) || (m_SizeX > (inputRegion.GetSize()[0] - m_StartX)))
173 m_SizeX = inputRegion.GetSize()[0] - m_StartX;
175 if ((m_SizeY == 0) || (m_SizeY > (inputRegion.GetSize()[1] - m_StartY)))
177 m_SizeY = inputRegion.GetSize()[1] - m_StartY;
187 desiredRegion.SetSize(size);
188 desiredRegion.SetIndex(start);
190 this->SetInternalExtractionRegion(desiredRegion);
196 typename Superclass::OutputImagePointer outputPtr = this->GetOutput();
209 outputPtr->SetLargestPossibleRegion(m_OutputImageRegion);
212 const itk::ImageBase<InputImageDimension>* phyData;
214 phyData =
dynamic_cast<const itk::ImageBase<InputImageDimension>*
>(this->GetInput());
219 itkExceptionMacro(<<
"otb::ExtractROIBase::GenerateOutputInformation "
220 <<
"cannot cast input to " <<
typeid(itk::ImageBase<InputImageDimension>*).name());
234 if (
static_cast<unsigned int>(OutputImageDimension) >
static_cast<unsigned int>(InputImageDimension))
239 for (i = 0; i < InputImageDimension; ++i)
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)
245 outputDirection[i][dim] = inputDirection[i][dim];
248 for (; i < OutputImageDimension; ++i)
250 outputSpacing[i] = 1.0;
251 outputOrigin[i] = 0.0;
252 for (
unsigned int dim = 0; dim < InputImageDimension; ++dim)
254 outputDirection[i][dim] = 0.0;
256 outputDirection[i][i] = 1.0;
262 int nonZeroCount = 0;
263 for (
unsigned int i = 0; i < InputImageDimension; ++i)
265 if (m_ExtractionRegion.GetSize()[i] == 0)
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)
272 outputDirection[nonZeroCount][dim] = inputDirection[nonZeroCount][dim];
279 outputPtr->SetSignedSpacing(outputSpacing);
280 outputPtr->SetDirection(outputDirection);
281 outputPtr->SetOrigin(outputOrigin);