diff --git a/Modules/DiffusionImaging/MiniApps/CMakeLists.txt b/Modules/DiffusionImaging/MiniApps/CMakeLists.txt index c1a29ef678..54b0581fc8 100755 --- a/Modules/DiffusionImaging/MiniApps/CMakeLists.txt +++ b/Modules/DiffusionImaging/MiniApps/CMakeLists.txt @@ -1,59 +1,60 @@ OPTION(BUILD_DiffusionMiniApps "Build commandline tools for diffusion" OFF) IF(BUILD_DiffusionMiniApps) # include necessary modules here MitkExt QmitkExt MITK_CHECK_MODULE(_RESULT DiffusionCore FiberTracking ) IF(_RESULT) MESSAGE("Warning: DiffusionMiniApps is missing ${_RESULT}") ELSE(_RESULT) MITK_USE_MODULE( DiffusionCore FiberTracking ) # needed include directories INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} ${ALL_INCLUDE_DIRECTORIES}) PROJECT( mitkDiffusionMiniApps ) # fill in the standalone executables here SET(DIFFUSIONMINIAPPS mitkDiffusionMiniApps ) # set additional files here SET(DIFFUSIONCORE_ADDITIONAL_FILES MiniAppManager.cpp FileFormatConverter.cpp TensorReconstruction.cpp QballReconstruction.cpp DiffusionIndices.cpp CopyGeometry.cpp GibbsTracking.cpp StreamlineTracking.cpp FiberProcessing.cpp TractometerAngularErrorTool.cpp TractogramAngularError.cpp FiberDirectionExtraction.cpp PeakExtraction.cpp PeaksAngularError.cpp + MultishellMethods.cpp ) # create an executable foreach tool (only one at the moment) FOREACH(tool ${DIFFUSIONMINIAPPS}) ADD_EXECUTABLE( ${tool} ${tool}.cpp ${DIFFUSIONCORE_ADDITIONAL_FILES} ) TARGET_LINK_LIBRARIES( ${tool} ${ALL_LIBRARIES} ) ENDFOREACH(tool) ENDIF() MITK_INSTALL_TARGETS(EXECUTABLES mitkDiffusionMiniApps ) ENDIF(BUILD_DiffusionMiniApps) diff --git a/Modules/DiffusionImaging/MiniApps/MultishellMethods.cpp b/Modules/DiffusionImaging/MiniApps/MultishellMethods.cpp new file mode 100644 index 0000000000..0f36097699 --- /dev/null +++ b/Modules/DiffusionImaging/MiniApps/MultishellMethods.cpp @@ -0,0 +1,229 @@ +/*=================================================================== + +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 "MiniAppManager.h" + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include "ctkCommandLineParser.h" +#include + +#include +#include +#include +#include +#include +#include + +int MultishellMethods(int argc, char* argv[]) +{ + ctkCommandLineParser parser; + parser.setArgumentPrefix("--", "-"); + parser.addArgument("in", "i", ctkCommandLineParser::String, "input file", us::Any(), false); + parser.addArgument("out", "o", ctkCommandLineParser::String, "output file", us::Any(), false); + parser.addArgument("adc", "D", ctkCommandLineParser::Bool, "ADC Average", us::Any(), false); + parser.addArgument("akc", "K", ctkCommandLineParser::Bool, "Kurtosis Fit", us::Any(), false); + parser.addArgument("biexp", "B", ctkCommandLineParser::Bool, "BiExp fit", us::Any(), false); + parser.addArgument("targetbvalue", "b", ctkCommandLineParser::String, "target bValue (mean, min, max)", us::Any(), false); + + map parsedArgs = parser.parseArguments(argc, argv); + if (parsedArgs.size()==0) + return EXIT_FAILURE; + + // mandatory arguments + string inName = us::any_cast(parsedArgs["in"]); + string outName = us::any_cast(parsedArgs["out"]); + bool applyADC = us::any_cast(parsedArgs["adc"]); + bool applyAKC = us::any_cast(parsedArgs["akc"]); + bool applyBiExp = us::any_cast(parsedArgs["biexp"]); + string targetType = us::any_cast(parsedArgs["targetbvalue"]); + + try + { + RegisterDiffusionCoreObjectFactory(); + RegisterFiberTrackingObjectFactory(); + + MITK_INFO << "Loading " << inName; + const std::string s1="", s2=""; + std::vector infile = mitk::BaseDataIO::LoadBaseDataFromFile( inName, s1, s2, false ); + mitk::BaseData::Pointer baseData = infile.at(0); + + if ( dynamic_cast*>(baseData.GetPointer()) ) + { + MITK_INFO << "Writing " << outName; + mitk::DiffusionImage::Pointer dwi = dynamic_cast*>(baseData.GetPointer()); + typedef itk::RadialMultishellToSingleshellImageFilter FilterType; + + typedef itk::DwiGradientLengthCorrectionFilter CorrectionFilterType; + + CorrectionFilterType::Pointer roundfilter = CorrectionFilterType::New(); + roundfilter->SetRoundingValue( 1000 ); + roundfilter->SetReferenceBValue(dwi->GetB_Value()); + roundfilter->SetReferenceGradientDirectionContainer(dwi->GetDirections()); + roundfilter->Update(); + + dwi->SetB_Value( roundfilter->GetNewBValue() ); + dwi->SetDirections( roundfilter->GetOutputGradientDirectionContainer()); + + // filter input parameter + const mitk::DiffusionImage::BValueMap + &originalShellMap = dwi->GetB_ValueMap(); + + const mitk::DiffusionImage::ImageType + *vectorImage = dwi->GetVectorImage(); + + const mitk::DiffusionImage::GradientDirectionContainerType::Pointer + gradientContainer = dwi->GetDirections(); + + const unsigned int + &bValue = dwi->GetB_Value(); + + // filter call + + + vnl_vector bValueList(originalShellMap.size()-1); + double targetBValue = bValueList.mean(); + + mitk::DiffusionImage::BValueMap::const_iterator it = originalShellMap.begin(); + ++it; int i = 0 ; + for(; it != originalShellMap.end(); ++it) + bValueList.put(i++,it->first); + + if( targetType == "mean" ) + targetBValue = bValueList.mean(); + else if( targetType == "min" ) + targetBValue = bValueList.min_value(); + else if( targetType == "max" ) + targetBValue = bValueList.max_value(); + + if(applyADC) + { + FilterType::Pointer filter = FilterType::New(); + filter->SetInput(vectorImage); + filter->SetOriginalGradientDirections(gradientContainer); + filter->SetOriginalBValueMap(originalShellMap); + filter->SetOriginalBValue(bValue); + + itk::ADCAverageFunctor::Pointer functor = itk::ADCAverageFunctor::New(); + functor->setListOfBValues(bValueList); + functor->setTargetBValue(targetBValue); + + filter->SetFunctor(functor); + filter->Update(); + // create new DWI image + mitk::DiffusionImage::Pointer outImage = mitk::DiffusionImage::New(); + outImage->SetVectorImage( filter->GetOutput() ); + outImage->SetB_Value( targetBValue ); + outImage->SetDirections( filter->GetTargetGradientDirections() ); + outImage->InitializeFromVectorImage(); + + mitk::NrrdDiffusionImageWriter::Pointer writer = mitk::NrrdDiffusionImageWriter::New(); + writer->SetFileName((string(outName) + "_ADC.dwi")); + writer->SetInput(outImage); + writer->Update(); + } + if(applyAKC) + { + FilterType::Pointer filter = FilterType::New(); + filter->SetInput(vectorImage); + filter->SetOriginalGradientDirections(gradientContainer); + filter->SetOriginalBValueMap(originalShellMap); + filter->SetOriginalBValue(bValue); + + itk::KurtosisFitFunctor::Pointer functor = itk::KurtosisFitFunctor::New(); + functor->setListOfBValues(bValueList); + functor->setTargetBValue(targetBValue); + + filter->SetFunctor(functor); + filter->Update(); + // create new DWI image + mitk::DiffusionImage::Pointer outImage = mitk::DiffusionImage::New(); + outImage->SetVectorImage( filter->GetOutput() ); + outImage->SetB_Value( targetBValue ); + outImage->SetDirections( filter->GetTargetGradientDirections() ); + outImage->InitializeFromVectorImage(); + + mitk::NrrdDiffusionImageWriter::Pointer writer = mitk::NrrdDiffusionImageWriter::New(); + writer->SetFileName((string(outName) + "_AKC.dwi")); + writer->SetInput(outImage); + writer->Update(); + } + if(applyBiExp) + { + FilterType::Pointer filter = FilterType::New(); + filter->SetInput(vectorImage); + filter->SetOriginalGradientDirections(gradientContainer); + filter->SetOriginalBValueMap(originalShellMap); + filter->SetOriginalBValue(bValue); + + itk::BiExpFitFunctor::Pointer functor = itk::BiExpFitFunctor::New(); + functor->setListOfBValues(bValueList); + functor->setTargetBValue(targetBValue); + + filter->SetFunctor(functor); + filter->Update(); + // create new DWI image + mitk::DiffusionImage::Pointer outImage = mitk::DiffusionImage::New(); + outImage->SetVectorImage( filter->GetOutput() ); + outImage->SetB_Value( targetBValue ); + outImage->SetDirections( filter->GetTargetGradientDirections() ); + outImage->InitializeFromVectorImage(); + + mitk::NrrdDiffusionImageWriter::Pointer writer = mitk::NrrdDiffusionImageWriter::New(); + writer->SetFileName((string(outName) + "_BiExp.dwi")); + writer->SetInput(outImage); + writer->Update(); + } + } + } + catch (itk::ExceptionObject e) + { + MITK_INFO << e; + return EXIT_FAILURE; + } + catch (std::exception e) + { + MITK_INFO << e.what(); + return EXIT_FAILURE; + } + catch (...) + { + MITK_INFO << "ERROR!?!"; + return EXIT_FAILURE; + } + MITK_INFO << "DONE"; + return EXIT_SUCCESS; +} +RegisterDiffusionMiniApp(MultishellMethods);