| 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 |
* https://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 "itkLiThresholdImageFilter.h" |
| 20 |
#include "itkHuangThresholdImageFilter.h" |
| 21 |
#include "itkIntermodesThresholdImageFilter.h" |
| 22 |
#include "itkIsoDataThresholdImageFilter.h" |
| 23 |
#include "itkKittlerIllingworthThresholdImageFilter.h" |
| 24 |
#include "itkMaximumEntropyThresholdImageFilter.h" |
| 25 |
#include "itkMomentsThresholdImageFilter.h" |
| 26 |
#include "itkOtsuThresholdImageFilter.h" |
| 27 |
#include "itkRenyiEntropyThresholdImageFilter.h" |
| 28 |
#include "itkShanbhagThresholdImageFilter.h" |
| 29 |
#include "itkTriangleThresholdImageFilter.h" |
| 30 |
#include "itkYenThresholdImageFilter.h" |
| 31 |
|
| 32 |
#include "itkConnectedComponentImageFilter.h" |
| 33 |
#include "itkLabelToRGBImageFilter.h" |
| 34 |
#include "itkImageFileReader.h" |
| 35 |
|
| 36 |
#include "itksys/SystemTools.hxx" |
| 37 |
#include <sstream> |
| 38 |
#include <map> |
| 39 |
#include "QuickView.h" |
| 40 |
#include "itkTestingMacros.h" |
| 41 |
|
| 42 |
int |
| 43 |
itkVtkConnectedComponentImageFilterTest(int argc, char * argv[]) |
| 44 |
{ |
| 45 |
if (argc < 2) |
| 46 |
{ |
| 47 |
std::cerr << "Missing parameters." << std::endl; |
| 48 |
std::cout << "Usage: " << itkNameOfTestExecutableMacro(argv); |
| 49 |
std::cout << " inputImageFile" << std::endl; |
| 50 |
return EXIT_FAILURE; |
| 51 |
} |
| 52 |
|
| 53 |
using InputPixelType = short; |
| 54 |
using OutputPixelType = int; |
| 55 |
using RGBPixelType = itk::RGBPixel<unsigned char>; |
| 56 |
|
| 57 |
using InputImageType = itk::Image<InputPixelType, 2>; |
| 58 |
using OutputImageType = itk::Image<OutputPixelType, 2>; |
| 59 |
using RGBImageType = itk::Image<RGBPixelType, 2>; |
| 60 |
|
| 61 |
using LiFilterType = itk::LiThresholdImageFilter<InputImageType, OutputImageType>; |
| 62 |
using HuangFilterType = itk::HuangThresholdImageFilter<InputImageType, OutputImageType>; |
| 63 |
using IntermodesFilterType = itk::IntermodesThresholdImageFilter<InputImageType, OutputImageType>; |
| 64 |
using IsoDataFilterType = itk::IsoDataThresholdImageFilter<InputImageType, OutputImageType>; |
| 65 |
using KittlerIllingworthFilterType = itk::KittlerIllingworthThresholdImageFilter<InputImageType, OutputImageType>; |
| 66 |
using LiFilterType = itk::LiThresholdImageFilter<InputImageType, OutputImageType>; |
| 67 |
using MaximumEntropyFilterType = itk::MaximumEntropyThresholdImageFilter<InputImageType, OutputImageType>; |
| 68 |
using MomentsFilterType = itk::MomentsThresholdImageFilter<InputImageType, OutputImageType>; |
| 69 |
using OtsuFilterType = itk::OtsuThresholdImageFilter<InputImageType, OutputImageType>; |
| 70 |
using RenyiEntropyFilterType = itk::RenyiEntropyThresholdImageFilter<InputImageType, OutputImageType>; |
| 71 |
using ShanbhagFilterType = itk::ShanbhagThresholdImageFilter<InputImageType, OutputImageType>; |
| 72 |
using TriangleFilterType = itk::TriangleThresholdImageFilter<InputImageType, OutputImageType>; |
| 73 |
using YenFilterType = itk::YenThresholdImageFilter<InputImageType, OutputImageType>; |
| 74 |
|
| 75 |
using ConnectedComponentImageFilterType = itk::ConnectedComponentImageFilter<OutputImageType, OutputImageType>; |
| 76 |
using RGBFilterType = itk::LabelToRGBImageFilter<OutputImageType, RGBImageType>; |
| 77 |
|
| 78 |
using ReaderType = itk::ImageFileReader<InputImageType>; |
| 79 |
|
| 80 |
auto reader = ReaderType::New(); |
| 81 |
reader->SetFileName(argv[1]); |
| 82 |
|
| 83 |
QuickView viewer; |
| 84 |
viewer.SetViewPortSize(200); |
| 85 |
viewer.SetNumberOfColumns(6); |
| 86 |
|
| 87 |
viewer.AddImage(reader->GetOutput(), true, itksys::SystemTools::GetFilenameName(argv[1])); |
| 88 |
|
| 89 |
using FilterContainerType = |
| 90 |
std::map<std::string, itk::HistogramThresholdImageFilter<InputImageType, OutputImageType>::Pointer>; |
| 91 |
FilterContainerType filterContainer; |
| 92 |
|
| 93 |
filterContainer["Huang"] = HuangFilterType::New(); |
| 94 |
filterContainer["Intermodes"] = IntermodesFilterType::New(); |
| 95 |
filterContainer["IsoData"] = IsoDataFilterType::New(); |
| 96 |
filterContainer["KittlerIllingworth"] = KittlerIllingworthFilterType::New(); |
| 97 |
filterContainer["Li"] = LiFilterType::New(); |
| 98 |
filterContainer["MaximumEntropy"] = MaximumEntropyFilterType::New(); |
| 99 |
filterContainer["Moments"] = MomentsFilterType::New(); |
| 100 |
filterContainer["Otsu"] = OtsuFilterType::New(); |
| 101 |
filterContainer["RenyiEntropy"] = RenyiEntropyFilterType::New(); |
| 102 |
filterContainer["Shanbhag"] = ShanbhagFilterType::New(); |
| 103 |
filterContainer["Triangle"] = TriangleFilterType::New(); |
| 104 |
filterContainer["Yen"] = YenFilterType::New(); |
| 105 |
|
| 106 |
auto it = filterContainer.begin(); |
| 107 |
for (it = filterContainer.begin(); it != filterContainer.end(); ++it) |
| 108 |
{ |
| 109 |
it->second->SetInsideValue(255); |
| 110 |
it->second->SetOutsideValue(0); |
| 111 |
it->second->SetNumberOfHistogramBins(256); |
| 112 |
it->second->SetInput(reader->GetOutput()); |
| 113 |
it->second->Update(); |
| 114 |
|
| 115 |
auto connected = ConnectedComponentImageFilterType::New(); |
| 116 |
connected->SetInput(it->second->GetOutput()); |
| 117 |
|
| 118 |
auto rgbFilter = RGBFilterType::New(); |
| 119 |
rgbFilter->SetInput(connected->GetOutput()); |
| 120 |
std::stringstream desc; |
| 121 |
desc << it->first << " threshold = " << it->second->GetThreshold(); |
| 122 |
viewer.AddRGBImage(rgbFilter->GetOutput(), true, desc.str()); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
// For testing, turn off interaction |
| 127 |
viewer.Visualize(false); |
| 128 |
|
| 129 |
|
| 130 |
std::cout << "Test finished." << std::endl; |
| 131 |
return EXIT_SUCCESS; |
| 132 |
} |