21 #ifndef otbSparseUnmixingImageFilter_hxx
22 #define otbSparseUnmixingImageFilter_hxx
27 #include "itkProcessObject.h"
32 template <
class TInputImage,
class TOutputImage,
unsigned int VNbInputImage,
class TPrecision, Wavelet::Wavelet TMotherWaveletOperator>
35 this->SetNumberOfRequiredInputs(NumberOfInputImages);
36 this->SetNumberOfRequiredOutputs(1);
37 this->SetNthOutput(0, OutputImageListType::New());
39 m_NumberOfComponentsRequired = 0;
40 m_NumberOfHistogramBins = 100;
41 m_AngleList = AngleListType::New();
43 m_WvltFilterList = WvltFilterListType::New();
44 m_WvltFilterList->Resize(NumberOfInputImages);
45 for (
unsigned int i = 0; i < NumberOfInputImages; ++i)
47 m_WvltFilterList->SetNthElement(i, WvltFilterType::New());
48 m_WvltFilterList->GetNthElement(i)->SetNumberOfDecompositions(2);
49 m_WvltFilterList->GetNthElement(i)->SetSubsampleImageFactor(1);
52 m_AngleListFilter = AngleListFilterType::New();
53 m_AngleListFilter->SetThresholdValue(10.);
55 m_Histogram = HistogramType::New();
56 m_Transformer = TransformFilterType::New();
59 template <
class TInputImage,
class TOutputImage,
unsigned int VNbInputImage,
class TPrecision, Wavelet::Wavelet TMotherWaveletOperator>
63 this->itk::ProcessObject::SetNthInput(i,
const_cast<InputImageType*
>(img));
66 template <
class TInputImage,
class TOutputImage,
unsigned int VNbInputImage,
class TPrecision, Wavelet::Wavelet TMotherWaveletOperator>
69 if (i >= this->GetNumberOfInputs())
74 return static_cast<const InputImageType*
>(this->itk::ProcessObject::GetInput(i));
77 template <
class TInputImage,
class TOutputImage,
unsigned int VNbInputImage,
class TPrecision, Wavelet::Wavelet TMotherWaveletOperator>
80 itk::ProgressAccumulator::Pointer progress = itk::ProgressAccumulator::New();
81 progress->SetMiniPipelineFilter(
this);
83 for (
unsigned int i = 0; i < NumberOfInputImages; ++i)
85 m_WvltFilterList->GetNthElement(i)->SetInput(this->GetInput(i));
86 progress->RegisterInternalFilter(m_WvltFilterList->GetNthElement(i), 0.5f /
static_cast<float>(NumberOfInputImages));
87 m_WvltFilterList->GetNthElement(i)->Update();
89 m_AngleListFilter->SetInput(i, m_WvltFilterList->GetNthElement(i)->GetOutput());
92 progress->RegisterInternalFilter(m_AngleListFilter, 0.25f);
93 m_AngleListFilter->Update();
95 GenerateNumberOfComponentsRequired();
98 for (
unsigned int i = 0; i < NumberOfInputImages; ++i)
99 m_Transformer->SetInput(i, this->GetInput(i));
100 m_Transformer->SetAngleList(m_AngleList);
101 progress->RegisterInternalFilter(m_Transformer, 0.25f);
102 m_Transformer->Update();
104 this->GetOutput()->Resize(m_Transformer->GetOutput()->Size());
105 for (
unsigned int i = 0; i < this->GetOutput()->Size(); ++i)
106 this->GetOutput()->SetNthElement(i, m_Transformer->GetOutput()->GetNthElement(i));
109 template <
class TInputImage,
class TOutputImage,
unsigned int VNbInputImage,
class TPrecision, Wavelet::Wavelet TMotherWaveletOperator>
118 size.Fill(m_NumberOfHistogramBins);
122 m_Histogram->Initialize(size, theMin, theMax);
124 typename InternalSampleListType::Iterator angleIter = m_AngleListFilter->GetOutputSampleList()->Begin();
126 if (m_AngleListFilter->GetOutputSampleList()->Begin() == m_AngleListFilter->GetOutputSampleList()->End())
128 throw itk::ExceptionObject(__FILE__, __LINE__,
"The value of threshold is too high so that there is no angle value to consider for unmixing", ITK_LOCATION);
131 while (angleIter != m_AngleListFilter->GetOutputSampleList()->End())
133 if (!m_Histogram->IncreaseFrequencyOfMeasurement(angleIter.GetMeasurementVector(), 1))
134 std::cerr <<
"Data out of bounds\n";
140 for (
unsigned int index = 0; index < m_Histogram->GetSize()[0]; ++index )
141 std::cerr << index <<
"\t" << m_Histogram->GetMeasurementVector( index )[0]
142 <<
"\t" << m_Histogram->GetFrequency(index) <<
"\n";
147 for (
unsigned int id = 0;
id < m_Histogram->Size(); ++id)
152 bool isLocalMax =
true;
153 for (
unsigned int k = 0; k < m_Histogram->GetSize().Size(); ++k)
157 prevIdx[k] = m_Histogram->GetSize(k) - 1;
159 prevIdx[k] = curIdx[k] - 1;
163 if (
static_cast<PrecisionType>(nextIdx[k]) == m_Histogram->GetSize(k) - 1)
166 nextIdx[k] = curIdx[k] + 1;
168 double freq_prev = m_Histogram->GetFrequency(prevIdx);
169 double freq_cur = m_Histogram->GetFrequency(curIdx);
170 double freq_next = m_Histogram->GetFrequency(nextIdx);
172 if (!(freq_prev < freq_cur && freq_cur > freq_next))
181 angles->PushBack(m_Histogram->GetMeasurementVector(curIdx));
185 m_NumberOfComponentsRequired = angles->Size();
186 m_AngleList = angles;
189 std::cout << m_NumberOfComponentsRequired <<
" sources found\n";
190 for (
unsigned int i = 0; i < angles->Size(); i++)
192 std::cerr <<
"Source " << i <<
":";
193 for (
unsigned int j = 0; j < m_Histogram->GetSize().Size(); ++j)
194 std::cout <<
"\t" << angles->GetMeasurementVector(i)[j];