diff --git a/Modules/Classification/CLMiniApps/CLLungSegmentation.cpp b/Modules/Classification/CLMiniApps/CLLungSegmentation.cpp index 5a0f1ae736..5fa6eb100e 100644 --- a/Modules/Classification/CLMiniApps/CLLungSegmentation.cpp +++ b/Modules/Classification/CLMiniApps/CLLungSegmentation.cpp @@ -1,164 +1,174 @@ /*=================================================================== 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; + IndexType bestSeedIndex; 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(); + auto spacing = itkImage->GetSpacing(); + + int minimumDistance = 50 * 50 * (spacing[0] + spacing[1] + spacing[2]); for (int x = -50; x < 50; ++x) { for (int y = -50; y < 50; ++y) { for (int z = -20; z < 20; ++z) { seedIndex[0] = startIndex[0] + x; seedIndex[1] = startIndex[1] + y; seedIndex[2] = startIndex[2] + z; if (region.IsInside(seedIndex)) { if (itkImage->GetPixel(seedIndex) > 0) { - x = 100; y = 100; z = 100; + int newDistance = x*x*spacing[0] + y*y*spacing[1] + z*z*spacing[2]; + if (newDistance < minimumDistance) + { + bestSeedIndex = seedIndex; + minimumDistance = newDistance; + } } } } } } + seedIndex = bestSeedIndex; 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; }