diff --git a/Modules/Classification/CLMiniApps/CLOverlayRoiCenterOfMass.cpp b/Modules/Classification/CLMiniApps/CLOverlayRoiCenterOfMass.cpp index 2bffe322a0..3f705a8b3f 100644 --- a/Modules/Classification/CLMiniApps/CLOverlayRoiCenterOfMass.cpp +++ b/Modules/Classification/CLMiniApps/CLOverlayRoiCenterOfMass.cpp @@ -1,174 +1,178 @@ /*=================================================================== 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. ===================================================================*/ #ifndef mitkCLPolyToNrrd_cpp #define mitkCLPolyToNrrd_cpp #include "time.h" #include #include #include #include "mitkCommandLineParser.h" #include #include #include #include #include #include "QmitkRegisterClasses.h" #include "QmitkRenderWindow.h" #include "vtkRenderLargeImage.h" #include "vtkPNGWriter.h" #include #include typedef itk::Image< double, 3 > FloatImageType; typedef itk::Image< unsigned char, 3 > MaskImageType; template static void FindMostSampleSlice(itk::Image* mask, int & selectedSlice) { - int size = mask->GetLargestPossibleRegion().GetSize()[0]; + int idx = VImageDimension - 1; + + int size = mask->GetLargestPossibleRegion().GetSize()[idx]; std::vector numberOfSamples; numberOfSamples.resize(size,0); itk::ImageRegionIteratorWithIndex > mask1Iter(mask, mask->GetLargestPossibleRegion()); while (!mask1Iter.IsAtEnd()) { if (mask1Iter.Value() > 0) { - numberOfSamples[mask1Iter.GetIndex()[0]]+=1; + numberOfSamples[mask1Iter.GetIndex()[idx]]+=1; } ++mask1Iter; } selectedSlice = 0; for (int i = 0; i < numberOfSamples.size(); ++i) { if (numberOfSamples[selectedSlice] < numberOfSamples[i]) selectedSlice = i; } } static void SaveSliceOrImageAsPNG(mitk::Image::Pointer image, mitk::Image::Pointer mask, std::string path, int index) { // Create a Standalone Datastorage for the single purpose of saving screenshots.. mitk::StandaloneDataStorage::Pointer ds = mitk::StandaloneDataStorage::New(); QmitkRenderWindow renderWindow; renderWindow.GetRenderer()->SetDataStorage(ds); auto nodeI = mitk::DataNode::New(); nodeI->SetData(image); auto nodeM = mitk::DataNode::New(); nodeM->SetData(mask); ds->Add(nodeI); ds->Add(nodeM); mitk::TimeGeometry::Pointer geo = ds->ComputeBoundingGeometry3D(ds->GetAll()); - mitk::RenderingManager::GetInstance()->InitializeViews(geo); + mitk::RenderingManager::GetInstance()->InitializeViews( + mask->GetTimeGeometry(), mitk::RenderingManager::REQUEST_UPDATE_ALL, true); mitk::SliceNavigationController::Pointer sliceNaviController = renderWindow.GetSliceNavigationController(); + sliceNaviController->SetViewDirection(mitk::SliceNavigationController::Axial); unsigned int numberOfSteps = 1; if (sliceNaviController) { numberOfSteps = sliceNaviController->GetSlice()->GetSteps(); - sliceNaviController->GetSlice()->SetPos(0); + sliceNaviController->GetSlice()->SetPos(numberOfSteps-index); } renderWindow.show(); renderWindow.resize(256, 256); - if (sliceNaviController) - { - sliceNaviController->GetSlice()->SetPos(index); - } + //if (sliceNaviController) + //{ + // sliceNaviController->GetSlice()->SetPos(index); + //} renderWindow.GetRenderer()->PrepareRender(); vtkRenderWindow* renderWindow2 = renderWindow.GetVtkRenderWindow(); mitk::BaseRenderer* baserenderer = mitk::BaseRenderer::GetInstance(renderWindow2); auto vtkRender = baserenderer->GetVtkRenderer(); vtkRender->GetRenderWindow()->WaitForCompletion(); vtkRenderLargeImage* magnifier = vtkRenderLargeImage::New(); magnifier->SetInput(vtkRender); magnifier->SetMagnification(3.0); std::stringstream ss; ss << path <<".png"; std::string tmpImageName; ss >> tmpImageName; auto fileWriter = vtkPNGWriter::New(); fileWriter->SetInputConnection(magnifier->GetOutputPort()); fileWriter->SetFileName(tmpImageName.c_str()); fileWriter->Write(); fileWriter->Delete(); } int main(int argc, char* argv[]) { mitkCommandLineParser parser; parser.setArgumentPrefix("--", "-"); parser.addArgument("image", "i", mitkCommandLineParser::InputImage, "Input Image", "", us::Any(),false); parser.addArgument("mask", "m", mitkCommandLineParser::InputImage, "Input Image", "", us::Any(),false); parser.addArgument("output", "o", mitkCommandLineParser::InputImage, "Output Image", "", us::Any(),false); // Miniapp Infos parser.setCategory("Classification Tools"); parser.setTitle("Image with Overlay Plotter"); parser.setDescription("Plots "); parser.setContributor("MBI"); std::map parsedArgs = parser.parseArguments(argc, argv); std::string imagePath = us::any_cast(parsedArgs["image"]); std::string maskPath = us::any_cast(parsedArgs["mask"]); std::string outputPath = us::any_cast(parsedArgs["output"]); if (parsedArgs.size()==0) { return EXIT_FAILURE; } if ( parsedArgs.count("help") || parsedArgs.count("h")) { return EXIT_SUCCESS; } std::string version = "Version: 1.0"; MITK_INFO << version; mitk::Image::Pointer image = mitk::IOUtil::LoadImage(imagePath); mitk::Image::Pointer mask = mitk::IOUtil::LoadImage(maskPath); // Create a QTApplication and a Datastorage // This is necessary in order to save screenshots of // each image / slice. QApplication qtapplication(argc, argv); QmitkRegisterClasses(); int currentSlice = 0; AccessByItk_1(mask, FindMostSampleSlice, currentSlice); SaveSliceOrImageAsPNG(image, mask, outputPath, currentSlice); return 0; } #endif diff --git a/Modules/Classification/CLUtilities/src/MiniAppUtils/mitkGlobalImageFeaturesParameter.cpp b/Modules/Classification/CLUtilities/src/MiniAppUtils/mitkGlobalImageFeaturesParameter.cpp index 16bf5bff6f..4a0fcacdaa 100644 --- a/Modules/Classification/CLUtilities/src/MiniAppUtils/mitkGlobalImageFeaturesParameter.cpp +++ b/Modules/Classification/CLUtilities/src/MiniAppUtils/mitkGlobalImageFeaturesParameter.cpp @@ -1,192 +1,193 @@ /*=================================================================== 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 static bool fileExists(const std::string& filename) { std::ifstream infile(filename.c_str()); bool isGood = infile.good(); infile.close(); return isGood; } void mitk::cl::GlobalImageFeaturesParameter::AddParameter(mitkCommandLineParser &parser) { // Required Parameter parser.addArgument("image", "i", mitkCommandLineParser::InputImage, "Input Image", "Path to the input image file", us::Any(), false); parser.addArgument("mask", "m", mitkCommandLineParser::InputImage, "Input Mask", "Path to the mask Image that specifies the area over for the statistic (Values = 1)", us::Any(), false); parser.addArgument("output", "o", mitkCommandLineParser::OutputFile, "Output text file", "Path to output file. The output statistic is appended to this file.", us::Any(), false); // Optional Parameter parser.addArgument("logfile", "log", mitkCommandLineParser::InputFile, "Text Logfile", "Path to the location of the target log file. ", us::Any()); parser.addArgument("save-image", "save-image", mitkCommandLineParser::OutputFile, "Output Image", "If spezified, the image that is used for the analysis is saved to this location.", us::Any()); parser.addArgument("save-mask", "save-mask", mitkCommandLineParser::OutputFile, "Output Image", "If spezified, the mask that is used for the analysis is saved to this location. ", us::Any()); parser.addArgument("save-image-screenshots", "save-screenshot", mitkCommandLineParser::OutputFile, "Output Image", "If spezified, a screenshot of each slice is saved. Specify an EXISTING folder with prefix (for example ~/demo/ or ~/demo/image-", us::Any()); parser.addArgument("header", "head", mitkCommandLineParser::Bool, "Add Header (Labels) to output", "", us::Any()); parser.addArgument("first-line-header", "fl-head", mitkCommandLineParser::Bool, "Add Header (Labels) to first line of output", "", us::Any()); parser.addArgument("resample-mask", "rm", mitkCommandLineParser::Bool, "Bool", "Resamples the mask to the resolution of the input image ", us::Any()); parser.addArgument("same-space", "sp", mitkCommandLineParser::Bool, "Bool", "Set the spacing of all images to equal. Otherwise an error will be thrown. ", us::Any()); parser.addArgument("fixed-isotropic", "fi", mitkCommandLineParser::Float, "Float", "Input image resampled to fixed isotropic resolution given in mm. Should be used with resample-mask ", us::Any()); parser.addArgument("minimum-intensity", "minimum", mitkCommandLineParser::Float, "Float", "Minimum intensity. If set, it is overwritten by more specific intensity minima", us::Any()); parser.addArgument("maximum-intensity", "maximum", mitkCommandLineParser::Float, "Float", "Maximum intensity. If set, it is overwritten by more specific intensity maxima", us::Any()); parser.addArgument("bins", "bins", mitkCommandLineParser::Int, "Int", "Number of bins if bins are used. If set, it is overwritten by more specific bin count", us::Any()); } void mitk::cl::GlobalImageFeaturesParameter::ParseParameter(std::map parsedArgs) { ParseFileLocations(parsedArgs); ParseAdditionalOutputs(parsedArgs); ParseHeaderInformation(parsedArgs); ParseMaskAdaptation(parsedArgs); ParseGlobalFeatureParameter(parsedArgs); } void mitk::cl::GlobalImageFeaturesParameter::ParseFileLocations(std::map &parsedArgs) { // // Read input and output file informations // imagePath = parsedArgs["image"].ToString(); maskPath = parsedArgs["mask"].ToString(); outputPath = parsedArgs["output"].ToString(); imageFolder = itksys::SystemTools::GetFilenamePath(imagePath); imageName = itksys::SystemTools::GetFilenameName(imagePath); maskFolder = itksys::SystemTools::GetFilenamePath(maskPath); maskName = itksys::SystemTools::GetFilenameName(maskPath); } void mitk::cl::GlobalImageFeaturesParameter::ParseAdditionalOutputs(std::map &parsedArgs) { // // Read input and output file informations // useLogfile = false; if (parsedArgs.count("logfile")) { useLogfile = true; logfilePath = us::any_cast(parsedArgs["logfile"]); } writeAnalysisImage = false; if (parsedArgs.count("save-image")) { writeAnalysisImage = true; anaylsisImagePath = us::any_cast(parsedArgs["save-image"]); } writeAnalysisMask = false; if (parsedArgs.count("save-mask")) { writeAnalysisMask = true; analysisMaskPath = us::any_cast(parsedArgs["save-mask"]); } writePNGScreenshots = false; if (parsedArgs.count("save-image-screenshots")) { + writePNGScreenshots = true; pngScreenshotsPath = us::any_cast(parsedArgs["save-image-screenshots"]); std::string pngScrenshotFolderPath = itksys::SystemTools::GetFilenamePath(pngScreenshotsPath); if (pngScreenshotsPath.back() == '/' || pngScreenshotsPath.back() == '\\') { pngScrenshotFolderPath = pngScreenshotsPath; } itk::FileTools::CreateDirectory(pngScrenshotFolderPath.c_str()); } } void mitk::cl::GlobalImageFeaturesParameter::ParseHeaderInformation(std::map &parsedArgs) { // // Check if an header is required or not. Consider also first line header option. // useHeader = false; useHeaderForFirstLineOnly = false; if (parsedArgs.count("header")) { useHeader = us::any_cast(parsedArgs["header"]); } if (parsedArgs.count("first-line-header")) { useHeaderForFirstLineOnly = us::any_cast(parsedArgs["first-line-header"]); } if (useHeaderForFirstLineOnly) { useHeader = !fileExists(outputPath); } } void mitk::cl::GlobalImageFeaturesParameter::ParseMaskAdaptation(std::map &parsedArgs) { // // Parse parameters that control how the input mask is adapted to the input image // resampleMask = false; ensureSameSpace = false; resampleToFixIsotropic = false; resampleResolution = 1.0; if (parsedArgs.count("resample-mask")) { resampleMask = us::any_cast(parsedArgs["resample-mask"]); } if (parsedArgs.count("same-space")) { ensureSameSpace = us::any_cast(parsedArgs["same-space"]); } if (parsedArgs.count("fixed-isotropic")) { resampleToFixIsotropic = true; resampleResolution = us::any_cast(parsedArgs["fixed-isotropic"]); } } void mitk::cl::GlobalImageFeaturesParameter::ParseGlobalFeatureParameter(std::map &parsedArgs) { // // Parse parameters that control how the input mask is adapted to the input image // defineGlobalMinimumIntensity = false; defineGlobalMaximumIntensity = false; defineGlobalNumberOfBins = false; if (parsedArgs.count("minimum-intensity")) { defineGlobalMinimumIntensity = true; globalMinimumIntensity = us::any_cast(parsedArgs["minimum-intensity"]); } if (parsedArgs.count("maximum-intensity")) { defineGlobalMaximumIntensity = true; globalMaximumIntensity = us::any_cast(parsedArgs["maximum-intensity"]); } if (parsedArgs.count("bins")) { defineGlobalNumberOfBins = true; globalNumberOfBins = us::any_cast(parsedArgs["bins"]); } }