22 #ifndef otbStreamingInnerProductVectorImageFilter_hxx
23 #define otbStreamingInnerProductVectorImageFilter_hxx
26 #include "itkImageRegionIterator.h"
27 #include "itkImageRegionConstIteratorWithIndex.h"
28 #include "itkNumericTraits.h"
29 #include "itkProgressReporter.h"
35 template <
class TInputImage>
38 this->DynamicMultiThreadingOff();
47 typename ImageType::Pointer output1 =
static_cast<ImageType*
>(this->MakeOutput(0).GetPointer());
48 this->itk::ProcessObject::SetNthOutput(0, output1.GetPointer());
49 typename MatrixObjectType::Pointer output2 =
static_cast<MatrixObjectType*
>(this->MakeOutput(1).GetPointer());
50 this->itk::ProcessObject::SetNthOutput(1, output2.GetPointer());
55 template <
class TInputImage>
61 return static_cast<itk::DataObject*
>(TInputImage::New().GetPointer());
64 return static_cast<itk::DataObject*
>(MatrixObjectType::New().GetPointer());
68 return static_cast<itk::DataObject*
>(TInputImage::New().GetPointer());
73 template <
class TInputImage>
76 return static_cast<MatrixObjectType*
>(this->itk::ProcessObject::GetOutput(1));
79 template <
class TInputImage>
83 return static_cast<const MatrixObjectType*
>(this->itk::ProcessObject::GetOutput(1));
86 template <
class TInputImage>
89 Superclass::GenerateOutputInformation();
92 this->GetOutput()->CopyInformation(this->GetInput());
93 this->GetOutput()->SetLargestPossibleRegion(this->GetInput()->GetLargestPossibleRegion());
95 if (this->GetOutput()->GetRequestedRegion().GetNumberOfPixels() == 0)
97 this->GetOutput()->SetRequestedRegion(this->GetOutput()->GetLargestPossibleRegion());
102 template <
class TInputImage>
112 template <
class TInputImage>
115 TInputImage* inputPtr =
const_cast<TInputImage*
>(this->GetInput());
116 inputPtr->UpdateOutputInformation();
118 if (this->GetOutput()->GetRequestedRegion().GetNumberOfPixels() == 0)
120 this->GetOutput()->SetRequestedRegion(this->GetOutput()->GetLargestPossibleRegion());
123 unsigned int numberOfThreads = this->GetNumberOfWorkUnits();
124 unsigned int numberOfTrainingImages = inputPtr->GetNumberOfComponentsPerPixel();
127 tempMatrix.set_size(numberOfTrainingImages, numberOfTrainingImages);
132 initMatrix.set_size(numberOfTrainingImages, numberOfTrainingImages);
134 this->GetInnerProductOutput()->Set(initMatrix);
137 template <
class TInputImage>
141 TInputImage* inputPtr =
const_cast<TInputImage*
>(this->GetInput());
142 unsigned int numberOfTrainingImages = inputPtr->GetNumberOfComponentsPerPixel();
143 unsigned int numberOfThreads = this->GetNumberOfWorkUnits();
145 innerProduct.set_size(numberOfTrainingImages, numberOfTrainingImages);
146 innerProduct.fill(0);
149 for (
unsigned int thread = 0; thread < numberOfThreads; thread++)
151 innerProduct += m_ThreadInnerProduct[thread];
157 for (
unsigned int band_x = 0; band_x < (numberOfTrainingImages - 1); ++band_x)
159 for (
unsigned int band_y = band_x + 1; band_y < numberOfTrainingImages; ++band_y)
161 innerProduct[band_x][band_y] = innerProduct[band_y][band_x];
165 if ((numberOfTrainingImages - 1) != 0)
167 innerProduct /= (numberOfTrainingImages - 1);
171 innerProduct.fill(0);
175 this->GetInnerProductOutput()->Set(innerProduct);
178 template <
class TInputImage>
186 itk::ProgressReporter progress(
this, threadId, outputRegionForThread.GetNumberOfPixels());
187 unsigned int numberOfTrainingImages = inputPtr->GetNumberOfComponentsPerPixel();
190 itk::ImageRegionConstIterator<TInputImage> it(inputPtr, outputRegionForThread);
191 if (m_CenterData ==
true)
195 while (!it.IsAtEnd())
199 for (
unsigned int i = 0; i < vectorValue.GetSize(); ++i)
201 mean +=
static_cast<double>(vectorValue[i]);
203 mean /=
static_cast<double>(vectorValue.GetSize());
206 for (
unsigned int band_x = 0; band_x < numberOfTrainingImages; ++band_x)
208 for (
unsigned int band_y = 0; band_y <= band_x; ++band_y)
210 m_ThreadInnerProduct[threadId][band_x][band_y] +=
211 (
static_cast<double>(vectorValue[band_x]) -
mean) * (
static_cast<double>(vectorValue[band_y]) -
mean);
215 progress.CompletedPixel();
222 while (!it.IsAtEnd())
226 for (
unsigned int band_x = 0; band_x < numberOfTrainingImages; ++band_x)
228 for (
unsigned int band_y = 0; band_y <= band_x; ++band_y)
230 m_ThreadInnerProduct[threadId][band_x][band_y] += (
static_cast<double>(vectorValue[band_x])) * (
static_cast<double>(vectorValue[band_y]));
234 progress.CompletedPixel();
239 template <
class TImage>
242 Superclass::PrintSelf(os, indent);
243 os << indent <<
"m_CenterData: " << m_CenterData << std::endl;
244 os << indent <<
"InnerProduct: " << this->GetInnerProductOutput()->Get() << std::endl;
std::vector< MatrixType > ArrayMatrixType
itk::SimpleDataObjectDecorator< MatrixType > MatrixObjectType
void AllocateOutputs() override
itk::ProcessObject::DataObjectPointerArraySizeType DataObjectPointerArraySizeType
void ThreadedGenerateData(const RegionType &outputRegionForThread, itk::ThreadIdType threadId) override
void PrintSelf(std::ostream &os, itk::Indent indent) const override
void Synthetize(void) override
void GenerateOutputInformation() override
vnl_matrix< double > MatrixType
TInputImage::RegionType RegionType
DataObjectPointer MakeOutput(DataObjectPointerArraySizeType idx) override
TInputImage::PixelType PixelType
MatrixObjectType * GetInnerProductOutput()
void Reset(void) override
PersistentInnerProductVectorImageFilter()
TInputImage::Pointer InputImagePointer
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.