| 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 <set> |
| 20 | #include "itkAdaptiveNonLocalMeansDenoisingImageFilter.h" |
| 21 | |
| 22 | #include "itkImageFileReader.h" |
| 23 | #include "itkImageFileWriter.h" |
| 24 | #include "itkTestingMacros.h" |
| 25 | #include "itkMath.h" |
| 26 | |
| 27 | #include <itkNrrdImageIOFactory.h> |
| 28 | |
| 29 | template <typename TFilter> |
| 30 | class CommandProgressUpdate : public itk::Command |
| 31 | { |
| 32 | public: |
| 33 | using Self = CommandProgressUpdate<TFilter>; |
| 34 | using Superclass = itk::Command; |
| 35 | using Pointer = itk::SmartPointer<CommandProgressUpdate<TFilter>>; |
| 36 | itkNewMacro(CommandProgressUpdate); |
| 37 | |
| 38 | protected: |
| 39 | CommandProgressUpdate() = default; |
| 40 | |
| 41 | using FilterType = TFilter; |
| 42 | |
| 43 | unsigned int m_CurrentProgress{ 0 }; |
| 44 | |
| 45 | public: |
| 46 | void |
| 47 | Execute(itk::Object * caller, const itk::EventObject & event) override |
| 48 | { |
| 49 | auto * po = dynamic_cast<itk::ProcessObject *>(caller); |
| 50 | if (!po) |
| 51 | return; |
| 52 | if (typeid(event) == typeid(itk::ProgressEvent)) |
| 53 | { |
| 54 | if (this->m_CurrentProgress < 99) |
| 55 | { |
| 56 | this->m_CurrentProgress++; |
| 57 | if (this->m_CurrentProgress % 10 == 0) |
| 58 | { |
| 59 | std::cout << this->m_CurrentProgress << std::flush; |
| 60 | } |
| 61 | else |
| 62 | { |
| 63 | std::cout << "*" << std::flush; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void |
| 70 | Execute(const itk::Object * object, const itk::EventObject & event) override |
| 71 | { |
| 72 | auto * po = dynamic_cast<itk::ProcessObject *>(const_cast<itk::Object *>(object)); |
| 73 | if (!po) |
| 74 | return; |
| 75 | |
| 76 | if (typeid(event) == typeid(itk::ProgressEvent)) |
| 77 | { |
| 78 | if (this->m_CurrentProgress < 99) |
| 79 | { |
| 80 | this->m_CurrentProgress++; |
| 81 | if (this->m_CurrentProgress % 10 == 0) |
| 82 | { |
| 83 | std::cout << this->m_CurrentProgress << std::flush; |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | std::cout << "*" << std::flush; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | }; |
| 93 | |
| 94 | int |
| 95 | main(int argc, char * argv[]) |
| 96 | { |
| 97 | if (argc < 4) |
| 98 | { |
| 99 | std::cerr << "Missing parameters." << std::endl; |
| 100 | std::cerr << "Usage: " << itkNameOfTestExecutableMacro(argv) << " inputImage" |
| 101 | << " outputImage" |
| 102 | << " similarityMetric (0: PEARSON_CORRELATION; 1: MEAN_SQUARES)" << std::endl; |
| 103 | return EXIT_FAILURE; |
| 104 | } |
| 105 | |
| 106 | constexpr unsigned int Dimension = 2; |
| 107 | using PixelType = float; |
| 108 | using ImageType = itk::Image<PixelType, Dimension>; |
| 109 | |
| 110 | using ReaderType = itk::ImageFileReader<ImageType>; |
| 111 | ReaderType::Pointer reader = ReaderType::New(); |
| 112 | reader->SetFileName(argv[1]); |
| 113 | |
| 114 | using DenoiserType = itk::AdaptiveNonLocalMeansDenoisingImageFilter<ImageType, ImageType>; |
| 115 | DenoiserType::Pointer filter = DenoiserType::New(); |
| 116 | |
| 117 | itk::NrrdImageIOFactory::RegisterOneFactory(); |
| 118 | |
| 119 | ITK_EXERCISE_BASIC_OBJECT_METHODS(filter, AdaptiveNonLocalMeansDenoisingImageFilter, NonLocalPatchBasedImageFilter); |
| 120 | |
| 121 | filter->SetInput(reader->GetOutput()); |
| 122 | filter->SetUseRicianNoiseModel(false); |
| 123 | ITK_TEST_SET_GET_VALUE(false, filter->GetUseRicianNoiseModel()); |
| 124 | |
| 125 | DenoiserType::NeighborhoodRadiusType neighborhoodPatchRadius; |
| 126 | DenoiserType::NeighborhoodRadiusType neighborhoodSearchRadius; |
| 127 | |
| 128 | neighborhoodPatchRadius.Fill(1); |
| 129 | neighborhoodSearchRadius.Fill(2); |
| 130 | |
| 131 | filter->SetNeighborhoodSearchRadius(neighborhoodSearchRadius); |
| 132 | filter->SetNeighborhoodPatchRadius(neighborhoodPatchRadius); |
| 133 | |
| 134 | DenoiserType::NeighborhoodRadiusType neighborhoodRadiusForLocalMeanAndVariance; |
| 135 | neighborhoodRadiusForLocalMeanAndVariance.Fill(1); |
| 136 | |
| 137 | filter->SetNeighborhoodRadiusForLocalMeanAndVariance(neighborhoodRadiusForLocalMeanAndVariance); |
| 138 | |
| 139 | filter->SetEpsilon(0.00001f); |
| 140 | ITK_TEST_EXPECT_TRUE(itk::Math::FloatAlmostEqual(0.00001f, filter->GetEpsilon())); |
| 141 | |
| 142 | filter->SetMeanThreshold(0.95f); |
| 143 | ITK_TEST_EXPECT_TRUE(itk::Math::FloatAlmostEqual(0.95f, filter->GetMeanThreshold())); |
| 144 | |
| 145 | filter->SetVarianceThreshold(0.5f); |
| 146 | ITK_TEST_EXPECT_TRUE(itk::Math::FloatAlmostEqual(0.5f, filter->GetVarianceThreshold())); |
| 147 | |
| 148 | filter->SetSmoothingFactor(1.0f); |
| 149 | ITK_TEST_EXPECT_TRUE(itk::Math::FloatAlmostEqual(1.0f, filter->GetSmoothingFactor())); |
| 150 | |
| 151 | filter->SetSmoothingVariance(2.0f); |
| 152 | ITK_TEST_EXPECT_TRUE(itk::Math::FloatAlmostEqual(2.0f, filter->GetSmoothingVariance())); |
| 153 | |
| 154 | auto similarityMetric = static_cast<DenoiserType::SimilarityMetricEnum>(std::atoi(argv[3])); |
| 155 | filter->SetSimilarityMetric(similarityMetric); |
| 156 | ITK_TEST_SET_GET_VALUE(similarityMetric, filter->GetSimilarityMetric()); |
| 157 | |
| 158 | using CommandType = CommandProgressUpdate<DenoiserType>; |
| 159 | CommandType::Pointer observer = CommandType::New(); |
| 160 | filter->AddObserver(itk::ProgressEvent(), observer); |
| 161 | |
| 162 | ITK_TRY_EXPECT_NO_EXCEPTION(filter->Update()); |
| 163 | |
| 164 | using WriterType = itk::ImageFileWriter<ImageType>; |
| 165 | WriterType::Pointer writer = WriterType::New(); |
| 166 | writer->SetFileName(argv[2]); |
| 167 | writer->SetInput(filter->GetOutput()); |
| 168 | |
| 169 | ITK_TRY_EXPECT_NO_EXCEPTION(writer->Update()); |
| 170 | |
| 171 | |
| 172 | // Test streaming enumeration for NonLocalPatchBasedImageFilterEnums::SimilarityMetric elements |
| 173 | const std::set<itk::NonLocalPatchBasedImageFilterEnums::SimilarityMetric> allSimilarityMetric{ |
| 174 | itk::NonLocalPatchBasedImageFilterEnums::SimilarityMetric::PEARSON_CORRELATION, |
| 175 | itk::NonLocalPatchBasedImageFilterEnums::SimilarityMetric::MEAN_SQUARES |
| 176 | }; |
| 177 | for (const auto & ee : allSimilarityMetric) |
| 178 | { |
| 179 | std::cout << "STREAMED ENUM VALUE NonLocalPatchBasedImageFilterEnums::SimilarityMetric: " << ee << std::endl; |
| 180 | } |
| 181 | |
| 182 | |
| 183 | std::cout << "Test finished" << std::endl; |
| 184 | return EXIT_SUCCESS; |
| 185 | } |