22 #ifndef otbBayesianFusionFilter_hxx
23 #define otbBayesianFusionFilter_hxx
30 template <
class TInputMultiSpectralImage,
class TInputMultiSpectralInterpImage,
class TInputPanchroImage,
class TOutputImage>
33 Superclass::Modified();
34 m_StatisticsHaveBeenGenerated =
false;
37 template <
class TInputMultiSpectralImage,
class TInputMultiSpectralInterpImage,
class TInputPanchroImage,
class TOutputImage>
40 if (!m_StatisticsHaveBeenGenerated)
42 this->ComputeInternalStatistics();
43 m_StatisticsHaveBeenGenerated =
true;
47 template <
class TInputMultiSpectralImage,
class TInputMultiSpectralInterpImage,
class TInputPanchroImage,
class TOutputImage>
55 typename OutputImageType::Pointer output = this->GetOutput();
58 typename InputPanchroImageType::Pointer panchro =
const_cast<InputPanchroImageType*
>(this->GetPanchro());
61 m_Beta.SetSize(multiSpecInterp->GetNumberOfComponentsPerPixel() + 1, 1);
62 m_Beta.Fill(itk::NumericTraits<InputMultiSpectralInterpRealType>::Zero);
65 m_CovarianceMatrix.SetSize(multiSpecInterp->GetNumberOfComponentsPerPixel(), multiSpecInterp->GetNumberOfComponentsPerPixel());
66 m_CovarianceMatrix.Fill(itk::NumericTraits<InputMultiSpectralInterpRealType>::Zero);
68 m_CovarianceInvMatrix.SetSize(multiSpecInterp->GetNumberOfComponentsPerPixel(), multiSpecInterp->GetNumberOfComponentsPerPixel());
69 m_CovarianceInvMatrix.Fill(itk::NumericTraits<InputMultiSpectralInterpRealType>::Zero);
71 m_Vcondopt.SetSize(multiSpecInterp->GetNumberOfComponentsPerPixel(), multiSpecInterp->GetNumberOfComponentsPerPixel());
72 m_Vcondopt.Fill(itk::NumericTraits<InputMultiSpectralInterpRealType>::Zero);
76 covComputefilter->SetInput(multiSpecInterp);
77 covComputefilter->Update();
79 m_CovarianceMatrix = covComputefilter->GetCovariance();
82 m_CovarianceInvMatrix = m_CovarianceMatrix.GetInverse();
86 caster->SetInput(panchro);
93 msTransposeMs->SetUsePadFirstInput(
true);
94 msTransposeMs->SetUsePadSecondInput(
true);
95 msTransposePan->SetUsePadFirstInput(
true);
98 msTransposeMs->SetFirstInput(multiSpec);
99 msTransposeMs->SetSecondInput(multiSpec);
101 msTransposePan->SetFirstInput(multiSpec);
102 msTransposePan->SetSecondInput(caster->GetOutput());
104 msTransposeMs->Update();
106 msTransposePan->Update();
107 otbMsgDebugMacro(<<
"MsTPan: " << msTransposePan->GetResultOutput()->Get());
110 temp = msTransposeMs->GetResultOutput()->Get().GetInverse();
111 m_Beta = temp * msTransposePan->GetResultOutput()->Get();
116 panTransposePan->SetFirstInput(caster->GetOutput());
117 panTransposePan->SetSecondInput(caster->GetOutput());
118 panTransposePan->Update();
119 otbMsgDebugMacro(<<
"PanTPan: " << msTransposePan->GetResultOutput()->Get());
121 S = panTransposePan->GetResultOutput()->Get();
122 tempS = msTransposePan->GetResultOutput()->Get().GetTranspose();
123 tempS = tempS * m_Beta;
129 if ((S.Rows() != tempS.Rows()) || (S.Cols() != tempS.Cols()))
131 itkExceptionMacro(<<
"Matrix with size (" << S.Rows() <<
"," << S.Cols() <<
") cannot be subtracted from matrix with size (" << tempS.Rows() <<
","
132 << tempS.Cols() <<
" )");
134 for (
unsigned int r = 0; r < S.Rows(); ++r)
136 for (
unsigned int c = 0; c < S.Cols(); ++c)
138 S(r, c) -= tempS(r, c);
144 tempS = m_Beta.GetTranspose();
145 tempS2 = msTransposePan->GetResultOutput()->Get();
146 tempS = tempS * tempS2;
151 if ((S.Rows() != tempS.Rows()) || (S.Cols() != tempS.Cols()))
153 itkExceptionMacro(<<
"Matrix with size (" << S.Rows() <<
"," << S.Cols() <<
") cannot be subtracted from matrix with size (" << tempS.Rows() <<
","
154 << tempS.Cols() <<
" )");
156 for (
unsigned int r = 0; r < S.Rows(); ++r)
158 for (
unsigned int c = 0; c < S.Cols(); ++c)
160 S(r, c) -= tempS(r, c);
167 xxT = msTransposeMs->GetResultOutput()->Get().GetTranspose();
169 xxTbT = xxTb.GetTranspose();
170 xxTbTb = xxTbT * m_Beta;
175 if ((S.Rows() != xxTbTb.Rows()) || (S.Cols() != xxTbTb.Cols()))
177 itkExceptionMacro(<<
"Matrix with size (" << S.Rows() <<
"," << S.Cols() <<
") cannot be subtracted from matrix with size (" << xxTbTb.Rows() <<
","
178 << xxTbTb.Cols() <<
" )");
182 for (
unsigned int r = 0; r < S.Rows(); ++r)
184 for (
unsigned int c = 0; c < S.Cols(); ++c)
186 S(r, c) += xxTbTb(r, c);
191 unsigned int size1 = multiSpec->GetLargestPossibleRegion().GetSize()[0] * multiSpec->GetLargestPossibleRegion().GetSize()[1];
192 unsigned int size2 = multiSpec->GetNumberOfComponentsPerPixel() + 1;
194 m_S /=
static_cast<float>(size1 - size2);
199 varPan.SetSize(multiSpecInterp->GetNumberOfComponentsPerPixel(), 1);
200 varPan.Fill(itk::NumericTraits<InputMultiSpectralInterpRealType>::Zero);
201 cutBeta.SetSize(multiSpecInterp->GetNumberOfComponentsPerPixel(), 1);
202 cutBeta.Fill(itk::NumericTraits<InputMultiSpectralInterpRealType>::Zero);
204 for (
unsigned int r = 1; r < m_Beta.Rows(); ++r)
206 cutBeta(r - 1, 0) = m_Beta(r, 0);
211 tempvarPan = varPan.GetTranspose();
212 varPan *= tempvarPan;
217 eye.SetSize(multiSpecInterp->GetNumberOfComponentsPerPixel(), multiSpecInterp->GetNumberOfComponentsPerPixel());
218 eye.Fill(itk::NumericTraits<InputMultiSpectralInterpRealType>::Zero);
219 for (
unsigned int r = 1; r < eye.Rows(); ++r)
221 eye(r, r) = std::pow(10., -12.);
228 if ((m_Vcondopt.Rows() != varPan.Rows()) || (m_Vcondopt.Cols() != varPan.Cols()) || (m_Vcondopt.Rows() != m_CovarianceInvMatrix.Rows()) ||
229 (m_Vcondopt.Cols() != m_CovarianceInvMatrix.Cols()))
231 itkExceptionMacro(<<
"Matrix with size (" << m_Vcondopt.Rows() <<
"," << m_Vcondopt.Cols() <<
") cannot be subtracted from matrix with size ("
232 << varPan.Rows() <<
"," << varPan.Cols() <<
" ) or ( " << m_CovarianceInvMatrix.Rows() <<
"," << m_CovarianceInvMatrix.Cols() <<
")");
234 for (
unsigned int r = 0; r < m_Vcondopt.Rows(); ++r)
236 for (
unsigned int c = 0; c < m_Vcondopt.Cols(); ++c)
238 m_Vcondopt(r, c) = 2 * m_Lambda * varPan(r, c) + 2 * m_CovarianceInvMatrix(r, c) * (1 - m_Lambda) + eye(r, c);
243 m_Vcondopt = m_Vcondopt.GetInverse();
245 this->GetModifiableFunctor().SetVcondopt(m_Vcondopt);
246 this->GetModifiableFunctor().SetBeta(cutBeta);
247 this->GetModifiableFunctor().SetAlpha(m_Beta(0, 0));
248 this->GetModifiableFunctor().SetCovarianceInvMatrix(m_CovarianceInvMatrix);
249 this->GetModifiableFunctor().SetLambda(m_Lambda);
250 this->GetModifiableFunctor().SetS(m_S);
254 multiSpecInterp->SetRequestedRegion(msiRequestedRegion);
255 multiSpecInterp->PropagateRequestedRegion();
256 multiSpecInterp->UpdateOutputData();
258 multiSpec->SetRequestedRegion(msRequestedRegion);
259 multiSpec->PropagateRequestedRegion();
260 multiSpec->UpdateOutputData();
262 panchro->SetRequestedRegion(panchroRequestedRegion);
263 panchro->PropagateRequestedRegion();
264 panchro->UpdateOutputData();