21 #ifndef otbScalarImageToHigherOrderTexturesFilter_hxx
22 #define otbScalarImageToHigherOrderTexturesFilter_hxx
25 #include "itkImageRegionIteratorWithIndex.h"
26 #include "itkImageRegionIterator.h"
27 #include "itkProgressReporter.h"
31 template <
class TInputImage,
class TOutputImage>
33 : m_Radius(), m_NumberOfBinsPerAxis(8), m_InputImageMinimum(0), m_InputImageMaximum(255), m_FastCalculations(false), m_SubsampleFactor(), m_SubsampleOffset()
36 this->SetNumberOfRequiredOutputs(10);
39 this->SetNthOutput(0, OutputImageType::New());
40 this->SetNthOutput(1, OutputImageType::New());
41 this->SetNthOutput(2, OutputImageType::New());
42 this->SetNthOutput(3, OutputImageType::New());
43 this->SetNthOutput(4, OutputImageType::New());
44 this->SetNthOutput(5, OutputImageType::New());
45 this->SetNthOutput(6, OutputImageType::New());
46 this->SetNthOutput(7, OutputImageType::New());
47 this->SetNthOutput(8, OutputImageType::New());
48 this->SetNthOutput(9, OutputImageType::New());
55 typedef itk::Neighborhood<typename InputImageType::PixelType, InputImageType::ImageDimension> NeighborhoodType;
56 NeighborhoodType hood;
61 unsigned int centerIndex = hood.GetCenterNeighborhoodIndex();
63 for (
unsigned int d = 0; d < centerIndex; d++)
66 offsets->push_back(offset);
74 template <
class TInputImage,
class TOutputImage>
79 template <
class TInputImage,
class TOutputImage>
83 return this->GetOutput(0);
86 template <
class TInputImage,
class TOutputImage>
90 return this->GetOutput(1);
93 template <
class TInputImage,
class TOutputImage>
97 return this->GetOutput(2);
100 template <
class TInputImage,
class TOutputImage>
104 return this->GetOutput(3);
107 template <
class TInputImage,
class TOutputImage>
111 return this->GetOutput(4);
114 template <
class TInputImage,
class TOutputImage>
118 return this->GetOutput(5);
121 template <
class TInputImage,
class TOutputImage>
125 return this->GetOutput(6);
128 template <
class TInputImage,
class TOutputImage>
132 return this->GetOutput(7);
135 template <
class TInputImage,
class TOutputImage>
139 return this->GetOutput(8);
142 template <
class TInputImage,
class TOutputImage>
146 return this->GetOutput(9);
149 template <
class TInputImage,
class TOutputImage>
153 offsetVector->push_back(offset);
154 this->SetOffsets(offsetVector);
157 template <
class TInputImage,
class TOutputImage>
164 outputRegion.SetIndex(0, 0);
165 outputRegion.SetIndex(1, 0);
166 outputRegion.SetSize(0, 1 + (inputRegion.GetSize(0) - 1 - m_SubsampleOffset[0]) / m_SubsampleFactor[0]);
167 outputRegion.SetSize(1, 1 + (inputRegion.GetSize(1) - 1 - m_SubsampleOffset[1]) / m_SubsampleFactor[1]);
169 typename OutputImageType::SpacingType outSpacing = inputPtr->GetSignedSpacing();
170 outSpacing[0] *= m_SubsampleFactor[0];
171 outSpacing[1] *= m_SubsampleFactor[1];
173 typename OutputImageType::PointType outOrigin;
174 inputPtr->TransformIndexToPhysicalPoint(inputRegion.GetIndex() + m_SubsampleOffset, outOrigin);
176 for (
unsigned int i = 0; i < this->GetNumberOfOutputs(); i++)
179 outputPtr->CopyInformation(inputPtr);
180 outputPtr->SetLargestPossibleRegion(outputRegion);
181 outputPtr->SetOrigin(outOrigin);
182 outputPtr->SetSignedSpacing(outSpacing);
187 template <
class TInputImage,
class TOutputImage>
191 Superclass::GenerateInputRequestedRegion();
197 if (!inputPtr || !outputPtr)
206 typename OutputRegionType::IndexType outputIndex = outputRequestedRegion.GetIndex();
207 typename OutputRegionType::SizeType outputSize = outputRequestedRegion.GetSize();
211 outputIndex[0] = outputIndex[0] * m_SubsampleFactor[0] + m_SubsampleOffset[0] + inputLargest.GetIndex(0);
212 outputIndex[1] = outputIndex[1] * m_SubsampleFactor[1] + m_SubsampleOffset[1] + inputLargest.GetIndex(1);
213 outputSize[0] = 1 + (outputSize[0] - 1) * m_SubsampleFactor[0];
214 outputSize[1] = 1 + (outputSize[1] - 1) * m_SubsampleFactor[1];
219 inputRequestedRegion.PadByRadius(m_Radius);
222 if (inputRequestedRegion.Crop(inputPtr->GetLargestPossibleRegion()))
224 inputPtr->SetRequestedRegion(inputRequestedRegion);
229 itk::InvalidRequestedRegionError e(__FILE__, __LINE__);
230 e.SetLocation(ITK_LOCATION);
231 e.SetDescription(
"Requested region is (at least partially) outside the largest possible region.");
232 e.SetDataObject(inputPtr);
237 template <
class TInputImage,
class TOutputImage>
239 itk::ThreadIdType threadId)
244 typedef typename itk::ImageRegionIterator<OutputImageType> IteratorType;
245 std::vector<IteratorType> outputImagesIterators;
247 for (
unsigned int i = 0; i < this->GetNumberOfOutputs(); ++i)
249 outputImagesIterators.push_back(IteratorType(this->GetOutput(i), outputRegionForThread));
250 outputImagesIterators[i].GoToBegin();
254 typename InputImageType::PointType topLeftPoint;
255 typename InputImageType::PointType bottomRightPoint;
256 inputPtr->TransformIndexToPhysicalPoint(outputImagesIterators[0].GetIndex() - m_Radius, topLeftPoint);
257 inputPtr->TransformIndexToPhysicalPoint(outputImagesIterators[0].GetIndex() + m_Radius, bottomRightPoint);
258 double maxDistance = topLeftPoint.EuclideanDistanceTo(bottomRightPoint);
263 itk::ProgressReporter progress(
this, threadId, outputRegionForThread.GetNumberOfPixels());
266 while (!outputImagesIterators[0].IsAtEnd())
269 typename InputRegionType::IndexType inputIndex;
270 typename InputRegionType::SizeType inputSize;
273 typename OutputImageType::IndexType outIndex;
275 for (
unsigned int dim = 0; dim < InputImageType::ImageDimension; ++dim)
277 outIndex[dim] = outputImagesIterators[0].GetIndex()[dim] * m_SubsampleFactor[dim] + m_SubsampleOffset[dim] + inputLargest.GetIndex(dim);
278 inputIndex[dim] = outIndex[dim] - m_Radius[dim];
279 inputSize[dim] = 2 * m_Radius[dim] + 1;
284 inputRegion.SetIndex(inputIndex);
285 inputRegion.SetSize(inputSize);
287 inputRegion.Crop(inputPtr->GetBufferedRegion());
291 localInputImage->SetRegions(inputRegion);
292 localInputImage->Allocate();
293 typedef itk::ImageRegionIteratorWithIndex<InputImageType> ImageRegionIteratorType;
294 typedef itk::ImageRegionConstIteratorWithIndex<InputImageType> ImageRegionConstIteratorType;
295 ImageRegionConstIteratorType itInputPtr(inputPtr, inputRegion);
296 ImageRegionIteratorType itLocalInputImage(localInputImage, inputRegion);
297 for (itInputPtr.GoToBegin(), itLocalInputImage.GoToBegin(); !itInputPtr.IsAtEnd(); ++itInputPtr, ++itLocalInputImage)
299 itLocalInputImage.Set(itInputPtr.Get());
302 typename ScalarImageToRunLengthFeaturesFilterType::Pointer runLengthFeatureCalculator = ScalarImageToRunLengthFeaturesFilterType::New();
303 runLengthFeatureCalculator->SetInput(localInputImage);
304 runLengthFeatureCalculator->SetOffsets(m_Offsets);
305 runLengthFeatureCalculator->SetNumberOfBinsPerAxis(m_NumberOfBinsPerAxis);
306 runLengthFeatureCalculator->SetPixelValueMinMax(m_InputImageMinimum, m_InputImageMaximum);
307 runLengthFeatureCalculator->SetDistanceValueMinMax(0, maxDistance);
309 runLengthFeatureCalculator->Update();
311 typename ScalarImageToRunLengthFeaturesFilterType::FeatureValueVector& featuresMeans = *(runLengthFeatureCalculator->GetFeatureMeans().GetPointer());
314 for (
unsigned int i = 0; i < this->GetNumberOfOutputs(); ++i)
317 outputImagesIterators[i].Set(featuresMeans[i]);
319 ++outputImagesIterators[i];
322 progress.CompletedPixel();