From 866d8c0cfaed5e5d191a305635b0942bbdc842cb Mon Sep 17 00:00:00 2001 From: Vadim Pisarevsky Date: Tue, 24 Apr 2012 15:16:21 +0000 Subject: [PATCH] added special function to patch NaN's in image. call this function from bilateralfilter (should fix bug #1811) --- modules/core/include/opencv2/core/core.hpp | 3 +++ modules/core/src/mathfuncs.cpp | 22 ++++++++++++++++++++++ modules/imgproc/src/smooth.cpp | 1 + 3 files changed, 26 insertions(+) diff --git a/modules/core/include/opencv2/core/core.hpp b/modules/core/include/opencv2/core/core.hpp index 12e64a3..f284ac7 100644 --- a/modules/core/include/opencv2/core/core.hpp +++ b/modules/core/include/opencv2/core/core.hpp @@ -2214,6 +2214,9 @@ CV_EXPORTS_W void magnitude(InputArray x, InputArray y, OutputArray magnitude); //! checks that each matrix element is within the specified range. CV_EXPORTS_W bool checkRange(InputArray a, bool quiet=true, CV_OUT Point* pos=0, double minVal=-DBL_MAX, double maxVal=DBL_MAX); +//! converts NaN's to the given number +CV_EXPORTS_W void patchNaNs(InputOutputArray a, double val=0); + //! implements generalized matrix product algorithm GEMM from BLAS CV_EXPORTS_W void gemm(InputArray src1, InputArray src2, double alpha, InputArray src3, double gamma, OutputArray dst, int flags=0); diff --git a/modules/core/src/mathfuncs.cpp b/modules/core/src/mathfuncs.cpp index 0ac641a..9a95019 100644 --- a/modules/core/src/mathfuncs.cpp +++ b/modules/core/src/mathfuncs.cpp @@ -2186,6 +2186,28 @@ bool checkRange(InputArray _src, bool quiet, Point* pt, double minVal, double ma } +void patchNaNs( InputOutputArray _a, double _val ) +{ + Mat a = _a.getMat(); + CV_Assert( a.depth() == CV_32F ); + + const Mat* arrays[] = {&a, 0}; + int* ptrs[1]; + NAryMatIterator it(arrays, (uchar**)ptrs); + size_t len = it.size*a.channels(); + Cv32suf val; + val.f = (float)_val; + + for( size_t i = 0; i < it.nplanes; i++, ++it ) + { + int* tptr = ptrs[0]; + for( size_t j = 0; j < len; j++ ) + if( (tptr[j] & 0x7fffffff) > 0x7f800000 ) + tptr[j] = val.i; + } +} + + void exp(const float* src, float* dst, int n) { Exp_32f(src, dst, n); diff --git a/modules/imgproc/src/smooth.cpp b/modules/imgproc/src/smooth.cpp index 41dda25..e18f3bd 100644 --- a/modules/imgproc/src/smooth.cpp +++ b/modules/imgproc/src/smooth.cpp @@ -1439,6 +1439,7 @@ bilateralFilter_32f( const Mat& src, Mat& dst, int d, // temporary copy of the image with borders for easy processing Mat temp; copyMakeBorder( src, temp, radius, radius, radius, radius, borderType ); + patchNaNs(temp); // allocate lookup tables vector _space_weight(d*d); -- 2.7.4