diff --git a/modules/photo/src/fast_nlmeans_denoising_invoker_commons.hpp b/modules/photo/src/fast_nlmeans_denoising_invoker_commons.hpp index df8e4703e..efd482f6b 100644 --- a/modules/photo/src/fast_nlmeans_denoising_invoker_commons.hpp +++ b/modules/photo/src/fast_nlmeans_denoising_invoker_commons.hpp @@ -126,11 +126,13 @@ class DistAbs { static inline WT f(double dist, const float *h, WT fixed_point_mult) { + double w = std::exp(-dist*dist / (h[0]*h[0] * pixelInfo::channels)); + if (std::isnan(w)) w = 1.0; // Handle h = 0.0 + static const double WEIGHT_THRESHOLD = 0.001; - WT weight = (WT)round(fixed_point_mult * - std::exp(-dist*dist / (h[0]*h[0] * pixelInfo::channels))); - if (weight < WEIGHT_THRESHOLD * fixed_point_mult) - weight = 0; + WT weight = (WT)round(fixed_point_mult * w); + if (weight < WEIGHT_THRESHOLD * fixed_point_mult) weight = 0; + return weight; } }; @@ -167,7 +169,8 @@ public: }; template - static inline WT calcWeight(double dist, const float *h, int fixed_point_mult) + static inline WT calcWeight(double dist, const float *h, + typename pixelInfo::sampleType fixed_point_mult) { return calcWeight_::f(dist, h, fixed_point_mult); } @@ -243,20 +246,22 @@ class DistSquared template struct calcWeight_ { - static inline WT f(double dist, const float *h, int fixed_point_mult) + static inline WT f(double dist, const float *h, WT fixed_point_mult) { + double w = std::exp(-dist / (h[0]*h[0] * pixelInfo::channels)); + if (std::isnan(w)) w = 1.0; // Handle h = 0.0 + static const double WEIGHT_THRESHOLD = 0.001; - WT weight = (WT)round(fixed_point_mult * - std::exp(-dist / (h[0]*h[0] * pixelInfo::channels))); - if (weight < WEIGHT_THRESHOLD * fixed_point_mult) - weight = 0; + WT weight = (WT)round(fixed_point_mult * w); + if (weight < WEIGHT_THRESHOLD * fixed_point_mult) weight = 0; + return weight; } }; template struct calcWeight_ > { - static inline Vec f(double dist, const float *h, int fixed_point_mult) + static inline Vec f(double dist, const float *h, ET fixed_point_mult) { Vec res; for (int i=0; i - static inline WT calcWeight(double dist, const float *h, int fixed_point_mult) + static inline WT calcWeight(double dist, const float *h, + typename pixelInfo::sampleType fixed_point_mult) { return calcWeight_::f(dist, h, fixed_point_mult); }