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