diff --git a/Modules/PhotoacousticsAlgorithms/CMakeLists.txt b/Modules/PhotoacousticsAlgorithms/CMakeLists.txt index cc0d68498e..7872f0baab 100644 --- a/Modules/PhotoacousticsAlgorithms/CMakeLists.txt +++ b/Modules/PhotoacousticsAlgorithms/CMakeLists.txt @@ -1,19 +1,20 @@ set(dependencies_list MitkCore MitkAlgorithmsExt MitkOpenCVVideoSupport) IF(MITK_USE_OpenCL) add_definitions(-DPHOTOACOUSTICS_USE_GPU) set(dependencies_list ${dependencies_list} MitkOpenCL) message("Using OpenCL in PhotoacousticAlgorithms") ENDIF(MITK_USE_OpenCL) MITK_CREATE_MODULE( SUBPROJECTS DEPENDS ${dependencies_list} #AUTOLOAD_WITH MitkCore INCLUDE_DIRS PUBLIC include INTERNAL_INCLUDE_DIRS ${INCLUDE_DIRS_INTERNAL} PACKAGE_DEPENDS ITK|ITKFFT+ITKImageCompose+ITKImageIntensity OpenCV TinyXML ) add_subdirectory(test) add_subdirectory(MitkPABeamformingTool) +add_subdirectory(MitkPAResampleCropTool) diff --git a/Modules/PhotoacousticsAlgorithms/MitkPAResampleCropTool/CMakeLists.txt b/Modules/PhotoacousticsAlgorithms/MitkPAResampleCropTool/CMakeLists.txt new file mode 100644 index 0000000000..7341757828 --- /dev/null +++ b/Modules/PhotoacousticsAlgorithms/MitkPAResampleCropTool/CMakeLists.txt @@ -0,0 +1,11 @@ +OPTION(BUILD_PhotoacousticResampleCropTool "Build MiniApp for resampling and cropping of images" OFF) + +IF(BUILD_PhotoacousticResampleCropTool) + PROJECT( MitkPAResampleCropTool ) + mitk_create_executable(PAResampleCropTool + DEPENDS MitkCommandLine MitkCore MitkPhotoacousticsAlgorithms + PACKAGE_DEPENDS TinyXML + CPP_FILES PAResampleCropTool.cpp) + + install(TARGETS ${EXECUTABLE_TARGET} RUNTIME DESTINATION bin) + ENDIF() diff --git a/Modules/PhotoacousticsAlgorithms/MitkPAResampleCropTool/PAResampleCropTool.cpp b/Modules/PhotoacousticsAlgorithms/MitkPAResampleCropTool/PAResampleCropTool.cpp new file mode 100644 index 0000000000..38bcf60347 --- /dev/null +++ b/Modules/PhotoacousticsAlgorithms/MitkPAResampleCropTool/PAResampleCropTool.cpp @@ -0,0 +1,190 @@ +/*=================================================================== + +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 +#include +#include + +#include +#include +#include + +#include +#include + +struct InputParameters +{ + mitk::Image::Pointer inputImage; + std::string outputFilename; + bool verbose; + std::string settingsFile; +}; + +struct CropSettings +{ + unsigned int dimX; + unsigned int dimY; + unsigned int cutBelow; +}; + +struct ResamplingSettings +{ + double spacing[3]; +}; + + +InputParameters parseInput(int argc, char* argv[]) +{ + mitkCommandLineParser parser; + parser.setCategory("MITK-Photoacoustics"); + parser.setTitle("Mitk Photoacoustics Resample and Crop Tool"); + parser.setDescription("Reads a nrrd file as an input and crops and resamples it as set with the parameters defined in an additionally provided xml file."); + parser.setContributor("Computer Assisted Medical Interventions, DKFZ"); + + parser.setArgumentPrefix("--", "-"); + + parser.beginGroup("Required parameters"); + parser.addArgument( + "inputImage", "i", mitkCommandLineParser::InputImage, + "Input image (mitk::Image)", "input image (.nrrd file)", + us::Any(), false); + parser.addArgument( + "output", "o", mitkCommandLineParser::OutputFile, + "Output filename", "output image (.nrrd file)", + us::Any(), false); + parser.addArgument( + "settings", "s", mitkCommandLineParser::String, + "settings file", "file containing specifications (.xml file)", + us::Any(), false); + parser.endGroup(); + + parser.beginGroup("Optional parameters"); + parser.addArgument( + "verbose", "v", mitkCommandLineParser::Bool, + "Verbose Output", "Whether to produce verbose, or rather debug output. (default: false)"); + parser.endGroup(); + + InputParameters input; + + std::map parsedArgs = parser.parseArguments(argc, argv); + if (parsedArgs.size() == 0) + exit(-1); + + input.verbose = (bool)parsedArgs.count("verbose"); + MITK_INFO(input.verbose) << "### VERBOSE OUTPUT ENABLED ###"; + + if (parsedArgs.count("inputImage")) + { + MITK_INFO(input.verbose) << "Reading input image..."; + input.inputImage = mitk::IOUtil::Load(us::any_cast(parsedArgs["inputImage"])); + MITK_INFO(input.verbose) << "Reading input image...[Done]"; + } + else + mitkThrow() << "No input image given."; + + if (parsedArgs.count("output")) + input.outputFilename = us::any_cast(parsedArgs["output"]); + else + mitkThrow() << "No output image path given.."; + + if (parsedArgs.count("settings")) + input.settingsFile = us::any_cast(parsedArgs["settings"]); + else + mitkThrow() << "No settings image path given.."; + + return input; +} + +void ParseXML(std::string xmlFile, InputParameters input, CropSettings cropSet, ResamplingSettings resSet) +{ + MITK_INFO << "Loading configuration File \"" << xmlFile << "\""; + TiXmlDocument doc(xmlFile); + if (!doc.LoadFile()) + mitkThrow() << "Failed to load settings file \"" << xmlFile << "\" Error: " << doc.ErrorDesc(); + + TiXmlElement* root = doc.FirstChildElement(); + if (root == NULL) + { + mitkThrow() << "Failed to load file: No root element."; + doc.Clear(); + } + for (TiXmlElement* elem = root->FirstChildElement(); elem != NULL; elem = elem->NextSiblingElement()) + { + std::string elemName = elem->Value(); + if (elemName == "ResampleCrop") + { + cropSet.dimX = std::stoi(elem->Attribute("dimX")); + cropSet.dimY = std::stoi(elem->Attribute("dimY")); + cropSet.cutBelow = std::stoi(elem->Attribute("cutBelow")); + resSet.spacing[0] = std::stoi(elem->Attribute("spacingX")); + resSet.spacing[1] = std::stoi(elem->Attribute("spacingY")); + resSet.spacing[2] = std::stoi(elem->Attribute("spacingZ")); + } + } +} + +int main(int argc, char * argv[]) +{ + auto input = parseInput(argc, argv); + + CropSettings cropSettings{ 0,0,0 }; + ResamplingSettings resSettings{ 0,0,0 }; + + MITK_INFO << "Parsing settings XML..."; + try + { + ParseXML(input.settingsFile, input, cropSettings, resSettings); + } + catch (mitk::Exception e) + { + MITK_INFO << e; + return -1; + } + + MITK_INFO << "Parsing settings XML...[Done]"; + + MITK_INFO(input.verbose) << "Processing input image..."; + + mitk::PhotoacousticFilterService::Pointer m_FilterService = mitk::PhotoacousticFilterService::New(); + + mitk::Image::Pointer output = input.inputImage; + MITK_INFO(input.verbose) << "Resampling input image..."; + output = m_FilterService->ApplyResampling(output, resSettings.spacing); + MITK_INFO(input.verbose) << "Resampling input image...[Done]"; + + if (output->GetDimension(0) != cropSettings.dimX) + { + double outputDim[] = {(double)cropSettings.dimX, (double)output->GetDimension(1), (double)output->GetDimension(2)}; + output = m_FilterService->ApplyResamplingToDim(output, outputDim); + } + + int err = 0; + int above = input.inputImage->GetDimension(1) - cropSettings.dimY - cropSettings.cutBelow; + if (above < 0) + mitkThrow() << "Cannot crop an image to a larger dimension than before"; + + MITK_INFO(input.verbose) << "Cropping input image..."; + output = m_FilterService->ApplyCropping(output, above, cropSettings.cutBelow, 0, 0, 0, 0, &err); + MITK_INFO(input.verbose) << "Cropping input image...[Done]"; + + MITK_INFO(input.verbose) << "Saving image..."; + mitk::IOUtil::Save(output, input.outputFilename); + MITK_INFO(input.verbose) << "Saving image...[Done]"; + + MITK_INFO(input.verbose) << "Processing input image...[Done]"; +} diff --git a/Modules/PhotoacousticsAlgorithms/MitkPAResampleCropTool/files.cmake b/Modules/PhotoacousticsAlgorithms/MitkPAResampleCropTool/files.cmake new file mode 100644 index 0000000000..f8bb3ffe66 --- /dev/null +++ b/Modules/PhotoacousticsAlgorithms/MitkPAResampleCropTool/files.cmake @@ -0,0 +1,3 @@ +set(CPP_FILES + PAResampleCropTool.cpp +)