/*M/////////////////////////////////////////////////////////////////////////////////////// // // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. // // By downloading, copying, installing or using the software you agree to this license. // If you do not agree to this license, do not download, install, // copy or use the software. // // // Intel License Agreement // For Open Source Computer Vision Library // // Copyright (C) 2000, Intel Corporation, all rights reserved. // Third party copyrights are property of their respective icvers. // // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // // * Redistribution's of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // // * Redistribution's in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // * The name of Intel Corporation may not be used to endorse or promote products // derived from this software without specific prior written permission. // // This software is provided by the copyright holders and contributors "as is" and // any express or implied warranties, including, but not limited to, the implied // warranties of merchantability and fitness for a particular purpose are disclaimed. // In no event shall the Intel Corporation or contributors be liable for any direct, // indirect, incidental, special, exemplary, or consequential damages // (including, but not limited to, procurement of substitute goods or services; // loss of use, data, or profits; or business interruption) however caused // and on any theory of liability, whether in contract, strict liability, // or tort (including negligence or otherwise) arising in any way out of // the use of this software, even if advised of the possibility of such damage. // //M*/ #ifndef __OPENCV_FAST_NLMEANS_DENOISING_INVOKER_COMMONS_HPP__ #define __OPENCV_FAST_NLMEANS_DENOISING_INVOKER_COMMONS_HPP__ using namespace cv; template struct pixelInfo_ { static const int channels = 1; typedef T sampleType; }; template struct pixelInfo_ > { static const int channels = n; typedef ET sampleType; }; template struct pixelInfo: public pixelInfo_ { using typename pixelInfo_::sampleType; static inline sampleType sampleMax() { return std::numeric_limits::max(); } static inline sampleType sampleMin() { return std::numeric_limits::min(); } static inline size_t sampleBytes() { return sizeof(sampleType); } static inline size_t sampleBits() { return 8*sampleBytes(); } }; class DistAbs { template struct calcDist_ { static inline IT f(const T a, const T b) { return std::abs((IT)(a-b)); } }; template struct calcDist_, IT> { static inline IT f(const Vec a, const Vec b) { return std::abs((IT)(a[0]-b[0])) + std::abs((IT)(a[1]-b[1])); } }; template struct calcDist_, IT> { static inline IT f(const Vec a, const Vec b) { return std::abs((IT)(a[0]-b[0])) + std::abs((IT)(a[1]-b[1])) + std::abs((IT)(a[2]-b[2])); } }; public: template static inline IT calcDist(const T a, const T b) { return calcDist_::f(a, b); } template static inline IT calcDist(const Mat& m, int i1, int j1, int i2, int j2) { const T a = m.at(i1, j1); const T b = m.at(i2, j2); return calcDist(a,b); } template static inline IT calcUpDownDist(T a_up, T a_down, T b_up, T b_down) { return calcDist(a_down, b_down) - calcDist(a_up, b_up); }; template static double calcWeight(double dist, double h) { return std::exp(-dist*dist / (h * h * pixelInfo::channels)); } template static double maxDist() { return (IT)pixelInfo::sampleMax() * (IT)pixelInfo::channels; } }; class DistSquared { template struct calcDist_ { static inline IT f(const T a, const T b) { return (IT)(a-b) * (IT)(a-b); } }; template struct calcDist_, IT> { static inline IT f(const Vec a, const Vec b) { return (IT)(a[0]-b[0])*(IT)(a[0]-b[0]) + (IT)(a[1]-b[1])*(IT)(a[1]-b[1]); } }; template struct calcDist_, IT> { static inline IT f(const Vec a, const Vec b) { return (IT)(a[0]-b[0])*(IT)(a[0]-b[0]) + (IT)(a[1]-b[1])*(IT)(a[1]-b[1]) + (IT)(a[2]-b[2])*(IT)(a[2]-b[2]); } }; template struct calcUpDownDist_ { static inline IT f(T a_up, T a_down, T b_up, T b_down) { IT A = a_down - b_down; IT B = a_up - b_up; return (A-B)*(A+B); } }; template struct calcUpDownDist_, IT> { private: typedef Vec T; public: static inline IT f(T a_up, T a_down, T b_up, T b_down) { return calcDist(a_down, b_down) - calcDist(a_up, b_up); } }; public: template static inline IT calcDist(const T a, const T b) { return calcDist_::f(a, b); } template static inline IT calcDist(const Mat& m, int i1, int j1, int i2, int j2) { const T a = m.at(i1, j1); const T b = m.at(i2, j2); return calcDist(a,b); } template static inline IT calcUpDownDist(T a_up, T a_down, T b_up, T b_down) { return calcUpDownDist_::f(a_up, a_down, b_up, b_down); }; template static double calcWeight(double dist, double h) { return std::exp(-dist / (h * h * pixelInfo::channels)); } template static double maxDist() { return (IT)pixelInfo::sampleMax() * (IT)pixelInfo::sampleMax() * (IT)pixelInfo::channels; } }; template struct incWithWeight_ { static inline void f(IT* estimation, IT weight, T p) { estimation[0] += weight * p; } }; template struct incWithWeight_, IT> { static inline void f(IT* estimation, IT weight, Vec p) { estimation[0] += weight * p[0]; estimation[1] += weight * p[1]; } }; template struct incWithWeight_, IT> { static inline void f(IT* estimation, IT weight, Vec p) { estimation[0] += weight * p[0]; estimation[1] += weight * p[1]; estimation[2] += weight * p[2]; } }; template static inline void incWithWeight(IT* estimation, IT weight, T p) { return incWithWeight_::f(estimation, weight, p); } template struct saturateCastFromArray_ { static inline T f(IT* estimation) { return saturate_cast(estimation[0]); } }; template struct saturateCastFromArray_, IT> { static inline Vec f(IT* estimation) { Vec res; res[0] = saturate_cast(estimation[0]); res[1] = saturate_cast(estimation[1]); return res; } }; template struct saturateCastFromArray_, IT> { static inline Vec f(IT* estimation) { Vec res; res[0] = saturate_cast(estimation[0]); res[1] = saturate_cast(estimation[1]); res[2] = saturate_cast(estimation[2]); return res; } }; template static inline T saturateCastFromArray(IT* estimation) { return saturateCastFromArray_::f(estimation); } #endif