|
| 1 | +/*========================================================================= |
| 2 | + * |
| 3 | + * Copyright NumFOCUS |
| 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 |
| 41 | +main(int argc, char * argv[]) |
| 42 | +{ |
| 43 | + if (argc < 10) |
| 44 | + { |
| 45 | + std::cerr << "Missing parameters." << std::endl; |
| 46 | + std::cerr << "Usage: " << argv[0] << " inputImageFile" |
| 47 | + << " reversedInputImageFile" |
| 48 | + << " distanceMapOutputImageFile" |
| 49 | + << " watershedOutputFileName" |
| 50 | + << " segmentationResultOutputImageFile" |
| 51 | + << " binarizingRadius" |
| 52 | + << " majorityThreshold" |
| 53 | + << " watershedThreshold" |
| 54 | + << " watershedLevel" |
| 55 | + << " cleaningStructuringElementRadius" << std::endl; |
| 56 | + return EXIT_FAILURE; |
| 57 | + } |
| 58 | + |
| 59 | + constexpr unsigned int Dimension = 3; |
| 60 | + |
| 61 | + using UnsignedCharPixelType = unsigned char; |
| 62 | + using FloatPixelType = float; |
| 63 | + |
| 64 | + using InputImageType = itk::Image<UnsignedCharPixelType, Dimension>; |
| 65 | + using FloatImageType = itk::Image<FloatPixelType, Dimension>; |
| 66 | + using RGBPixelType = itk::RGBPixel<UnsignedCharPixelType>; |
| 67 | + using RGBImageType = itk::Image<RGBPixelType, Dimension>; |
| 68 | + using LabeledImageType = itk::Image<itk::IdentifierType, Dimension>; |
| 69 | + |
| 70 | + |
| 71 | + using FileReaderType = itk::ImageFileReader<InputImageType>; |
| 72 | + FileReaderType::Pointer reader = FileReaderType::New(); |
| 73 | + reader->SetFileName(argv[1]); |
| 74 | + reader->Update(); |
| 75 | + |
| 76 | + |
| 77 | + // Create bubble image: get a binarized version of the input image |
| 78 | + using VotingBinaryIterativeHoleFillingImageFilterType = |
| 79 | + itk::VotingBinaryIterativeHoleFillingImageFilter<InputImageType>; |
| 80 | + VotingBinaryIterativeHoleFillingImageFilterType::Pointer votingBinaryHoleFillingImageFilter = |
| 81 | + VotingBinaryIterativeHoleFillingImageFilterType::New(); |
| 82 | + votingBinaryHoleFillingImageFilter->SetInput(reader->GetOutput()); |
| 83 | + |
| 84 | + const unsigned int binarizingRadius = std::stoi(argv[6]); |
| 85 | + |
| 86 | + InputImageType::SizeType indexRadius; |
| 87 | + indexRadius.Fill(binarizingRadius); |
| 88 | + |
| 89 | + votingBinaryHoleFillingImageFilter->SetRadius(indexRadius); |
| 90 | + |
| 91 | + votingBinaryHoleFillingImageFilter->SetBackgroundValue(0); |
| 92 | + votingBinaryHoleFillingImageFilter->SetForegroundValue(255); |
| 93 | + |
| 94 | + const unsigned int majorityThreshold = std::stoi(argv[7]); |
| 95 | + votingBinaryHoleFillingImageFilter->SetMajorityThreshold(majorityThreshold); |
| 96 | + |
| 97 | + votingBinaryHoleFillingImageFilter->Update(); |
| 98 | + |
| 99 | + using FileWriterType = itk::ImageFileWriter<InputImageType>; |
| 100 | + FileWriterType::Pointer reversedImageWriter = FileWriterType::New(); |
| 101 | + reversedImageWriter->SetFileName(argv[2]); |
| 102 | + reversedImageWriter->SetInput(votingBinaryHoleFillingImageFilter->GetOutput()); |
| 103 | + reversedImageWriter->Update(); |
| 104 | + |
| 105 | + |
| 106 | + // Get the distance map of the input image |
| 107 | + using SignedMaurerDistanceMapImageFilterType = |
| 108 | + itk::SignedMaurerDistanceMapImageFilter<InputImageType, FloatImageType>; |
| 109 | + SignedMaurerDistanceMapImageFilterType::Pointer distanceMapImageFilter = |
| 110 | + SignedMaurerDistanceMapImageFilterType::New(); |
| 111 | + distanceMapImageFilter->SetInput(votingBinaryHoleFillingImageFilter->GetOutput()); |
| 112 | + |
| 113 | + distanceMapImageFilter->SetInsideIsPositive(false); |
| 114 | + distanceMapImageFilter->Update(); |
| 115 | + |
| 116 | + |
| 117 | + using DistanceMapFileWriterType = itk::ImageFileWriter<FloatImageType>; |
| 118 | + DistanceMapFileWriterType::Pointer distanceMapWriter = DistanceMapFileWriterType::New(); |
| 119 | + distanceMapWriter->SetFileName(argv[3]); |
| 120 | + distanceMapWriter->SetInput(distanceMapImageFilter->GetOutput()); |
| 121 | + distanceMapWriter->Update(); |
| 122 | + |
| 123 | + |
| 124 | + // Apply the watershed segmentation |
| 125 | + using WatershedFilterType = itk::WatershedImageFilter<FloatImageType>; |
| 126 | + WatershedFilterType::Pointer watershed = WatershedFilterType::New(); |
| 127 | + |
| 128 | + const float watershedThreshold = std::stod(argv[8]); |
| 129 | + const float watershedLevel = std::stod(argv[9]); |
| 130 | + |
| 131 | + watershed->SetThreshold(watershedThreshold); |
| 132 | + watershed->SetLevel(watershedLevel); |
| 133 | + |
| 134 | + watershed->SetInput(distanceMapImageFilter->GetOutput()); |
| 135 | + watershed->Update(); |
| 136 | + |
| 137 | + |
| 138 | + using RGBFilterType = itk::ScalarToRGBColormapImageFilter<LabeledImageType, RGBImageType>; |
| 139 | + RGBFilterType::Pointer colormapImageFilter = RGBFilterType::New(); |
| 140 | + colormapImageFilter->SetColormap(RGBFilterType::Jet); |
| 141 | + colormapImageFilter->SetInput(watershed->GetOutput()); |
| 142 | + colormapImageFilter->Update(); |
| 143 | + |
| 144 | + using WatershedFileWriterType = itk::ImageFileWriter<RGBImageType>; |
| 145 | + WatershedFileWriterType::Pointer watershedWriter = WatershedFileWriterType::New(); |
| 146 | + watershedWriter->SetFileName(argv[4]); |
| 147 | + watershedWriter->SetInput(colormapImageFilter->GetOutput()); |
| 148 | + watershedWriter->Update(); |
| 149 | + |
| 150 | + |
| 151 | + // Clean the segmentation image: remove small objects by performing an |
| 152 | + // opening morphological operation |
| 153 | + using StructuringElementType = |
| 154 | + itk::BinaryBallStructuringElement<LabeledImageType::PixelType, LabeledImageType::ImageDimension>; |
| 155 | + StructuringElementType structuringElement; |
| 156 | + |
| 157 | + const unsigned int cleaningStructuringElementRadius = std::stoi(argv[10]); |
| 158 | + structuringElement.SetRadius(cleaningStructuringElementRadius); |
| 159 | + structuringElement.CreateStructuringElement(); |
| 160 | + |
| 161 | + using BinaryMorphologicalOpeningImageFilterType = |
| 162 | + itk::BinaryMorphologicalOpeningImageFilter<LabeledImageType, LabeledImageType, StructuringElementType>; |
| 163 | + BinaryMorphologicalOpeningImageFilterType::Pointer openingFilter = BinaryMorphologicalOpeningImageFilterType::New(); |
| 164 | + openingFilter->SetInput(watershed->GetOutput()); |
| 165 | + openingFilter->SetKernel(structuringElement); |
| 166 | + openingFilter->Update(); |
| 167 | + |
| 168 | + |
| 169 | + using SegmentationFileWriterType = itk::ImageFileWriter<RGBImageType>; |
| 170 | + SegmentationFileWriterType::Pointer segmentationWriter = SegmentationFileWriterType::New(); |
| 171 | + segmentationWriter->SetFileName(argv[5]); |
| 172 | + segmentationWriter->SetInput(colormapImageFilter->GetOutput()); |
| 173 | + segmentationWriter->Update(); |
| 174 | + |
| 175 | + |
| 176 | + return EXIT_SUCCESS; |
| 177 | +} |
0 commit comments