diff --git a/Modules/PhotoacousticsLib/MitkSpectralUnmixing/ReadMe.txt b/Modules/PhotoacousticsLib/MitkSpectralUnmixing/ReadMe.txt new file mode 100644 index 0000000000..71f9877690 --- /dev/null +++ b/Modules/PhotoacousticsLib/MitkSpectralUnmixing/ReadMe.txt @@ -0,0 +1,15 @@ +/** + \spectral unmixing mini App (SUMA) + + \brief The SUMA is designed to enable batch processing for spectral unmixing. To use the SUMA one has to build the MitkSpectralUnmixingApp located in the PAlib. Afterwards a .bat script is available int the suberbuild/MITK-build/bin. This script takes 4 required and one optional parameter: + +Required parameters + -i, --inputFilename, input filename // "E:/mydata/awesome_exp/first_image.nrrd" + -o, --outputFileStruct, input save name // "E:/mydata/awesome_exp_unmixed/first_image_ctr" will be saved as "_HbO2_SU_.nrrd", "_Hb_SU_.nrrd" and "_sO2_.nrrd"; it is recommended to add an counter (ctr) to the savenames otherwise they will be overwritten. + -l, --inputWavelengths, input wavelengths // (int) 299 < wavelength < 1001 with format: int blank int blank int blank ... + -a, --inputAlg, input algorithm // insert alg: "QR", "NNLS", "WLS", "SVD", "LU" + -w, --inputWeights, input weights (optional) // int weights in % format and order corresponding to the wavelength: int blank int blank ... + +To costumize the SUMA for batch processing of a whole directory, you can either write a script which calls the SUMA for every image again oR insert a loop arround the actual unmixing in the code entering your file structure (see comments inside the code). + +For further questions please contact Niklas Holzwarth (n.holzwarth@dkfz-heidelberg.de or niklas.holzwarth@gmail.com) \ No newline at end of file diff --git a/Modules/PhotoacousticsLib/MitkSpectralUnmixing/SpectralUnmixingApp.cpp b/Modules/PhotoacousticsLib/MitkSpectralUnmixing/SpectralUnmixingApp.cpp index c87a60cd41..0a84953cfd 100644 --- a/Modules/PhotoacousticsLib/MitkSpectralUnmixing/SpectralUnmixingApp.cpp +++ b/Modules/PhotoacousticsLib/MitkSpectralUnmixing/SpectralUnmixingApp.cpp @@ -1,283 +1,289 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include #include #include "mitkPALinearSpectralUnmixingFilter.h" #include "mitkPASpectralUnmixingFilterBase.h" #include "mitkPASpectralUnmixingFilterVigra.h" #include "mitkPASpectralUnmixingSO2.h" #include #include #include #include #include #include "mitkPreferenceListReaderOptionsFunctor.h" +/* \brief The spectral unmixing mini app (SUMA) is designed to enable batch processing + for spectral unmixing. For detailed documentation look into the header files of the + included spectral unmixing filers.*/ + struct InputParameters { std::string inputFilename; - std::string outputFileStruct; + std::string outputFileStruct; // "E:/mydata/awesome_exp_unmixed/a" will be saved as "a_HbO2_SU_.nrrd", "a_Hb_SU_.nrrd" and "a_sO2_.nrrd"; std::string inputAlg; mitkCommandLineParser::StringContainerType inputWavelengths; mitkCommandLineParser::StringContainerType inputWeights; }; - InputParameters parseInput(int argc, char *argv[]) { MITK_INFO << "Parsing arguments..."; mitkCommandLineParser parser; parser.setCategory("MITK-Photoacoustics"); parser.setTitle("Mitk Spectral Unmixing App"); parser.setDescription("Batch processing for spectral unmixing."); parser.setContributor("Computer Assisted Medical Interventions, DKFZ"); parser.setArgumentPrefix("--", "-"); parser.beginGroup("Required parameters"); parser.addArgument("inputFilename", "i", mitkCommandLineParser::InputDirectory, "Input Filename (NAME.nrrd)", "input filename", us::Any(), false); parser.addArgument("outputFileStruct", "o", mitkCommandLineParser::OutputDirectory, "Input save name (name without ending!)", "input save name", us::Any(), false); parser.addArgument("inputWavelengths", "l", mitkCommandLineParser::StringList, "Input wavelengths (123 124 125 ... int blank int blank)", "input wavelengths", us::Any(), false); parser.addArgument("inputAlg", "a", mitkCommandLineParser::String, "Input algorithm (string)", "input algorithm", us::Any(), false); parser.addArgument("inputWeights", "w", - mitkCommandLineParser::String, + mitkCommandLineParser::StringList, "Input weights (123 124 125 ... int in % blank int in % blank)", "input weights", us::Any(), true); parser.endGroup(); InputParameters input; std::map parsedArgs = parser.parseArguments(argc, argv); if (argc == 0) exit(-1); for (int i = 0; i < argc; ++i) { MITK_INFO << argv[i]; } if (parsedArgs.count("inputFilename")) { + input.inputFilename = us::any_cast(parsedArgs["inputFilename"]); } else { - MITK_ERROR << "Error: No input"; - mitkThrow() << "Error: No input"; + MITK_ERROR << "Error: No input file"; + mitkThrow() << "Error: No input file"; } if (parsedArgs.count("outputFileStruct")) { input.outputFileStruct = us::any_cast(parsedArgs["outputFileStruct"]); } else { MITK_ERROR << "Error: No output"; mitkThrow() << "Error: No output"; } if (parsedArgs.count("inputWavelengths")) { input.inputWavelengths = us::any_cast(parsedArgs["inputWavelengths"]); } else { MITK_ERROR << "Error: No wavelengths"; mitkThrow() << "Error: No wavelengths"; } if (parsedArgs.count("inputAlg")) { input.inputAlg = us::any_cast(parsedArgs["inputAlg"]); } else { MITK_ERROR << "Error: No algorithm"; mitkThrow() << "Error: No algorithm"; } if (parsedArgs.count("inputWeights")) { input.inputWeights = us::any_cast(parsedArgs["inputWeights"]); } MITK_INFO << "Parsing arguments...[Done]"; return input; } mitk::pa::SpectralUnmixingFilterBase::Pointer GetFilterInstance(std::string algorithm, std::vector weights = std::vector()) { mitk::pa::SpectralUnmixingFilterBase::Pointer spectralUnmixingFilter; if (algorithm == "QR") { spectralUnmixingFilter = mitk::pa::LinearSpectralUnmixingFilter::New(); dynamic_cast(spectralUnmixingFilter.GetPointer()) ->SetAlgorithm(mitk::pa::LinearSpectralUnmixingFilter::AlgortihmType::HOUSEHOLDERQR); } else if (algorithm == "SVD") { spectralUnmixingFilter = mitk::pa::LinearSpectralUnmixingFilter::New(); dynamic_cast(spectralUnmixingFilter.GetPointer()) ->SetAlgorithm(mitk::pa::LinearSpectralUnmixingFilter::AlgortihmType::JACOBISVD); } else if (algorithm == "LU") { spectralUnmixingFilter = mitk::pa::LinearSpectralUnmixingFilter::New(); dynamic_cast(spectralUnmixingFilter.GetPointer()) ->SetAlgorithm(mitk::pa::LinearSpectralUnmixingFilter::AlgortihmType::FULLPIVLU); } else if (algorithm == "NNLS") { spectralUnmixingFilter = mitk::pa::SpectralUnmixingFilterVigra::New(); dynamic_cast(spectralUnmixingFilter.GetPointer()) ->SetAlgorithm(mitk::pa::SpectralUnmixingFilterVigra::VigraAlgortihmType::LARS); } else if (algorithm == "WLS") { spectralUnmixingFilter = mitk::pa::SpectralUnmixingFilterVigra::New(); dynamic_cast(spectralUnmixingFilter.GetPointer()) ->SetAlgorithm(mitk::pa::SpectralUnmixingFilterVigra::VigraAlgortihmType::WEIGHTED); std::vector weigthVec = weights; for (int i = 0; i < 3; ++i) { dynamic_cast(spectralUnmixingFilter.GetPointer()) - ->AddWeight(weigthVec[i]/100); + ->AddWeight(weigthVec[i]); } } return spectralUnmixingFilter; } int main(int argc, char *argv[]) { auto input = parseInput(argc, argv); std::string algo = input.inputAlg; - std::string inputImage = input.inputFilename; std::string outputDir = input.outputFileStruct; auto inputWls = input.inputWavelengths; std::vector wavelengths; for (int s = 0; s < inputWls.size(); ++s) { int wl = std::stoi(inputWls[s]); wavelengths.push_back(wl); MITK_INFO << "Wavelength: " << wl << "\n"; } - auto m_inputImage = mitk::IOUtil::Load(inputImage); mitk::pa::SpectralUnmixingFilterBase::Pointer m_SpectralUnmixingFilter; if (algo == "WLS") { auto inputW = input.inputWeights; std::vector Weights; for (int s = 0; s < inputW.size(); ++s) { int w = std::stoi(inputW[s]); Weights.push_back(w); MITK_INFO << "Weights: " << w << "\n"; } m_SpectralUnmixingFilter = GetFilterInstance(algo, Weights); } else { m_SpectralUnmixingFilter = GetFilterInstance(algo); } - m_SpectralUnmixingFilter->SetInput(m_inputImage); - m_SpectralUnmixingFilter->Verbose(false); m_SpectralUnmixingFilter->RelativeError(false); m_SpectralUnmixingFilter->AddChromophore(mitk::pa::PropertyCalculator::ChromophoreType::OXYGENATED); m_SpectralUnmixingFilter->AddChromophore(mitk::pa::PropertyCalculator::ChromophoreType::DEOXYGENATED); m_SpectralUnmixingFilter->AddOutputs(2); for (int wIdx = 0; wIdx < wavelengths.size(); ++wIdx) { m_SpectralUnmixingFilter->AddWavelength(wavelengths[wIdx]); MITK_INFO << wavelengths[wIdx]; } - + + //to add a batch processing: loop for a dir start here; don't forget to set a counter to the three output savenames!!! + std::string inputImage = input.inputFilename; + auto m_inputImage = mitk::IOUtil::Load(inputImage); + + m_SpectralUnmixingFilter->SetInput(m_inputImage); + m_SpectralUnmixingFilter->Update(); auto output1 = m_SpectralUnmixingFilter->GetOutput(0); auto output2 = m_SpectralUnmixingFilter->GetOutput(1); output1->SetSpacing(m_inputImage->GetGeometry()->GetSpacing()); output2->SetSpacing(m_inputImage->GetGeometry()->GetSpacing()); std::string unmixingOutputHbO2 = outputDir + "_HbO2_SU_.nrrd"; std::string unmixingOutputHb = outputDir + "_Hb_SU_.nrrd"; mitk::IOUtil::Save(output1, unmixingOutputHbO2); mitk::IOUtil::Save(output2, unmixingOutputHb); auto m_sO2 = mitk::pa::SpectralUnmixingSO2::New(); m_sO2->Verbose(false); m_sO2->SetInput(0, output1); m_sO2->SetInput(1, output2); m_sO2->Update(); mitk::Image::Pointer sO2 = m_sO2->GetOutput(0); sO2->SetSpacing(m_inputImage->GetGeometry()->GetSpacing()); std::string outputSo2 = outputDir + "_sO2_.nrrd"; mitk::IOUtil::Save(sO2, outputSo2); m_sO2 = nullptr; m_SpectralUnmixingFilter = nullptr; - + //to add a batch processing: loop for a dir end here MITK_INFO << "Spectral Unmixing DONE"; }