diff --git a/Modules/Classification/CLMiniApps/CLLungSegmentation.cpp b/Modules/Classification/CLMiniApps/CLLungSegmentation.cpp index e163908c6f..5a0f1ae736 100644 --- a/Modules/Classification/CLMiniApps/CLLungSegmentation.cpp +++ b/Modules/Classification/CLMiniApps/CLLungSegmentation.cpp @@ -1,154 +1,164 @@ /*=================================================================== 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 "mitkDicomSeriesReader.h" #include "mitkProperties.h" #include "mitkCommandLineParser.h" #include "mitkIOUtil.h" #include #include "mitkLabelSetImage.h" #include "mitkImageCast.h" #include "mitkImageTimeSelector.h" #include "mitkITKImageImport.h" #include "mitkImageAccessByItk.h" #include #include template void StartRegionGrowing(itk::Image* itkImage, mitk::Image::Pointer &result) { typedef itk::Image InputImageType; typedef typename InputImageType::IndexType IndexType; typedef itk::ConnectedThresholdImageFilter RegionGrowingFilterType; typename RegionGrowingFilterType::Pointer regionGrower = RegionGrowingFilterType::New(); // convert world coordinates to image indices IndexType startIndex; IndexType seedIndex; startIndex[0] = itkImage->GetLargestPossibleRegion().GetSize()[0]/2; startIndex[1] = itkImage->GetLargestPossibleRegion().GetSize()[1]/2; startIndex[2] = itkImage->GetLargestPossibleRegion().GetSize()[2]/2; + auto region = itkImage->GetLargestPossibleRegion(); for (int x = -50; x < 50; ++x) { for (int y = -50; y < 50; ++y) { - for (int z = -50; z < 50; ++z) + for (int z = -20; z < 20; ++z) { seedIndex[0] = startIndex[0] + x; seedIndex[1] = startIndex[1] + y; seedIndex[2] = startIndex[2] + z; - if (itkImage->GetPixel(seedIndex) > 0) + if (region.IsInside(seedIndex)) { - x = 100; y = 100; z = 100; + if (itkImage->GetPixel(seedIndex) > 0) + { + x = 100; y = 100; z = 100; + } } } } } MITK_INFO << "Seedpoint: " << seedIndex; //perform region growing in desired segmented region regionGrower->SetInput(itkImage); regionGrower->AddSeed(seedIndex); regionGrower->SetLower(1); regionGrower->SetUpper(255); try { regionGrower->Update(); } catch (const itk::ExceptionObject&) { return; // can't work } catch (...) { return; } //Store result and preview mitk::CastToMitkImage(regionGrower->GetOutput(), result); } int main(int argc, char* argv[]) { mitkCommandLineParser parser; parser.setTitle("Dicom Loader"); parser.setCategory("Preprocessing Tools"); parser.setDescription(""); parser.setContributor("MBI"); parser.setArgumentPrefix("--","-"); // Add command line argument names parser.addArgument("help", "h",mitkCommandLineParser::Bool, "Help:", "Show this help text"); parser.addArgument("input", "i", mitkCommandLineParser::InputDirectory, "Input folder:", "Input folder", us::Any(), false); parser.addArgument("output", "o", mitkCommandLineParser::OutputFile, "Output file:", "Output file",us::Any(),false); std::map parsedArgs = parser.parseArguments(argc, argv); if (parsedArgs.size()==0) return EXIT_FAILURE; // Show a help message if ( parsedArgs.count("help") || parsedArgs.count("h")) { std::cout << parser.helpText(); return EXIT_SUCCESS; } std::string inputFile = us::any_cast(parsedArgs["input"]); std::string outFileName = us::any_cast(parsedArgs["output"]); + MITK_INFO << "Start Image Loading"; + mitk::Image::Pointer image = mitk::IOUtil::LoadImage(inputFile); + MITK_INFO << "Loaded Image"; + mitk::OtsuSegmentationFilter::Pointer otsuFilter = mitk::OtsuSegmentationFilter::New(); otsuFilter->SetNumberOfThresholds(2); otsuFilter->SetValleyEmphasis(false); otsuFilter->SetNumberOfBins(128); otsuFilter->SetInput(image); try { otsuFilter->Update(); } catch (...) { mitkThrow() << "itkOtsuFilter error (image dimension must be in {2, 3} and image must not be RGB)"; } + MITK_INFO << "Calculated Otsu"; + mitk::LabelSetImage::Pointer resultImage = mitk::LabelSetImage::New(); resultImage->InitializeByLabeledImage(otsuFilter->GetOutput()); mitk::Image::Pointer rawMask = resultImage->CreateLabelMask(1); mitk::Image::Pointer pickedMask; AccessByItk_1(rawMask, StartRegionGrowing, pickedMask); mitk::MorphologicalOperations::FillHoles(pickedMask); mitk::MorphologicalOperations::Closing(pickedMask, 5, mitk::MorphologicalOperations::StructuralElementType::Ball); mitk::MorphologicalOperations::FillHoles(pickedMask); mitk::IOUtil::Save(pickedMask, outFileName); return EXIT_SUCCESS; } diff --git a/Modules/Classification/CLMiniApps/XRaxSimulationFromCT.cpp b/Modules/Classification/CLMiniApps/XRaxSimulationFromCT.cpp index 641e9388ad..25e7386938 100644 --- a/Modules/Classification/CLMiniApps/XRaxSimulationFromCT.cpp +++ b/Modules/Classification/CLMiniApps/XRaxSimulationFromCT.cpp @@ -1,224 +1,227 @@ /*=================================================================== 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 "mitkDicomSeriesReader.h" #include "mitkProperties.h" #include "mitkCommandLineParser.h" #include "mitkIOUtil.h" #include "itkImageRegionIterator.h" // MITK #include #include #include struct Params { bool invert; float zeroValue; }; template void CreateXRay(itk::Image* itkImage, mitk::Image::Pointer mask1, std::string output, Params param) { typedef itk::Image ImageType; typedef itk::Image MaskType; typedef itk::Image NewImageType; typename MaskType::Pointer itkMask = MaskType::New(); mitk::CastToItkImage(mask1, itkMask); NewImageType::SpacingType newSpacing; auto spacing = itkImage->GetSpacing(); spacing[0] = itkImage->GetSpacing()[0]; spacing[1] = itkImage->GetSpacing()[1]; spacing[2] = itkImage->GetSpacing()[2]; + spacing = itkImage->GetSpacing(); NewImageType::RegionType region1,region2,region3,region1m,region2m,region3m; NewImageType::IndexType start; start[0] = 0; start[1] = 0; NewImageType::SizeType size1, size2, size3; size1[0] = mask1->GetDimensions()[0]; size2[0] = mask1->GetDimensions()[0]; size3[0] = mask1->GetDimensions()[1]; size1[1] = mask1->GetDimensions()[1]; size2[1] = mask1->GetDimensions()[2]; size3[1] = mask1->GetDimensions()[2]; region1.SetSize(size1); region1m.SetSize(size1); region2.SetSize(size2); region2m.SetSize(size2); region3.SetSize(size3); region3m.SetSize(size3); region1.SetIndex(start); region1m.SetIndex(start); region2.SetIndex(start); region2m.SetIndex(start); region3.SetIndex(start); region3m.SetIndex(start); NewImageType::Pointer image1 = NewImageType::New(); image1->SetRegions(region1); image1->Allocate(); image1->FillBuffer(0); newSpacing[0] = spacing[0]; newSpacing[1] = spacing[1]; image1->SetSpacing(newSpacing); NewImageType::Pointer image2 = NewImageType::New(); image2->SetRegions(region2); image2->Allocate(); image2->FillBuffer(0); newSpacing[0] = spacing[0]; newSpacing[1] = spacing[2]; image2->SetSpacing(newSpacing); NewImageType::Pointer image3 = NewImageType::New(); image3->SetRegions(region3); image3->Allocate(); image3->FillBuffer(0); newSpacing[0] = spacing[1]; newSpacing[1] = spacing[2]; image3->SetSpacing(newSpacing); NewImageType::Pointer image1m = NewImageType::New(); image1m->SetRegions(region1m); image1m->Allocate(); image1m->FillBuffer(0); newSpacing[0] = spacing[0]; newSpacing[1] = spacing[1]; image1m->SetSpacing(newSpacing); NewImageType::Pointer image2m = NewImageType::New(); image2m->SetRegions(region2m); image2m->Allocate(); image2m->FillBuffer(0); newSpacing[0] = spacing[0]; newSpacing[1] = spacing[2]; image2m->SetSpacing(newSpacing); NewImageType::Pointer image3m = NewImageType::New(); image3m->SetRegions(region3m); image3m->Allocate(); image3m->FillBuffer(0); newSpacing[0] = spacing[1]; newSpacing[1] = spacing[2]; image3m->SetSpacing(newSpacing); for (unsigned int x = 0; x < mask1->GetDimensions()[0]; ++x) { for (unsigned int y = 0; y < mask1->GetDimensions()[1]; ++y) { for (unsigned int z = 0; z < mask1->GetDimensions()[2]; ++z) { NewImageType::IndexType newIndex; typename ImageType::IndexType index; index[0] = x; index[1] = y; index[2] = z; double pixel = itkImage->GetPixel(index)+1024; pixel = pixel / 1000.0; + pixel = (pixel < 0)? 0 : pixel; newIndex[0] = x; newIndex[1] = y; image1->SetPixel(newIndex, image1->GetPixel(newIndex) + pixel); newIndex[0] = x; newIndex[1] = z; image2->SetPixel(newIndex, image2->GetPixel(newIndex) + pixel); newIndex[0] = y; newIndex[1] = z; image3->SetPixel(newIndex, image3->GetPixel(newIndex) + pixel); if (itkMask->GetPixel(index) > 0 && !param.invert) { pixel = param.zeroValue + 1024; pixel = pixel / 1000.0; } if (itkMask->GetPixel(index) < 1 && param.invert) { pixel = param.zeroValue + 1024; pixel = pixel / 1000.0; } + pixel = (pixel < 0)? 0 : pixel; newIndex[0] = x; newIndex[1] = y; image1m->SetPixel(newIndex, image1m->GetPixel(newIndex) + pixel); newIndex[0] = x; newIndex[1] = z; image2m->SetPixel(newIndex, image2m->GetPixel(newIndex) + pixel); newIndex[0] = y; newIndex[1] = z; image3m->SetPixel(newIndex, image3m->GetPixel(newIndex) + pixel); } } } mitk::Image::Pointer img = mitk::ImportItkImage(image1); mitk::IOUtil::Save(img, output + "1.nrrd"); img = mitk::ImportItkImage(image2); mitk::IOUtil::Save(img, output + "2.nrrd"); img = mitk::ImportItkImage(image3); mitk::IOUtil::Save(img, output + "3.nrrd"); img = mitk::ImportItkImage(image1m); mitk::IOUtil::Save(img, output + "1m.nrrd"); img = mitk::ImportItkImage(image2m); mitk::IOUtil::Save(img, output + "2m.nrrd"); img = mitk::ImportItkImage(image3m); mitk::IOUtil::Save(img, output + "3m.nrrd"); } int main(int argc, char* argv[]) { mitkCommandLineParser parser; parser.setTitle("Dicom Loader"); parser.setCategory("Preprocessing Tools"); parser.setDescription(""); parser.setContributor("MBI"); parser.setArgumentPrefix("-","-"); // Add command line argument names parser.addArgument("help", "h",mitkCommandLineParser::Bool, "Help:", "Show this help text"); parser.addArgument("input", "i", mitkCommandLineParser::InputDirectory, "Input image:", "Input folder", us::Any(), false); parser.addArgument("mask", "m", mitkCommandLineParser::InputDirectory, "Input mask:", "Input folder", us::Any(), false); parser.addArgument("output", "o", mitkCommandLineParser::OutputFile, "Output file:", "Output file", us::Any(), false); parser.addArgument("invert", "invert", mitkCommandLineParser::Bool, "Input mask:", "Input folder", us::Any()); parser.addArgument("zero_value", "zero", mitkCommandLineParser::Float, "Output file:", "Output file", us::Any()); std::map parsedArgs = parser.parseArguments(argc, argv); if (parsedArgs.size()==0) return EXIT_FAILURE; // Show a help message if ( parsedArgs.count("help") || parsedArgs.count("h")) { std::cout << parser.helpText(); return EXIT_SUCCESS; } std::string inputImage = us::any_cast(parsedArgs["input"]); MITK_INFO << inputImage; std::string inputMask = us::any_cast(parsedArgs["mask"]); MITK_INFO << inputMask; Params param; param.invert = false; param.zeroValue = 0; if (parsedArgs.count("invert")) { param.invert = true; } if (parsedArgs.count("zero_value")) { - param.zeroValue = us::any_cast(parsedArgs["output-mode"]); + param.zeroValue = us::any_cast(parsedArgs["zero_value"]); } mitk::Image::Pointer image = mitk::IOUtil::LoadImage(inputImage); mitk::Image::Pointer mask = mitk::IOUtil::LoadImage(inputMask); AccessByItk_3(image, CreateXRay, mask, parsedArgs["output"].ToString(),param); //const mitk::Image::Pointer image = *imageIter; //mitk::IOUtil::SaveImage(image,outFileName); return EXIT_SUCCESS; }