Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkPASpectralUnmixingFilterVigra.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
14 
15 // ImageAccessor
16 #include <mitkImageReadAccessor.h>
17 #include <mitkImageWriteAccessor.h>
18 
19 //vigra
20 #include <vigra/matrix.hxx>
21 #include <vigra/regression.hxx>
22 #include <vigra/quadprog.hxx>
23 
25 {
26 }
27 
29 {
30 }
31 
33 {
34  algorithmName = inputAlgorithmName;
35 }
36 
38 {
39  double value = double(weight) / 100.0;
40  weightsvec.push_back(value);
41 }
42 
44  Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> endmemberMatrix, Eigen::VectorXf inputVector)
45 {
46  unsigned int numberOfWavelengths = endmemberMatrix.rows();
47  unsigned int numberOfChromophores = endmemberMatrix.cols();
48 
49  // writes endmemberMatrix and inputVector into vigra::Matrix<double>
50  std::vector<double> aData;
51  std::vector<double> bData;
52  for (unsigned int i = 0; i < numberOfWavelengths; ++i)
53  {
54  bData.push_back((double)inputVector(i));
55  for (unsigned int j = 0; j < numberOfChromophores; ++j)
56  aData.push_back((double)endmemberMatrix(i, j));
57  }
58  const double* aDat = aData.data();
59  const double* bDat = bData.data();
60 
61  vigra::Matrix<double> A(vigra::Shape2(numberOfWavelengths, numberOfChromophores), aDat);
62  vigra::Matrix<double> b(vigra::Shape2(numberOfWavelengths, 1), bDat);
63  vigra::Matrix<double> x(vigra::Shape2(numberOfChromophores, 1));
64 
65  if (mitk::pa::SpectralUnmixingFilterVigra::VigraAlgortihmType::LARS == algorithmName)
66  nonnegativeLeastSquares(A, b, x);
67 
68  else if (mitk::pa::SpectralUnmixingFilterVigra::VigraAlgortihmType::GOLDFARB == algorithmName)
69  {
70  vigra::linalg::Matrix<double> eye(vigra::linalg::identityMatrix<double>(numberOfChromophores)),
71  zeros(vigra::Shape2(numberOfChromophores, 1)),
72  empty,
73  U = vigra::linalg::transpose(A)*A,
74  // v= -transpose(A)*b replaced by -v used in "quadraticProgramming"
75  v = vigra::linalg::transpose(A)*b;
76  x = 0;
77  quadraticProgramming(U, -v, empty, empty, eye, zeros, x);
78  }
79 
80  else if (mitk::pa::SpectralUnmixingFilterVigra::VigraAlgortihmType::WEIGHTED == algorithmName)
81  {
82  if (weightsvec.size() != numberOfWavelengths)
83  mitkThrow() << "Number of weights and wavelengths doesn't match! OR Invalid weight!";
84  const double* weightsdat = weightsvec.data();
85  vigra::Matrix<double> weigths(vigra::Shape2(numberOfWavelengths, 1), weightsdat);
86  vigra::linalg::weightedLeastSquares(A, b, weigths, x);
87  }
88 
89  else if (mitk::pa::SpectralUnmixingFilterVigra::VigraAlgortihmType::LS == algorithmName)
90  linearSolve(A, b, x);
91 
92  else
93  mitkThrow() << "404 VIGRA ALGORITHM NOT FOUND";
94 
95  Eigen::VectorXf resultVector(numberOfChromophores);
96  for (unsigned int k = 0; k < numberOfChromophores; ++k)
97  resultVector[k] = (float)x(k, 0);
98 
99  return resultVector;
100 }
float k(1.0)
Eigen::VectorXf SpectralUnmixingAlgorithm(Eigen::Matrix< float, Eigen::Dynamic, Eigen::Dynamic > EndmemberMatrix, Eigen::VectorXf inputVector) override
overrides the baseclass method with a mehtod to calculate the spectral unmixing result vector...
VigraAlgortihmType
Contains all implemented Vigra algorithms for spectral unmixing. For detailed information of the algo...
void SetAlgorithm(VigraAlgortihmType inputAlgorithmName)
Takes a mitk::pa::SpectralUnmixingFilterVigra::VigraAlgortihmType and fix it for usage at the "Spectr...
#define mitkThrow()
void AddWeight(unsigned int weight)
AddWeight takes integers and writes them at the end of the weightsvec vector. The first call of the m...