| 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 | #ifndef itkAdaptiveNonLocalMeansDenoisingImageFilter_hxx |
| 19 | #define itkAdaptiveNonLocalMeansDenoisingImageFilter_hxx |
| 20 | |
| 21 | |
| 22 | #include "itkArray.h" |
| 23 | #include "itkDiscreteGaussianImageFilter.h" |
| 24 | #include "itkImageRegionConstIterator.h" |
| 25 | #include "itkImageRegionIterator.h" |
| 26 | #include "itkImageRegionIteratorWithIndex.h" |
| 27 | #include "itkMath.h" |
| 28 | #include "itkMeanImageFilter.h" |
| 29 | #include "itkNeighborhoodIterator.h" |
| 30 | #include "itkProgressReporter.h" |
| 31 | #include "itkStatisticsImageFilter.h" |
| 32 | #include "itkVarianceImageFilter.h" |
| 33 | |
| 34 | #include <numeric> |
| 35 | |
| 36 | namespace itk |
| 37 | { |
| 38 | |
| 39 | template <typename TInputImage, typename TOutputImage, typename TMaskImage> |
| 40 | AdaptiveNonLocalMeansDenoisingImageFilter<TInputImage, TOutputImage, TMaskImage>:: |
| 41 | AdaptiveNonLocalMeansDenoisingImageFilter() |
| 42 | : m_UseRicianNoiseModel(true) |
| 43 | , m_Epsilon(0.00001) |
| 44 | , m_MeanThreshold(0.95) |
| 45 | , m_VarianceThreshold(0.5) |
| 46 | , m_SmoothingFactor(1.0) |
| 47 | , m_SmoothingVariance(2.0) |
| 48 | , m_MaximumInputPixelIntensity(NumericTraits<RealType>::NonpositiveMin()) |
| 49 | , m_MinimumInputPixelIntensity(NumericTraits<RealType>::max()) |
| 50 | { |
| 51 | this->SetNumberOfRequiredInputs(1); |
| 52 | |
| 53 | this->m_MeanImage = nullptr; |
| 54 | this->m_VarianceImage = nullptr; |
| 55 | this->m_IntensitySquaredDistanceImage = nullptr; |
| 56 | this->m_ThreadContributionCountImage = nullptr; |
| 57 | |
| 58 | this->m_RicianBiasImage = nullptr; |
| 59 | |
| 60 | this->m_NeighborhoodRadiusForLocalMeanAndVariance.Fill(1); |
| 61 | this->DynamicMultiThreadingOff(); |
| 62 | } |
| 63 | |
| 64 | template <typename TInputImage, typename TOutputImage, typename TMaskImage> |
| 65 | void |
| 66 | AdaptiveNonLocalMeansDenoisingImageFilter<TInputImage, TOutputImage, TMaskImage>::BeforeThreadedGenerateData() |
| 67 | { |
| 68 | Superclass::BeforeThreadedGenerateData(); |
| 69 | |
| 70 | const InputImageType * inputImage = this->GetInput(); |
| 71 | |
| 72 | typedef MeanImageFilter<InputImageType, RealImageType> MeanImageFilterType; |
| 73 | typename MeanImageFilterType::Pointer meanImageFilter = MeanImageFilterType::New(); |
| 74 | meanImageFilter->SetInput(inputImage); |
| 75 | meanImageFilter->SetRadius(this->GetNeighborhoodRadiusForLocalMeanAndVariance()); |
| 76 | |
| 77 | this->m_MeanImage = meanImageFilter->GetOutput(); |
| 78 | this->m_MeanImage->Update(); |
| 79 | this->m_MeanImage->DisconnectPipeline(); |
| 80 | |
| 81 | typedef VarianceImageFilter<InputImageType, RealImageType> VarianceImageFilterType; |
| 82 | typename VarianceImageFilterType::Pointer varianceImageFilter = VarianceImageFilterType::New(); |
| 83 | varianceImageFilter->SetInput(inputImage); |
| 84 | varianceImageFilter->SetRadius(this->GetNeighborhoodRadiusForLocalMeanAndVariance()); |
| 85 | |
| 86 | this->m_VarianceImage = varianceImageFilter->GetOutput(); |
| 87 | this->m_VarianceImage->Update(); |
| 88 | this->m_VarianceImage->DisconnectPipeline(); |
| 89 | |
| 90 | typedef StatisticsImageFilter<InputImageType> StatsFilterType; |
| 91 | typename StatsFilterType::Pointer statsFilter = StatsFilterType::New(); |
| 92 | statsFilter->SetInput(inputImage); |
| 93 | statsFilter->Update(); |
| 94 | |
| 95 | this->m_MaximumInputPixelIntensity = static_cast<RealType>(statsFilter->GetMaximum()); |
| 96 | this->m_MinimumInputPixelIntensity = static_cast<RealType>(statsFilter->GetMinimum()); |
| 97 | |
| 98 | this->m_ThreadContributionCountImage = RealImageType::New(); |
| 99 | this->m_ThreadContributionCountImage->CopyInformation(inputImage); |
| 100 | this->m_ThreadContributionCountImage->SetRegions(inputImage->GetRequestedRegion()); |
| 101 | this->m_ThreadContributionCountImage->Allocate(true); |
| 102 | |
| 103 | if (this->m_UseRicianNoiseModel) |
| 104 | { |
| 105 | this->m_RicianBiasImage = RealImageType::New(); |
| 106 | this->m_RicianBiasImage->CopyInformation(inputImage); |
| 107 | this->m_RicianBiasImage->SetRegions(inputImage->GetRequestedRegion()); |
| 108 | this->m_RicianBiasImage->Allocate(true); |
| 109 | } |
| 110 | |
| 111 | this->AllocateOutputs(); |
| 112 | // Output buffer needs to be zero initialized |
| 113 | this->GetOutput()->FillBuffer(0.0); |
| 114 | } |
| 115 | |
| 116 | template <typename TInputImage, typename TOutputImage, typename TMaskImage> |
| 117 | void |
| 118 | AdaptiveNonLocalMeansDenoisingImageFilter<TInputImage, TOutputImage, TMaskImage>::ThreadedGenerateData( |
| 119 | const RegionType & region, |
| 120 | ThreadIdType threadId) |
| 121 | { |
| 122 | ProgressReporter progress(this, threadId, region.GetNumberOfPixels(), 100); |
| 123 | |
| 124 | const InputImageType * inputImage = this->GetInput(); |
| 125 | const MaskImageType * maskImage = this->GetMaskImage(); |
| 126 | |
| 127 | OutputImageType * outputImage = this->GetOutput(); |
| 128 | RegionType targetImageRegion = this->GetTargetImageRegion(); |
| 129 | |
| 130 | NeighborhoodOffsetListType neighborhoodPatchOffsetList = this->GetNeighborhoodPatchOffsetList(); |
| 131 | |
| 132 | NeighborhoodRadiusType neighborhoodSearchRadius = this->GetNeighborhoodSearchRadius(); |
| 133 | |
| 134 | ConstNeighborhoodIterator<RealImageType> ItV(neighborhoodSearchRadius, this->m_VarianceImage, region); |
| 135 | ConstNeighborhoodIterator<RealImageType> ItM(neighborhoodSearchRadius, this->m_MeanImage, region); |
| 136 | |
| 137 | const unsigned int neighborhoodSearchSize = this->GetNeighborhoodSearchSize(); |
| 138 | const unsigned int neighborhoodPatchSize = this->GetNeighborhoodPatchSize(); |
| 139 | |
| 140 | Array<RealType> weightedAverageIntensities(neighborhoodPatchSize); |
| 141 | |
| 142 | ItM.GoToBegin(); |
| 143 | ItV.GoToBegin(); |
| 144 | |
| 145 | while (!ItM.IsAtEnd()) |
| 146 | { |
| 147 | typename InputImageType::IndexType centerIndex = ItM.GetIndex(); |
| 148 | |
| 149 | InputPixelType inputCenterPixel = inputImage->GetPixel(centerIndex); |
| 150 | RealType meanCenterPixel = this->m_MeanImage->GetPixel(centerIndex); |
| 151 | RealType varianceCenterPixel = this->m_VarianceImage->GetPixel(centerIndex); |
| 152 | |
| 153 | RealType maxWeight = NumericTraits<RealType>::ZeroValue(); |
| 154 | RealType sumOfWeights = NumericTraits<RealType>::ZeroValue(); |
| 155 | |
| 156 | weightedAverageIntensities.Fill(NumericTraits<RealType>::ZeroValue()); |
| 157 | |
| 158 | RealType meanNeighborhoodPixel = NumericTraits<RealType>::ZeroValue(); |
| 159 | RealType varianceNeighborhoodPixel = NumericTraits<RealType>::ZeroValue(); |
| 160 | |
| 161 | if (inputCenterPixel > 0 && meanCenterPixel > this->m_Epsilon && varianceCenterPixel > this->m_Epsilon && |
| 162 | (!maskImage || maskImage->GetPixel(centerIndex) != NumericTraits<MaskPixelType>::ZeroValue())) |
| 163 | { |
| 164 | // Calculate the minimum distance |
| 165 | |
| 166 | RealType minimumDistance = NumericTraits<RealType>::max(); |
| 167 | for (unsigned int m = 0; m < neighborhoodSearchSize; m++) |
| 168 | { |
| 169 | if (!ItM.IndexInBounds(m) || m == static_cast<unsigned int>(0.5 * neighborhoodSearchSize)) |
| 170 | { |
| 171 | continue; |
| 172 | } |
| 173 | |
| 174 | IndexType neighborhoodIndex = ItM.GetIndex(m); |
| 175 | |
| 176 | if (inputImage->GetPixel(neighborhoodIndex) <= 0) |
| 177 | { |
| 178 | continue; |
| 179 | } |
| 180 | |
| 181 | meanNeighborhoodPixel = this->m_MeanImage->GetPixel(neighborhoodIndex); |
| 182 | varianceNeighborhoodPixel = this->m_VarianceImage->GetPixel(neighborhoodIndex); |
| 183 | |
| 184 | if (meanNeighborhoodPixel <= this->m_Epsilon || varianceNeighborhoodPixel <= this->m_Epsilon) |
| 185 | { |
| 186 | continue; |
| 187 | } |
| 188 | |
| 189 | const RealType meanRatio = meanCenterPixel / meanNeighborhoodPixel; |
| 190 | const RealType meanRatioInverse = (this->m_MaximumInputPixelIntensity - meanCenterPixel) / |
| 191 | (this->m_MaximumInputPixelIntensity - meanNeighborhoodPixel); |
| 192 | |
| 193 | const RealType varianceRatio = varianceCenterPixel / varianceNeighborhoodPixel; |
| 194 | |
| 195 | if (((meanRatio > this->m_MeanThreshold && |
| 196 | meanRatio < itk::NumericTraits<RealType>::OneValue() / this->m_MeanThreshold) || |
| 197 | (meanRatioInverse > this->m_MeanThreshold && |
| 198 | meanRatioInverse < itk::NumericTraits<RealType>::OneValue() / this->m_MeanThreshold)) && |
| 199 | varianceRatio > this->m_VarianceThreshold && |
| 200 | varianceRatio < itk::NumericTraits<RealType>::OneValue() / this->m_VarianceThreshold) |
| 201 | { |
| 202 | |
| 203 | RealType averageDistance = itk::NumericTraits<RealType>::ZeroValue(); |
| 204 | RealType count = itk::NumericTraits<RealType>::ZeroValue(); |
| 205 | |
| 206 | for (unsigned int n = 0; n < neighborhoodPatchSize; n++) |
| 207 | { |
| 208 | IndexType neighborhoodPatchIndex = neighborhoodIndex + neighborhoodPatchOffsetList[n]; |
| 209 | |
| 210 | if (!targetImageRegion.IsInside(neighborhoodPatchIndex)) |
| 211 | { |
| 212 | continue; |
| 213 | } |
| 214 | RealType neighborhoodInputImagePixel = static_cast<RealType>(inputImage->GetPixel(neighborhoodPatchIndex)); |
| 215 | RealType neighborhoodMeanImagePixel = this->m_MeanImage->GetPixel(neighborhoodPatchIndex); |
| 216 | averageDistance += itk::Math::sqr(neighborhoodInputImagePixel - neighborhoodMeanImagePixel); |
| 217 | |
| 218 | count += itk::NumericTraits<RealType>::OneValue(); |
| 219 | } |
| 220 | averageDistance /= count; |
| 221 | minimumDistance = std::min(averageDistance, minimumDistance); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | if (itk::Math::AlmostEquals(minimumDistance, NumericTraits<RealType>::ZeroValue())) |
| 226 | { |
| 227 | minimumDistance = NumericTraits<RealType>::OneValue(); |
| 228 | } |
| 229 | |
| 230 | // Rician correction |
| 231 | |
| 232 | if (this->m_UseRicianNoiseModel) |
| 233 | { |
| 234 | for (unsigned int n = 0; n < neighborhoodPatchSize; n++) |
| 235 | { |
| 236 | IndexType neighborhoodPatchIndex = centerIndex + neighborhoodPatchOffsetList[n]; |
| 237 | if (!targetImageRegion.IsInside(neighborhoodPatchIndex)) |
| 238 | { |
| 239 | continue; |
| 240 | } |
| 241 | |
| 242 | if (itk::Math::AlmostEquals(minimumDistance, NumericTraits<RealType>::max())) |
| 243 | { |
| 244 | this->m_RicianBiasImage->SetPixel(neighborhoodPatchIndex, 0.0); |
| 245 | } |
| 246 | else |
| 247 | { |
| 248 | this->m_RicianBiasImage->SetPixel(neighborhoodPatchIndex, minimumDistance); |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | // Patch filtering |
| 254 | |
| 255 | for (unsigned int m = 0; m < neighborhoodSearchSize; m++) |
| 256 | { |
| 257 | if (!ItM.IndexInBounds(m) || m == static_cast<unsigned int>(0.5 * neighborhoodSearchSize)) |
| 258 | { |
| 259 | continue; |
| 260 | } |
| 261 | |
| 262 | IndexType neighborhoodIndex = ItM.GetIndex(m); |
| 263 | |
| 264 | if (inputImage->GetPixel(neighborhoodIndex) <= 0) |
| 265 | { |
| 266 | continue; |
| 267 | } |
| 268 | |
| 269 | meanNeighborhoodPixel = this->m_MeanImage->GetPixel(neighborhoodIndex); |
| 270 | varianceNeighborhoodPixel = this->m_VarianceImage->GetPixel(neighborhoodIndex); |
| 271 | |
| 272 | if (meanNeighborhoodPixel <= this->m_Epsilon || varianceNeighborhoodPixel <= this->m_Epsilon) |
| 273 | { |
| 274 | continue; |
| 275 | } |
| 276 | |
| 277 | const RealType meanRatio = meanCenterPixel / meanNeighborhoodPixel; |
| 278 | const RealType meanRatioInverse = (this->m_MaximumInputPixelIntensity - meanCenterPixel) / |
| 279 | (this->m_MaximumInputPixelIntensity - meanNeighborhoodPixel); |
| 280 | |
| 281 | const RealType varianceRatio = varianceCenterPixel / varianceNeighborhoodPixel; |
| 282 | |
| 283 | if (((meanRatio > this->m_MeanThreshold && |
| 284 | meanRatio < itk::NumericTraits<RealType>::OneValue() / this->m_MeanThreshold) || |
| 285 | (meanRatioInverse > this->m_MeanThreshold && |
| 286 | meanRatioInverse < itk::NumericTraits<RealType>::OneValue() / this->m_MeanThreshold)) && |
| 287 | varianceRatio > this->m_VarianceThreshold && |
| 288 | varianceRatio < itk::NumericTraits<RealType>::OneValue() / this->m_VarianceThreshold) |
| 289 | { |
| 290 | |
| 291 | RealType averageDistance = 0.0; |
| 292 | RealType count = 0.0; |
| 293 | for (unsigned int n = 0; n < neighborhoodPatchSize; n++) |
| 294 | { |
| 295 | IndexType searchNeighborhoodPatchIndex = neighborhoodIndex + neighborhoodPatchOffsetList[n]; |
| 296 | IndexType centerNeighborhoodPatchIndex = centerIndex + neighborhoodPatchOffsetList[n]; |
| 297 | if (!targetImageRegion.IsInside(searchNeighborhoodPatchIndex) || |
| 298 | !targetImageRegion.IsInside(centerNeighborhoodPatchIndex)) |
| 299 | { |
| 300 | continue; |
| 301 | } |
| 302 | RealType distance1 = inputImage->GetPixel(searchNeighborhoodPatchIndex) - |
| 303 | this->m_MeanImage->GetPixel(searchNeighborhoodPatchIndex); |
| 304 | RealType distance2 = inputImage->GetPixel(centerNeighborhoodPatchIndex) - |
| 305 | this->m_MeanImage->GetPixel(centerNeighborhoodPatchIndex); |
| 306 | averageDistance += itk::Math::sqr(distance1 - distance2); |
| 307 | count += itk::NumericTraits<RealType>::OneValue(); |
| 308 | } |
| 309 | averageDistance /= count; |
| 310 | |
| 311 | RealType weight = itk::NumericTraits<RealType>::ZeroValue(); |
| 312 | if (averageDistance <= static_cast<RealType>(3.0) * minimumDistance) |
| 313 | { |
| 314 | weight = std::exp(-averageDistance / minimumDistance); |
| 315 | } |
| 316 | if (weight > maxWeight) |
| 317 | { |
| 318 | maxWeight = weight; |
| 319 | } |
| 320 | |
| 321 | if (weight > itk::NumericTraits<RealType>::ZeroValue()) |
| 322 | { |
| 323 | for (unsigned int n = 0; n < neighborhoodPatchSize; n++) |
| 324 | { |
| 325 | IndexType neighborhoodPatchIndex = neighborhoodIndex + neighborhoodPatchOffsetList[n]; |
| 326 | if (!targetImageRegion.IsInside(neighborhoodPatchIndex)) |
| 327 | { |
| 328 | continue; |
| 329 | } |
| 330 | if (this->m_UseRicianNoiseModel) |
| 331 | { |
| 332 | weightedAverageIntensities[n] += weight * itk::Math::sqr(inputImage->GetPixel(neighborhoodPatchIndex)); |
| 333 | } |
| 334 | else |
| 335 | { |
| 336 | weightedAverageIntensities[n] += weight * inputImage->GetPixel(neighborhoodPatchIndex); |
| 337 | } |
| 338 | } |
| 339 | sumOfWeights += weight; |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | if (itk::Math::AlmostEquals(maxWeight, NumericTraits<RealType>::ZeroValue())) |
| 345 | { |
| 346 | maxWeight = NumericTraits<RealType>::OneValue(); |
| 347 | } |
| 348 | } |
| 349 | else |
| 350 | { |
| 351 | maxWeight = NumericTraits<RealType>::OneValue(); |
| 352 | } |
| 353 | |
| 354 | for (unsigned int n = 0; n < neighborhoodPatchSize; n++) |
| 355 | { |
| 356 | IndexType neighborhoodPatchIndex = centerIndex + neighborhoodPatchOffsetList[n]; |
| 357 | if (!targetImageRegion.IsInside(neighborhoodPatchIndex)) |
| 358 | { |
| 359 | continue; |
| 360 | } |
| 361 | if (this->m_UseRicianNoiseModel) |
| 362 | { |
| 363 | weightedAverageIntensities[n] += maxWeight * itk::Math::sqr(inputImage->GetPixel(neighborhoodPatchIndex)); |
| 364 | } |
| 365 | else |
| 366 | { |
| 367 | weightedAverageIntensities[n] += maxWeight * inputImage->GetPixel(neighborhoodPatchIndex); |
| 368 | } |
| 369 | } |
| 370 | sumOfWeights += maxWeight; |
| 371 | |
| 372 | if (sumOfWeights > itk::NumericTraits<RealType>::ZeroValue()) |
| 373 | { |
| 374 | for (unsigned int n = 0; n < neighborhoodPatchSize; n++) |
| 375 | { |
| 376 | IndexType neighborhoodPatchIndex = centerIndex + neighborhoodPatchOffsetList[n]; |
| 377 | if (!targetImageRegion.IsInside(neighborhoodPatchIndex)) |
| 378 | { |
| 379 | continue; |
| 380 | } |
| 381 | typename OutputImageType::PixelType estimate = outputImage->GetPixel(neighborhoodPatchIndex); |
| 382 | estimate += (weightedAverageIntensities[n] / sumOfWeights); |
| 383 | |
| 384 | outputImage->SetPixel(neighborhoodPatchIndex, estimate); |
| 385 | this->m_ThreadContributionCountImage->SetPixel( |
| 386 | neighborhoodPatchIndex, this->m_ThreadContributionCountImage->GetPixel(neighborhoodPatchIndex) + 1); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | ++ItM; |
| 391 | ++ItV; |
| 392 | |
| 393 | progress.CompletedPixel(); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | template <typename TInputImage, typename TOutputImage, typename TMaskImage> |
| 398 | void |
| 399 | AdaptiveNonLocalMeansDenoisingImageFilter<TInputImage, TOutputImage, TMaskImage>::AfterThreadedGenerateData() |
| 400 | { |
| 401 | const MaskImageType * maskImage = this->GetMaskImage(); |
| 402 | |
| 403 | if (this->m_UseRicianNoiseModel) |
| 404 | { |
| 405 | typedef DiscreteGaussianImageFilter<RealImageType, RealImageType> SmootherType; |
| 406 | typename SmootherType::Pointer smoother = SmootherType::New(); |
| 407 | smoother->SetInput(this->m_RicianBiasImage); |
| 408 | smoother->SetVariance(this->m_SmoothingVariance); |
| 409 | smoother->SetUseImageSpacing(true); |
| 410 | smoother->Update(); |
| 411 | |
| 412 | ImageRegionConstIterator<RealImageType> ItS(smoother->GetOutput(), smoother->GetOutput()->GetRequestedRegion()); |
| 413 | ImageRegionConstIteratorWithIndex<RealImageType> ItM(this->m_MeanImage, this->m_MeanImage->GetRequestedRegion()); |
| 414 | ImageRegionIterator<RealImageType> ItB(this->m_RicianBiasImage, this->m_RicianBiasImage->GetRequestedRegion()); |
| 415 | ItS.GoToBegin(); |
| 416 | ItM.GoToBegin(); |
| 417 | ItB.GoToBegin(); |
| 418 | |
| 419 | while (!ItS.IsAtEnd()) |
| 420 | { |
| 421 | if (ItS.Get() > itk::NumericTraits<RealType>::ZeroValue() && |
| 422 | (!maskImage || maskImage->GetPixel(ItM.GetIndex()) != NumericTraits<MaskPixelType>::ZeroValue())) |
| 423 | { |
| 424 | const RealType snr = ItM.Get() / std::sqrt(ItS.Get()); |
| 425 | |
| 426 | RealType bias = static_cast<RealType>(2.0) * ItS.Get() / this->CalculateCorrectionFactor(snr); |
| 427 | |
| 428 | if (std::isnan(bias) || std::isinf(bias)) |
| 429 | { |
| 430 | bias = itk::NumericTraits<RealType>::ZeroValue(); |
| 431 | } |
| 432 | ItB.Set(bias); |
| 433 | } |
| 434 | |
| 435 | ++ItS; |
| 436 | ++ItM; |
| 437 | ++ItB; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | ImageRegionIteratorWithIndex<OutputImageType> ItO(this->GetOutput(), this->GetOutput()->GetRequestedRegion()); |
| 442 | ImageRegionConstIterator<RealImageType> ItL(this->m_ThreadContributionCountImage, |
| 443 | this->m_ThreadContributionCountImage->GetRequestedRegion()); |
| 444 | |
| 445 | for (ItO.GoToBegin(), ItL.GoToBegin(); !ItO.IsAtEnd(); ++ItO, ++ItL) |
| 446 | { |
| 447 | RealType estimate = ItO.Get(); |
| 448 | |
| 449 | if (itk::Math::FloatAlmostEqual(ItL.Get(), itk::NumericTraits<RealType>::ZeroValue())) |
| 450 | { |
| 451 | continue; |
| 452 | } |
| 453 | |
| 454 | estimate /= ItL.Get(); |
| 455 | |
| 456 | if (this->m_UseRicianNoiseModel) |
| 457 | { |
| 458 | RealType bias = this->m_RicianBiasImage->GetPixel(ItO.GetIndex()); |
| 459 | |
| 460 | estimate -= bias; |
| 461 | if (estimate < itk::NumericTraits<RealType>::ZeroValue()) |
| 462 | { |
| 463 | estimate = itk::NumericTraits<RealType>::ZeroValue(); |
| 464 | } |
| 465 | estimate = std::sqrt(estimate); |
| 466 | } |
| 467 | |
| 468 | ItO.Set(estimate); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | template <typename TInputImage, typename TOutputImage, typename TMaskImage> |
| 473 | typename AdaptiveNonLocalMeansDenoisingImageFilter<TInputImage, TOutputImage, TMaskImage>::RealType |
| 474 | AdaptiveNonLocalMeansDenoisingImageFilter<TInputImage, TOutputImage, TMaskImage>::CalculateCorrectionFactor( |
| 475 | RealType snr) |
| 476 | { |
| 477 | const RealType snrSquared = itk::Math::sqr(snr); |
| 478 | |
| 479 | RealType value = |
| 480 | static_cast<RealType>(2.0) + snrSquared - |
| 481 | static_cast<RealType>(0.125) * static_cast<RealType>(Math::pi) * |
| 482 | static_cast<RealType>(std::exp(static_cast<RealType>(-0.5) * snrSquared)) * |
| 483 | itk::Math::sqr((static_cast<RealType>(2.0) + snrSquared) * |
| 484 | static_cast<RealType>( |
| 485 | this->m_ModifiedBesselCalculator.ModifiedBesselI0(static_cast<RealType>(0.25) * snrSquared)) + |
| 486 | snrSquared * static_cast<RealType>(this->m_ModifiedBesselCalculator.ModifiedBesselI1( |
| 487 | static_cast<RealType>(0.25) * snrSquared))); |
| 488 | |
| 489 | if (value < static_cast<RealType>(0.001) || value > static_cast<RealType>(10.0)) |
| 490 | { |
| 491 | value = itk::NumericTraits<RealType>::OneValue(); |
| 492 | } |
| 493 | return value; |
| 494 | } |
| 495 | |
| 496 | template <typename TInputImage, typename TOutputImage, typename TMaskImage> |
| 497 | void |
| 498 | AdaptiveNonLocalMeansDenoisingImageFilter<TInputImage, TOutputImage, TMaskImage>::PrintSelf(std::ostream & os, |
| 499 | Indent indent) const |
| 500 | { |
| 501 | Superclass::PrintSelf(os, indent); |
| 502 | |
| 503 | if (this->m_UseRicianNoiseModel) |
| 504 | { |
| 505 | os << indent << "Using Rician noise model." << std::endl; |
| 506 | } |
| 507 | else |
| 508 | { |
| 509 | os << indent << "Using Gaussian noise model." << std::endl; |
| 510 | } |
| 511 | |
| 512 | os << indent << "Epsilon = " << this->m_Epsilon << std::endl; |
| 513 | os << indent << "Mean threshold = " << this->m_MeanThreshold << std::endl; |
| 514 | os << indent << "Variance threshold = " << this->m_VarianceThreshold << std::endl; |
| 515 | os << indent << "Smoothing variance = " << this->m_SmoothingVariance << std::endl; |
| 516 | |
| 517 | os << indent |
| 518 | << "Neighborhood radius for local mean and variance = " << this->m_NeighborhoodRadiusForLocalMeanAndVariance |
| 519 | << std::endl; |
| 520 | } |
| 521 | |
| 522 | } // end namespace itk |
| 523 | |
| 524 | #endif |