|  | 
|  | 1 | +/*========================================================================= | 
|  | 2 | + * | 
|  | 3 | + *  Copyright Insight Software Consortium | 
|  | 4 | + * | 
|  | 5 | + *  Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 6 | + *  you may not use this file except in compliance with the License. | 
|  | 7 | + *  You may obtain a copy of the License at | 
|  | 8 | + * | 
|  | 9 | + *         http://www.apache.org/licenses/LICENSE-2.0.txt | 
|  | 10 | + * | 
|  | 11 | + *  Unless required by applicable law or agreed to in writing, software | 
|  | 12 | + *  distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 13 | + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 14 | + *  See the License for the specific language governing permissions and | 
|  | 15 | + *  limitations under the License. | 
|  | 16 | + * | 
|  | 17 | + *=========================================================================*/ | 
|  | 18 | + | 
|  | 19 | +#include "itkBinaryBallStructuringElement.h" | 
|  | 20 | +#include "itkBinaryMorphologicalOpeningImageFilter.h" | 
|  | 21 | +#include "itkImageFileReader.h" | 
|  | 22 | +#include "itkImageFileWriter.h" | 
|  | 23 | +#include "itkScalarToRGBColormapImageFilter.h" | 
|  | 24 | +#include "itkSignedMaurerDistanceMapImageFilter.h" | 
|  | 25 | +#include "itkVotingBinaryIterativeHoleFillingImageFilter.h" | 
|  | 26 | +#include "itkWatershedImageFilter.h" | 
|  | 27 | + | 
|  | 28 | +// Run with: | 
|  | 29 | +// ./SegmentWithWatershedAndDistanceMap <inputImageFile> | 
|  | 30 | +// <reversedInputImageFile> <distanceMapOutputImageFile> | 
|  | 31 | +// <watershedOutputFileName> <segmentationResultOutputImageFile> | 
|  | 32 | +// binarizingRadius majorityThreshold watershedThreshold watershedLevel | 
|  | 33 | +// cleaningStructuringElementRadius | 
|  | 34 | +// e.g. | 
|  | 35 | +// ./SegmentWithWatershedAndDistanceMap PlateauBorder.tif | 
|  | 36 | +// reversedInputImage.tif distanceMap.tif watershed.tif segmentationResult.tif | 
|  | 37 | +// 2 2 0.01 0.5 3 | 
|  | 38 | +// (A rule of thumb is to set the threshold to be about 1 / 100 of the level.) | 
|  | 39 | + | 
|  | 40 | +int main( int argc, char *argv[] ) | 
|  | 41 | +{ | 
|  | 42 | +  if( argc < 10 ) | 
|  | 43 | +    { | 
|  | 44 | +    std::cerr << "Missing parameters." << std::endl; | 
|  | 45 | +    std::cerr << "Usage: " << argv[0] | 
|  | 46 | +      << " inputImageFile" | 
|  | 47 | +      << " reversedInputImageFile" | 
|  | 48 | +      << " distanceMapOutputImageFile" | 
|  | 49 | +      << " watershedOutputFileName" | 
|  | 50 | +      << " segmentationResultOutputImageFile" | 
|  | 51 | +      << " binarizingRadius" | 
|  | 52 | +      << " majorityThreshold" | 
|  | 53 | +      << " watershedThreshold" | 
|  | 54 | +      << " watershedLevel" | 
|  | 55 | +      << " cleaningStructuringElementRadius" | 
|  | 56 | +      << std::endl; | 
|  | 57 | +    return EXIT_FAILURE; | 
|  | 58 | +    } | 
|  | 59 | + | 
|  | 60 | +  constexpr unsigned int Dimension = 3; | 
|  | 61 | + | 
|  | 62 | +  using UnsignedCharPixelType = unsigned char; | 
|  | 63 | +  using FloatPixelType = float; | 
|  | 64 | + | 
|  | 65 | +  using InputImageType = itk::Image< UnsignedCharPixelType, Dimension >; | 
|  | 66 | +  using FloatImageType = itk::Image< FloatPixelType, Dimension >; | 
|  | 67 | +  using RGBPixelType = itk::RGBPixel<  UnsignedCharPixelType >; | 
|  | 68 | +  using RGBImageType = itk::Image< RGBPixelType, Dimension >; | 
|  | 69 | +  using LabeledImageType = itk::Image< itk::IdentifierType, Dimension >; | 
|  | 70 | + | 
|  | 71 | + | 
|  | 72 | +  using FileReaderType = itk::ImageFileReader< InputImageType >; | 
|  | 73 | +  FileReaderType::Pointer reader = FileReaderType::New(); | 
|  | 74 | +  reader->SetFileName( argv[1] ); | 
|  | 75 | +  reader->Update(); | 
|  | 76 | + | 
|  | 77 | + | 
|  | 78 | +  // Create bubble image: get a binarized version of the input image | 
|  | 79 | +  using VotingBinaryIterativeHoleFillingImageFilterType = | 
|  | 80 | +    itk::VotingBinaryIterativeHoleFillingImageFilter< InputImageType >; | 
|  | 81 | +  VotingBinaryIterativeHoleFillingImageFilterType::Pointer votingBinaryHoleFillingImageFilter = | 
|  | 82 | +    VotingBinaryIterativeHoleFillingImageFilterType::New(); | 
|  | 83 | +  votingBinaryHoleFillingImageFilter->SetInput( reader->GetOutput() ); | 
|  | 84 | + | 
|  | 85 | +  const unsigned int binarizingRadius = std::stoi( argv[6] ); | 
|  | 86 | + | 
|  | 87 | +  InputImageType::SizeType indexRadius; | 
|  | 88 | +  indexRadius.Fill( binarizingRadius ); | 
|  | 89 | + | 
|  | 90 | +  votingBinaryHoleFillingImageFilter->SetRadius( indexRadius ); | 
|  | 91 | + | 
|  | 92 | +  votingBinaryHoleFillingImageFilter->SetBackgroundValue( 0 ); | 
|  | 93 | +  votingBinaryHoleFillingImageFilter->SetForegroundValue( 255 ); | 
|  | 94 | + | 
|  | 95 | +  const unsigned int majorityThreshold = std::stoi( argv[7] ); | 
|  | 96 | +  votingBinaryHoleFillingImageFilter->SetMajorityThreshold( majorityThreshold ); | 
|  | 97 | + | 
|  | 98 | +  votingBinaryHoleFillingImageFilter->Update(); | 
|  | 99 | + | 
|  | 100 | +  using FileWriterType = itk::ImageFileWriter< InputImageType >; | 
|  | 101 | +  FileWriterType::Pointer reversedImageWriter = FileWriterType::New(); | 
|  | 102 | +  reversedImageWriter->SetFileName( argv[2] ); | 
|  | 103 | +  reversedImageWriter->SetInput( votingBinaryHoleFillingImageFilter->GetOutput() ); | 
|  | 104 | +  reversedImageWriter->Update(); | 
|  | 105 | + | 
|  | 106 | + | 
|  | 107 | +  // Get the distance map of the input image | 
|  | 108 | +  using SignedMaurerDistanceMapImageFilterType = | 
|  | 109 | +    itk::SignedMaurerDistanceMapImageFilter< InputImageType, FloatImageType >; | 
|  | 110 | +  SignedMaurerDistanceMapImageFilterType::Pointer distanceMapImageFilter = | 
|  | 111 | +    SignedMaurerDistanceMapImageFilterType::New(); | 
|  | 112 | +  distanceMapImageFilter->SetInput( votingBinaryHoleFillingImageFilter->GetOutput() ); | 
|  | 113 | + | 
|  | 114 | +  distanceMapImageFilter->SetInsideIsPositive( false ); | 
|  | 115 | +  distanceMapImageFilter->Update(); | 
|  | 116 | + | 
|  | 117 | + | 
|  | 118 | +  using DistanceMapFileWriterType = itk::ImageFileWriter< FloatImageType >; | 
|  | 119 | +  DistanceMapFileWriterType::Pointer distanceMapWriter = DistanceMapFileWriterType::New(); | 
|  | 120 | +  distanceMapWriter->SetFileName( argv[3] ); | 
|  | 121 | +  distanceMapWriter->SetInput( distanceMapImageFilter->GetOutput() ); | 
|  | 122 | +  distanceMapWriter->Update(); | 
|  | 123 | + | 
|  | 124 | + | 
|  | 125 | +  // Apply the watershed segmentation | 
|  | 126 | +  using WatershedFilterType = itk::WatershedImageFilter< FloatImageType >; | 
|  | 127 | +  WatershedFilterType::Pointer watershed = WatershedFilterType::New(); | 
|  | 128 | + | 
|  | 129 | +  const float watershedThreshold = std::stod( argv[8] ); | 
|  | 130 | +  const float watershedLevel = std::stod( argv[9] ); | 
|  | 131 | + | 
|  | 132 | +  watershed->SetThreshold( watershedThreshold ); | 
|  | 133 | +  watershed->SetLevel( watershedLevel ); | 
|  | 134 | + | 
|  | 135 | +  watershed->SetInput( distanceMapImageFilter->GetOutput() ); | 
|  | 136 | +  watershed->Update(); | 
|  | 137 | + | 
|  | 138 | + | 
|  | 139 | +  using RGBFilterType = itk::ScalarToRGBColormapImageFilter< LabeledImageType, RGBImageType>; | 
|  | 140 | +  RGBFilterType::Pointer colormapImageFilter = RGBFilterType::New(); | 
|  | 141 | +  colormapImageFilter->SetColormap( RGBFilterType::Jet ); | 
|  | 142 | +  colormapImageFilter->SetInput( watershed->GetOutput() ); | 
|  | 143 | +  colormapImageFilter->Update(); | 
|  | 144 | + | 
|  | 145 | +  using WatershedFileWriterType = itk::ImageFileWriter< RGBImageType >; | 
|  | 146 | +  WatershedFileWriterType::Pointer watershedWriter = WatershedFileWriterType::New(); | 
|  | 147 | +  watershedWriter->SetFileName( argv[4] ); | 
|  | 148 | +  watershedWriter->SetInput( colormapImageFilter->GetOutput() ); | 
|  | 149 | +  watershedWriter->Update(); | 
|  | 150 | + | 
|  | 151 | + | 
|  | 152 | +  // Clean the segmentation image: remove small objects by performing an | 
|  | 153 | +  // opening morphological operation | 
|  | 154 | +  using StructuringElementType = | 
|  | 155 | +    itk::BinaryBallStructuringElement< LabeledImageType::PixelType, LabeledImageType::ImageDimension >; | 
|  | 156 | +  StructuringElementType structuringElement; | 
|  | 157 | + | 
|  | 158 | +  const unsigned int cleaningStructuringElementRadius = std::stoi( argv[10] ); | 
|  | 159 | +  structuringElement.SetRadius( cleaningStructuringElementRadius ); | 
|  | 160 | +  structuringElement.CreateStructuringElement(); | 
|  | 161 | + | 
|  | 162 | +  using BinaryMorphologicalOpeningImageFilterType = | 
|  | 163 | +    itk::BinaryMorphologicalOpeningImageFilter< LabeledImageType, LabeledImageType, StructuringElementType >; | 
|  | 164 | +  BinaryMorphologicalOpeningImageFilterType::Pointer openingFilter = | 
|  | 165 | +    BinaryMorphologicalOpeningImageFilterType::New(); | 
|  | 166 | +  openingFilter->SetInput( watershed->GetOutput() ); | 
|  | 167 | +  openingFilter->SetKernel( structuringElement ); | 
|  | 168 | +  openingFilter->Update(); | 
|  | 169 | + | 
|  | 170 | + | 
|  | 171 | +  using SegmentationFileWriterType = itk::ImageFileWriter< RGBImageType >; | 
|  | 172 | +  SegmentationFileWriterType::Pointer segmentationWriter = SegmentationFileWriterType::New(); | 
|  | 173 | +  segmentationWriter->SetFileName( argv[5] ); | 
|  | 174 | +  segmentationWriter->SetInput( colormapImageFilter->GetOutput() ); | 
|  | 175 | +  segmentationWriter->Update(); | 
|  | 176 | + | 
|  | 177 | + | 
|  | 178 | +  return EXIT_SUCCESS; | 
|  | 179 | +} | 
0 commit comments