diff --git a/modules/photo/doc/denoising.rst b/modules/photo/doc/denoising.rst new file mode 100644 index 0000000000..957ec88006 --- /dev/null +++ b/modules/photo/doc/denoising.rst @@ -0,0 +1,91 @@ +Denoising +========== + +.. highlight:: cpp + +fastNlMeansDenoising +----------- +Perform image denoising using Non-local Means Denoising algorithm http://www.ipol.im/pub/algo/bcm_non_local_means_denoising/ +with several computational optimizations. Noise expected to be a gaussian white noise + +.. ocv:function:: void fastNlMeansDenoising( Mat& src, Mat& dst, int templateWindowSize, int searchWindowSize, int h ) + + :param src: Input 8-bit 1-channel, 2-channel or 3-channel image. + + :param dst: Output image with the same size and type as ``src`` . + + :param templateWindowSize: Size in pixels of the template patch that is used to compute weights. Should be odd. Recommended value 7 pixels + + :param searchWindowSize: Size in pixels of the window that is used to compute weighted average for given pixel. Should be odd. Affect performance linearly: greater searchWindowsSize - greater denoising time. Recommended value 21 pixels + + :param h: Parameter regulating filter strength. Big h value perfectly removes noise but also removes image details, smaller h value preserves details but also preserves some noise + +This function expected to be applied to grayscale images. For colored images look at ``fastNlMeansDenoisingColored``. +Advanced usage of this functions can be manual denoising of colored image in different colorspaces. +Such approach is used in ``fastNlMeansDenoisingColored`` by converting image to CIELAB colorspace and then separately denoise L and AB components with different h parameter. + +fastNlMeansDenoisingColored +----------- +Modification of ``fastNlMeansDenoising`` function for colored images + +.. ocv:function:: void fastNlMeansDenoisingColored( Mat& src, Mat& dst, int templateWindowSize, int searchWindowSize, int h, int hForColorComponents ) + + :param src: Input 8-bit 3-channel image. + + :param dst: Output image with the same size and type as ``src`` . + + :param templateWindowSize: Size in pixels of the template patch that is used to compute weights. Should be odd. Recommended value 7 pixels + + :param searchWindowSize: Size in pixels of the window that is used to compute weighted average for given pixel. Should be odd. Affect performance linearly: greater searchWindowsSize - greater denoising time. Recommended value 21 pixels + + :param h: Parameter regulating filter strength for luminance component. Bigger h value perfectly removes noise but also removes image details, smaller h value preserves details but also preserves some noise + + :param hForColorComponents: The same as h but for color components. For most images value equals 10 will be enought to remove colored noise and do not distort colors + +The function converts image to CIELAB colorspace and then separately denoise L and AB components with given h parameters using ``fastNlMeansDenoising`` function. + +fastNlMeansDenoisingMulti +----------- +Modification of ``fastNlMeansDenoising`` function for images sequence where consequtive images have been captured in small period of time. For example video. This version of the function is for grayscale images or for manual manipulation with colorspaces. +For more details see http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.131.6394 + +.. ocv:function:: void fastNlMeansDenoisingMulti( const std::vector& srcImgs, int imgToDenoiseIndex, int temporalWindowSize, Mat& dst, int templateWindowSize, int searchWindowSize, int h) + + :param srcImgs: Input 8-bit 1-channel, 2-channel or 3-channel images sequence. All images should have the same type and size. + + :param imgToDenoiseIndex: Target image to denoise index in ``srcImgs`` sequence + + :param temporalWindowSize: Number of surrounding images to use for target image denoising. Should be odd. Images from ``imgToDenoiseIndex - temporalWindowSize / 2`` to ``imgToDenoiseIndex - temporalWindowSize / 2`` from ``srcImgs`` will be used to denoise ``srcImgs[imgToDenoiseIndex]`` image. + + :param dst: Output image with the same size and type as ``srcImgs`` images. + + :param templateWindowSize: Size in pixels of the template patch that is used to compute weights. Should be odd. Recommended value 7 pixels + + :param searchWindowSize: Size in pixels of the window that is used to compute weighted average for given pixel. Should be odd. Affect performance linearly: greater searchWindowsSize - greater denoising time. Recommended value 21 pixels + + :param h: Parameter regulating filter strength for luminance component. Bigger h value perfectly removes noise but also removes image details, smaller h value preserves details but also preserves some noise + +fastNlMeansDenoisingColoredMulti +----------- +Modification of ``fastNlMeansDenoisingMulti`` function for colored images sequences + +.. ocv:function:: void fastNlMeansDenoisingColoredMulti( const std::vector& srcImgs, int imgToDenoiseIndex, int temporalWindowSize, Mat& dst, int templateWindowSize, int searchWindowSize, int h, int hForColorComponents) + + :param srcImgs: Input 8-bit 3-channel images sequence. All images should have the same type and size. + + :param imgToDenoiseIndex: Target image to denoise index in ``srcImgs`` sequence + + :param temporalWindowSize: Number of surrounding images to use for target image denoising. Should be odd. Images from ``imgToDenoiseIndex - temporalWindowSize / 2`` to ``imgToDenoiseIndex - temporalWindowSize / 2`` from ``srcImgs`` will be used to denoise ``srcImgs[imgToDenoiseIndex]`` image. + + :param dst: Output image with the same size and type as ``srcImgs`` images. + + :param templateWindowSize: Size in pixels of the template patch that is used to compute weights. Should be odd. Recommended value 7 pixels + + :param searchWindowSize: Size in pixels of the window that is used to compute weighted average for given pixel. Should be odd. Affect performance linearly: greater searchWindowsSize - greater denoising time. Recommended value 21 pixels + + :param h: Parameter regulating filter strength for luminance component. Bigger h value perfectly removes noise but also removes image details, smaller h value preserves details but also preserves some noise. + + :param hForColorComponents: The same as h but for color components. + +The function converts images to CIELAB colorspace and then separately denoise L and AB components with given h parameters using ``fastNlMeansDenoisingMulti`` function. + diff --git a/modules/photo/doc/photo.rst b/modules/photo/doc/photo.rst index 9d8636ee73..6f05239120 100644 --- a/modules/photo/doc/photo.rst +++ b/modules/photo/doc/photo.rst @@ -8,3 +8,4 @@ photo. Computational Photography :maxdepth: 2 inpainting + denoising diff --git a/modules/photo/include/opencv2/photo/denoising.hpp b/modules/photo/include/opencv2/photo/denoising.hpp new file mode 100644 index 0000000000..b322c31755 --- /dev/null +++ b/modules/photo/include/opencv2/photo/denoising.hpp @@ -0,0 +1,79 @@ +/*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. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. +// Copyright (C) 2008-2012, Willow Garage Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// 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 the copyright holders 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_DENOISING_HPP__ +#define __OPENCV_DENOISING_HPP__ + +#include "opencv2/core/core.hpp" +#include "opencv2/imgproc/imgproc.hpp" +#include + +#ifdef __cplusplus + +/*! \namespace cv + Namespace where all the C++ OpenCV functionality resides + */ +namespace cv +{ + +CV_EXPORTS void fastNlMeansDenoising( const Mat& src, Mat& dst, + int templateWindowSize, int searchWindowSize, int h); + +CV_EXPORTS void fastNlMeansDenoisingColored( const Mat& src, Mat& dst, + int templateWindowSize, int searchWindowSize, + int h, int hForColorComponents); + +CV_EXPORTS void fastNlMeansDenoisingMulti( const std::vector& srcImgs, + int imgToDenoiseIndex, int temporalWindowSize, + Mat& dst, + int templateWindowSize, int searchWindowSize, int h); + +CV_EXPORTS void fastNlMeansDenoisingColoredMulti( const std::vector& srcImgs, + int imgToDenoiseIndex, int temporalWindowSize, + Mat& dst, + int templateWindowSize, int searchWindowSize, + int h, int hForColorComponents); + +} +#endif + +#endif diff --git a/modules/photo/src/arrays.hpp b/modules/photo/src/arrays.hpp new file mode 100644 index 0000000000..c1c4e5f971 --- /dev/null +++ b/modules/photo/src/arrays.hpp @@ -0,0 +1,161 @@ +/*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_DENOISING_ARRAYS_HPP__ +#define __OPENCV_DENOISING_ARRAYS_HPP__ + +template struct Array2d { + T* a; + int n1,n2; + bool needToDeallocArray; + + Array2d(const Array2d& array2d): + a(array2d.a), n1(array2d.n1), n2(array2d.n2), needToDeallocArray(false) + { + if (array2d.needToDeallocArray) { + // copy constructor for self allocating arrays not supported + throw new exception(); + } + } + + Array2d(T* _a, int _n1, int _n2): + a(_a), n1(_n1), n2(_n2), needToDeallocArray(false) {} + + Array2d(int _n1, int _n2): + n1(_n1), n2(_n2), needToDeallocArray(true) + { + a = new T[n1*n2]; + } + + ~Array2d() { + if (needToDeallocArray) { + delete a; + } + } + + T* operator [] (int i) { + return a + i*n2; + } + + inline T* row_ptr(int i) { + return (*this)[i]; + } +}; + +template struct Array3d { + T* a; + int n1,n2,n3; + bool needToDeallocArray; + + Array3d(T* _a, int _n1, int _n2, int _n3): + a(_a), n1(_n1), n2(_n2), n3(_n3), needToDeallocArray(false) {} + + Array3d(int _n1, int _n2, int _n3): + n1(_n1), n2(_n2), n3(_n3), needToDeallocArray(true) + { + a = new T[n1*n2*n3]; + } + + ~Array3d() { + if (needToDeallocArray) { + delete a; + } + } + + Array2d operator [] (int i) { + Array2d array2d(a + i*n2*n3, n2, n3); + return array2d; + } + + inline T* row_ptr(int i1, int i2) { + return a + i1*n2*n3 + i2*n3; + } +}; + +template struct Array4d { + T* a; + int n1,n2,n3,n4; + bool needToDeallocArray; + int steps[4]; + + void init_steps() { + steps[0] = n2*n3*n4; + steps[1] = n3*n4; + steps[2] = n4; + steps[3] = 1; + } + + Array4d(T* _a, int _n1, int _n2, int _n3, int _n4): + a(_a), n1(_n1), n2(_n2), n3(_n3), n4(_n4), needToDeallocArray(false) + { + init_steps(); + } + + Array4d(int _n1, int _n2, int _n3, int _n4): + n1(_n1), n2(_n2), n3(_n3), n4(_n4), needToDeallocArray(true) + { + a = new T[n1*n2*n3*n4]; + init_steps(); + } + + ~Array4d() { + if (needToDeallocArray) { + delete a; + } + } + + Array3d operator [] (int i) { + Array3d array3d(a + i*n2*n3*n4, n2, n3, n4); + return array3d; + } + + inline T* row_ptr(int i1, int i2, int i3) { + return a + i1*n2*n3*n4 + i2*n3*n4 + i3*n4; + } + + inline int step_size(int dimension) { + return steps[dimension]; + } +}; + +#endif + + diff --git a/modules/photo/src/denoising.cpp b/modules/photo/src/denoising.cpp new file mode 100644 index 0000000000..39643e33dc --- /dev/null +++ b/modules/photo/src/denoising.cpp @@ -0,0 +1,220 @@ +/*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*/ + +#include "precomp.hpp" +#include "opencv2/photo/denoising.hpp" +#include "opencv2/imgproc/imgproc.hpp" +#include "fast_nlmeans_denoising_invoker.hpp" +#include "fast_nlmeans_multi_denoising_invoker.hpp" + +void cv::fastNlMeansDenoising( const cv::Mat& src, cv::Mat& dst, + int templateWindowSize, int searchWindowSize, int h) +{ + switch (src.type()) { + case CV_8U: + parallel_for(cv::BlockedRange(0, src.rows), + FastNlMeansDenoisingInvoker( + src, dst, templateWindowSize, searchWindowSize, h)); + break; + case CV_8UC2: + parallel_for(cv::BlockedRange(0, src.rows), + FastNlMeansDenoisingInvoker( + src, dst, templateWindowSize, searchWindowSize, h)); + break; + case CV_8UC3: + parallel_for(cv::BlockedRange(0, src.rows), + FastNlMeansDenoisingInvoker( + src, dst, templateWindowSize, searchWindowSize, h)); + break; + default: + CV_Error(CV_StsBadArg, + "Unsupported matrix format! Only uchar, Vec2b, Vec3b are supported"); + } +} + +void cv::fastNlMeansDenoisingColored( const cv::Mat& src, cv::Mat& dst, + int templateWindowSize, int searchWindowSize, + int h, int hForColorComponents) +{ + if (src.type() != CV_8UC3) { + CV_Error(CV_StsBadArg, "Type of input image should be CV_8UC3!"); + return; + } + + Mat src_lab; + cvtColor(src, src_lab, CV_LBGR2Lab); + + Mat l(src.size(), CV_8U); + Mat ab(src.size(), CV_8UC2); + Mat l_ab[] = { l, ab }; + int from_to[] = { 0,0, 1,1, 2,2 }; + mixChannels(&src_lab, 1, l_ab, 2, from_to, 3); + + fastNlMeansDenoising(l, l, templateWindowSize, searchWindowSize, h); + fastNlMeansDenoising(ab, ab, templateWindowSize, searchWindowSize, hForColorComponents); + + Mat l_ab_denoised[] = { l, ab }; + Mat dst_lab(src.size(), src.type()); + mixChannels(l_ab_denoised, 2, &dst_lab, 1, from_to, 3); + + cvtColor(dst_lab, dst, CV_Lab2LBGR); +} + +static void fastNlMeansDenoisingMultiCheckPreconditions( + const std::vector& srcImgs, + int imgToDenoiseIndex, int temporalWindowSize, + int templateWindowSize, int searchWindowSize) +{ + int src_imgs_size = srcImgs.size(); + if (src_imgs_size == 0) { + CV_Error(CV_StsBadArg, "Input images vector should not be empty!"); + } + + if (temporalWindowSize % 2 == 0 || + searchWindowSize % 2 == 0 || + templateWindowSize % 2 == 0) { + CV_Error(CV_StsBadArg, "All windows sizes should be odd!"); + } + + int temporalWindowHalfSize = temporalWindowSize / 2; + if (imgToDenoiseIndex - temporalWindowHalfSize < 0 || + imgToDenoiseIndex + temporalWindowHalfSize >= src_imgs_size) + { + CV_Error(CV_StsBadArg, + "imgToDenoiseIndex and temporalWindowSize " + "should be choosen corresponding srcImgs size!"); + } + + for (int i = 1; i < src_imgs_size; i++) { + if (srcImgs[0].size() != srcImgs[i].size() || srcImgs[0].type() != srcImgs[i].type()) { + CV_Error(CV_StsBadArg, "Input images should have the same size and type!"); + } + } +} + +void cv::fastNlMeansDenoisingMulti( const std::vector& srcImgs, + int imgToDenoiseIndex, int temporalWindowSize, + cv::Mat& dst, + int templateWindowSize, int searchWindowSize, int h) +{ + fastNlMeansDenoisingMultiCheckPreconditions( + srcImgs, imgToDenoiseIndex, + temporalWindowSize, templateWindowSize, searchWindowSize + ); + + switch (srcImgs[0].type()) { + case CV_8U: + parallel_for(cv::BlockedRange(0, srcImgs[0].rows), + FastNlMeansMultiDenoisingInvoker( + srcImgs, imgToDenoiseIndex, temporalWindowSize, + dst, templateWindowSize, searchWindowSize, h)); + break; + case CV_8UC2: + parallel_for(cv::BlockedRange(0, srcImgs[0].rows), + FastNlMeansMultiDenoisingInvoker( + srcImgs, imgToDenoiseIndex, temporalWindowSize, + dst, templateWindowSize, searchWindowSize, h)); + break; + case CV_8UC3: + parallel_for(cv::BlockedRange(0, srcImgs[0].rows), + FastNlMeansMultiDenoisingInvoker( + srcImgs, imgToDenoiseIndex, temporalWindowSize, + dst, templateWindowSize, searchWindowSize, h)); + break; + default: + CV_Error(CV_StsBadArg, + "Unsupported matrix format! Only uchar, Vec2b, Vec3b are supported"); + } +} + +void cv::fastNlMeansDenoisingColoredMulti( const std::vector& srcImgs, + int imgToDenoiseIndex, int temporalWindowSize, + cv::Mat& dst, + int templateWindowSize, int searchWindowSize, + int h, int hForColorComponents) +{ + fastNlMeansDenoisingMultiCheckPreconditions( + srcImgs, imgToDenoiseIndex, + temporalWindowSize, templateWindowSize, searchWindowSize + ); + + int src_imgs_size = srcImgs.size(); + + if (srcImgs[0].type() != CV_8UC3) { + CV_Error(CV_StsBadArg, "Type of input images should be CV_8UC3!"); + return; + } + + int from_to[] = { 0,0, 1,1, 2,2 }; + + // TODO convert only required images + vector src_lab(src_imgs_size); + vector l(src_imgs_size); + vector ab(src_imgs_size); + for (int i = 0; i < src_imgs_size; i++) { + src_lab[i] = Mat::zeros(srcImgs[0].size(), CV_8UC3); + l[i] = Mat::zeros(srcImgs[0].size(), CV_8UC1); + ab[i] = Mat::zeros(srcImgs[0].size(), CV_8UC2); + cvtColor(srcImgs[i], src_lab[i], CV_LBGR2Lab); + + Mat l_ab[] = { l[i], ab[i] }; + mixChannels(&src_lab[i], 1, l_ab, 2, from_to, 3); + } + + Mat dst_l; + Mat dst_ab; + + fastNlMeansDenoisingMulti( + l, imgToDenoiseIndex, temporalWindowSize, + dst_l, templateWindowSize, searchWindowSize, h); + + fastNlMeansDenoisingMulti( + ab, imgToDenoiseIndex, temporalWindowSize, + dst_ab, templateWindowSize, searchWindowSize, hForColorComponents); + + Mat l_ab_denoised[] = { dst_l, dst_ab }; + Mat dst_lab(srcImgs[0].size(), srcImgs[0].type()); + mixChannels(l_ab_denoised, 2, &dst_lab, 1, from_to, 3); + + cvtColor(dst_lab, dst, CV_Lab2LBGR); +} + + diff --git a/modules/photo/src/fast_nlmeans_denoising_invoker.hpp b/modules/photo/src/fast_nlmeans_denoising_invoker.hpp new file mode 100644 index 0000000000..58e4a45e17 --- /dev/null +++ b/modules/photo/src/fast_nlmeans_denoising_invoker.hpp @@ -0,0 +1,342 @@ +/*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_HPP__ +#define __OPENCV_FAST_NLMEANS_DENOISING_INVOKER_HPP__ + +#include "precomp.hpp" +#include +#include +#include +#include + +#include "fast_nlmeans_denoising_invoker_commons.hpp" +#include "arrays.hpp" + +using namespace std; +using namespace cv; + +template +struct FastNlMeansDenoisingInvoker { + public: + FastNlMeansDenoisingInvoker(const Mat& src, Mat& dst, + int template_window_size, int search_window_size, const double h); + + void operator() (const BlockedRange& range) const; + + private: + const Mat& src_; + Mat& dst_; + + Mat extended_src_; + int border_size_; + + int template_window_size_; + int search_window_size_; + + int template_window_half_size_; + int search_window_half_size_; + + int fixed_point_mult_; + int almost_template_window_size_sq_bin_shift; + vector almost_dist2weight; + + void calcDistSumsForFirstElementInRow( + int i, + Array2d& dist_sums, + Array3d& col_dist_sums, + Array3d& up_col_dist_sums) const; + + void calcDistSumsForElementInFirstRow( + int i, + int j, + int first_col_num, + Array2d& dist_sums, + Array3d& col_dist_sums, + Array3d& up_col_dist_sums) const; +}; + +template +FastNlMeansDenoisingInvoker::FastNlMeansDenoisingInvoker( + const cv::Mat& src, + cv::Mat& dst, + int template_window_size, + int search_window_size, + const double h) : src_(src), dst_(dst) +{ + template_window_half_size_ = template_window_size / 2; + search_window_half_size_ = search_window_size / 2; + template_window_size_ = template_window_half_size_ * 2 + 1; + search_window_size_ = search_window_half_size_ * 2 + 1; + + border_size_ = search_window_half_size_ + template_window_half_size_; + copyMakeBorder(src_, extended_src_, + border_size_, border_size_, border_size_, border_size_, cv::BORDER_DEFAULT); + + const int max_estimate_sum_value = search_window_size_ * search_window_size_ * 255; + fixed_point_mult_ = numeric_limits::max() / max_estimate_sum_value; + + // precalc weight for every possible l2 dist between blocks + // additional optimization of precalced weights to replace division(averaging) by binary shift + int template_window_size_sq = template_window_size_ * template_window_size_; + almost_template_window_size_sq_bin_shift = 0; + while (1 << almost_template_window_size_sq_bin_shift < template_window_size_sq) { + almost_template_window_size_sq_bin_shift++; + } + + int almost_template_window_size_sq = 1 << almost_template_window_size_sq_bin_shift; + double almost_dist2actual_dist_multiplier = + ((double) almost_template_window_size_sq) / template_window_size_sq; + + int max_dist = 256 * 256 * src_.channels(); + int almost_max_dist = (int) (max_dist / almost_dist2actual_dist_multiplier + 1); + almost_dist2weight.resize(almost_max_dist); + + const double WEIGHT_THRESHOLD = 0.001; + for (int almost_dist = 0; almost_dist < almost_max_dist; almost_dist++) { + double dist = almost_dist * almost_dist2actual_dist_multiplier; + int weight = cvRound(fixed_point_mult_ * std::exp(- dist / (h * h * src_.channels()))); + + if (weight < WEIGHT_THRESHOLD * fixed_point_mult_) { + weight = 0; + } + + almost_dist2weight[almost_dist] = weight; + } + // additional optimization init end + + if (dst_.empty()) { + dst_ = Mat::zeros(src_.size(), src_.type()); + } +} + +template +void FastNlMeansDenoisingInvoker::operator() (const BlockedRange& range) const { + int row_from = range.begin(); + int row_to = range.end() - 1; + + int dist_sums_array[search_window_size_ * search_window_size_]; + Array2d dist_sums(dist_sums_array, search_window_size_, search_window_size_); + + // for lazy calc optimization + int col_dist_sums_array[template_window_size_ * search_window_size_ * search_window_size_]; + Array3d col_dist_sums(&col_dist_sums_array[0], + template_window_size_, search_window_size_, search_window_size_); + + int first_col_num = -1; + + Array3d up_col_dist_sums(src_.cols, search_window_size_, search_window_size_); + + for (int i = row_from; i <= row_to; i++) { + for (int j = 0; j < src_.cols; j++) { + int search_window_y = i - search_window_half_size_; + int search_window_x = j - search_window_half_size_; + + // calc dist_sums + if (j == 0) { + calcDistSumsForFirstElementInRow(i, dist_sums, col_dist_sums, up_col_dist_sums); + first_col_num = 0; + + } else { // calc cur dist_sums using previous dist_sums + if (i == row_from) { + calcDistSumsForElementInFirstRow(i, j, first_col_num, + dist_sums, col_dist_sums, up_col_dist_sums); + + } else { + int ay = border_size_ + i; + int ax = border_size_ + j + template_window_half_size_; + + int start_by = + border_size_ + i - search_window_half_size_; + + int start_bx = + border_size_ + j - search_window_half_size_ + template_window_half_size_; + + T a_up = extended_src_.at(ay - template_window_half_size_ - 1, ax); + T a_down = extended_src_.at(ay + template_window_half_size_, ax); + + // copy class member to local variable for optimization + int search_window_size = search_window_size_; + + for (int y = 0; y < search_window_size; y++) { + int* dist_sums_row = dist_sums.row_ptr(y); + + int* col_dist_sums_row = col_dist_sums.row_ptr(first_col_num,y); + + int* up_col_dist_sums_row = up_col_dist_sums.row_ptr(j, y); + + const T* b_up_ptr = + extended_src_.ptr(start_by - template_window_half_size_ - 1 + y); + + const T* b_down_ptr = + extended_src_.ptr(start_by + template_window_half_size_ + y); + + for (int x = 0; x < search_window_size; x++) { + dist_sums_row[x] -= col_dist_sums_row[x]; + + col_dist_sums_row[x] = + up_col_dist_sums_row[x] + + calcUpDownDist( + a_up, a_down, + b_up_ptr[start_bx + x], b_down_ptr[start_bx + x] + ); + + dist_sums_row[x] += col_dist_sums_row[x]; + + up_col_dist_sums_row[x] = col_dist_sums_row[x]; + + } + } + } + + first_col_num = (first_col_num + 1) % template_window_size_; + } + + // calc weights + int weights_sum = 0; + + int estimation[src_.channels()]; + for (int channel_num = 0; channel_num < src_.channels(); channel_num++) { + estimation[channel_num] = 0; + } + + for (int y = 0; y < search_window_size_; y++) { + const T* cur_row_ptr = extended_src_.ptr(border_size_ + search_window_y + y); + int* dist_sums_row = dist_sums.row_ptr(y); + for (int x = 0; x < search_window_size_; x++) { + int almostAvgDist = + dist_sums_row[x] >> almost_template_window_size_sq_bin_shift; + + int weight = almost_dist2weight[almostAvgDist]; + weights_sum += weight; + + T p = cur_row_ptr[border_size_ + search_window_x + x]; + incWithWeight(estimation, weight, p); + } + } + + if (weights_sum > 0) { + for (int channel_num = 0; channel_num < src_.channels(); channel_num++) { + estimation[channel_num] = + cvRound(((double)estimation[channel_num]) / weights_sum); + } + + dst_.at(i,j) = saturateCastFromArray(estimation); + + } else { // weights_sum == 0 + dst_.at(i,j) = src_.at(i,j); + } + } + } +} + +template +inline void FastNlMeansDenoisingInvoker::calcDistSumsForFirstElementInRow( + int i, + Array2d& dist_sums, + Array3d& col_dist_sums, + Array3d& up_col_dist_sums) const +{ + int j = 0; + + for (int y = 0; y < search_window_size_; y++) { + for (int x = 0; x < search_window_size_; x++) { + dist_sums[y][x] = 0; + for (int tx = 0; tx < template_window_size_; tx++) { + col_dist_sums[tx][y][x] = 0; + } + + int start_y = i + y - search_window_half_size_; + int start_x = j + x - search_window_half_size_; + + for (int ty = -template_window_half_size_; ty <= template_window_half_size_; ty++) { + for (int tx = -template_window_half_size_; tx <= template_window_half_size_; tx++) { + int dist = calcDist(extended_src_, + border_size_ + i + ty, border_size_ + j + tx, + border_size_ + start_y + ty, border_size_ + start_x + tx); + + dist_sums[y][x] += dist; + col_dist_sums[tx + template_window_half_size_][y][x] += dist; + } + } + + up_col_dist_sums[j][y][x] = col_dist_sums[template_window_size_ - 1][y][x]; + } + } +} + +template +inline void FastNlMeansDenoisingInvoker::calcDistSumsForElementInFirstRow( + int i, + int j, + int first_col_num, + Array2d& dist_sums, + Array3d& col_dist_sums, + Array3d& up_col_dist_sums) const +{ + int ay = border_size_ + i; + int ax = border_size_ + j + template_window_half_size_; + + int start_by = border_size_ + i - search_window_half_size_; + int start_bx = border_size_ + j - search_window_half_size_ + template_window_half_size_; + + int new_last_col_num = first_col_num; + + for (int y = 0; y < search_window_size_; y++) { + for (int x = 0; x < search_window_size_; x++) { + dist_sums[y][x] -= col_dist_sums[first_col_num][y][x]; + + col_dist_sums[new_last_col_num][y][x] = 0; + int by = start_by + y; + int bx = start_bx + x; + for (int ty = -template_window_half_size_; ty <= template_window_half_size_; ty++) { + col_dist_sums[new_last_col_num][y][x] += + calcDist(extended_src_, ay + ty, ax, by + ty, bx); + } + + dist_sums[y][x] += col_dist_sums[new_last_col_num][y][x]; + + up_col_dist_sums[j][y][x] = col_dist_sums[new_last_col_num][y][x]; + } + } +} + +#endif diff --git a/modules/photo/src/fast_nlmeans_denoising_invoker_commons.hpp b/modules/photo/src/fast_nlmeans_denoising_invoker_commons.hpp new file mode 100644 index 0000000000..c1084f15e9 --- /dev/null +++ b/modules/photo/src/fast_nlmeans_denoising_invoker_commons.hpp @@ -0,0 +1,120 @@ +/*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__ + +#include +#include +#include + +using namespace std; +using namespace cv; + +template static inline int calcDist(const T a, const T b); + +template <> inline int calcDist(const uchar a, const uchar b) { + return (a-b) * (a-b); +} + +template <> inline int calcDist(const Vec2b a, const Vec2b b) { + return (a[0]-b[0])*(a[0]-b[0]) + (a[1]-b[1])*(a[1]-b[1]); +} + +template <> inline int calcDist(const Vec3b a, const Vec3b b) { + return (a[0]-b[0])*(a[0]-b[0]) + (a[1]-b[1])*(a[1]-b[1]) + (a[2]-b[2])*(a[2]-b[2]); +} + +template static inline int 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 int 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 <> inline int calcUpDownDist(uchar a_up, uchar a_down, uchar b_up, uchar b_down) { + int A = a_down - b_down; + int B = a_up - b_up; + return (A-B)*(A+B); +} + +template static inline void incWithWeight(int* estimation, int weight, T p); + +template <> inline void incWithWeight(int* estimation, int weight, uchar p) { + estimation[0] += weight * p; +} + +template <> inline void incWithWeight(int* estimation, int weight, Vec2b p) { + estimation[0] += weight * p[0]; + estimation[1] += weight * p[1]; +} + +template <> inline void incWithWeight(int* estimation, int weight, Vec3b p) { + estimation[0] += weight * p[0]; + estimation[1] += weight * p[1]; + estimation[2] += weight * p[2]; +} + +template static inline T saturateCastFromArray(int* estimation); + +template <> inline uchar saturateCastFromArray(int* estimation) { + return saturate_cast(estimation[0]); +} + +template <> inline Vec2b saturateCastFromArray(int* estimation) { + Vec2b res; + res[0] = saturate_cast(estimation[0]); + res[1] = saturate_cast(estimation[1]); + return res; +} + +template <> inline Vec3b saturateCastFromArray(int* estimation) { + Vec3b res; + res[0] = saturate_cast(estimation[0]); + res[1] = saturate_cast(estimation[1]); + res[2] = saturate_cast(estimation[2]); + return res; +} + +#endif diff --git a/modules/photo/src/fast_nlmeans_multi_denoising_invoker.hpp b/modules/photo/src/fast_nlmeans_multi_denoising_invoker.hpp new file mode 100644 index 0000000000..cb08c7e434 --- /dev/null +++ b/modules/photo/src/fast_nlmeans_multi_denoising_invoker.hpp @@ -0,0 +1,394 @@ +/*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_MULTI_DENOISING_INVOKER_HPP__ +#define __OPENCV_FAST_NLMEANS_MULTI_DENOISING_INVOKER_HPP__ + +#include "precomp.hpp" +#include +#include +#include +#include + +#include "fast_nlmeans_denoising_invoker_commons.hpp" +#include "arrays.hpp" + +using namespace std; +using namespace cv; + +template +struct FastNlMeansMultiDenoisingInvoker { + public: + FastNlMeansMultiDenoisingInvoker( + const std::vector& srcImgs, int imgToDenoiseIndex, int temporalWindowSize, + Mat& dst, int template_window_size, int search_window_size, const double h); + + void operator() (const BlockedRange& range) const; + + private: + int rows_; + int cols_; + int channels_count_; + + Mat& dst_; + + vector extended_srcs_; + Mat main_extended_src_; + int border_size_; + + int template_window_size_; + int search_window_size_; + int temporal_window_size_; + + int template_window_half_size_; + int search_window_half_size_; + int temporal_window_half_size_; + + int fixed_point_mult_; + int almost_template_window_size_sq_bin_shift; + vector almost_dist2weight; + + void calcDistSumsForFirstElementInRow( + int i, + Array3d& dist_sums, + Array4d& col_dist_sums, + Array4d& up_col_dist_sums) const; + + void calcDistSumsForElementInFirstRow( + int i, + int j, + int first_col_num, + Array3d& dist_sums, + Array4d& col_dist_sums, + Array4d& up_col_dist_sums) const; +}; + +template +FastNlMeansMultiDenoisingInvoker::FastNlMeansMultiDenoisingInvoker( + const vector& srcImgs, + int imgToDenoiseIndex, + int temporalWindowSize, + cv::Mat& dst, + int template_window_size, + int search_window_size, + const double h) : dst_(dst), extended_srcs_(srcImgs.size()) +{ + rows_ = srcImgs[0].rows; + cols_ = srcImgs[0].cols; + channels_count_ = srcImgs[0].channels(); + + template_window_half_size_ = template_window_size / 2; + search_window_half_size_ = search_window_size / 2; + temporal_window_half_size_ = temporalWindowSize / 2; + + template_window_size_ = template_window_half_size_ * 2 + 1; + search_window_size_ = search_window_half_size_ * 2 + 1; + temporal_window_size_ = temporal_window_half_size_ * 2 + 1; + + border_size_ = search_window_half_size_ + template_window_half_size_; + for (int i = 0; i < temporal_window_size_; i++) { + copyMakeBorder( + srcImgs[imgToDenoiseIndex - temporal_window_half_size_ + i], extended_srcs_[i], + border_size_, border_size_, border_size_, border_size_, cv::BORDER_DEFAULT); + } + main_extended_src_ = extended_srcs_[temporal_window_half_size_]; + + const int max_estimate_sum_value = + temporal_window_size_ * search_window_size_ * search_window_size_ * 255; + + fixed_point_mult_ = numeric_limits::max() / max_estimate_sum_value; + + // precalc weight for every possible l2 dist between blocks + // additional optimization of precalced weights to replace division(averaging) by binary shift + int template_window_size_sq = template_window_size_ * template_window_size_; + almost_template_window_size_sq_bin_shift = 0; + while (1 << almost_template_window_size_sq_bin_shift < template_window_size_sq) { + almost_template_window_size_sq_bin_shift++; + } + + int almost_template_window_size_sq = 1 << almost_template_window_size_sq_bin_shift; + double almost_dist2actual_dist_multiplier = + ((double) almost_template_window_size_sq) / template_window_size_sq; + + int max_dist = 256 * 256 * channels_count_; + int almost_max_dist = (int) (max_dist / almost_dist2actual_dist_multiplier + 1); + almost_dist2weight.resize(almost_max_dist); + + const double WEIGHT_THRESHOLD = 0.001; + for (int almost_dist = 0; almost_dist < almost_max_dist; almost_dist++) { + double dist = almost_dist * almost_dist2actual_dist_multiplier; + int weight = cvRound(fixed_point_mult_ * std::exp(- dist / (h * h * channels_count_))); + + if (weight < WEIGHT_THRESHOLD * fixed_point_mult_) { + weight = 0; + } + + almost_dist2weight[almost_dist] = weight; + } + // additional optimization init end + + if (dst_.empty()) { + dst_ = Mat::zeros(srcImgs[0].size(), srcImgs[0].type()); + } +} + +template +void FastNlMeansMultiDenoisingInvoker::operator() (const BlockedRange& range) const { + int row_from = range.begin(); + int row_to = range.end() - 1; + + int dist_sums_array[temporal_window_size_ * search_window_size_ * search_window_size_]; + Array3d dist_sums(dist_sums_array, + temporal_window_size_, search_window_size_, search_window_size_); + + // for lazy calc optimization + int col_dist_sums_array[ + template_window_size_ * temporal_window_size_ * search_window_size_ * search_window_size_]; + + Array4d col_dist_sums(col_dist_sums_array, + template_window_size_, temporal_window_size_, search_window_size_, search_window_size_); + + int first_col_num = -1; + + Array4d up_col_dist_sums( + cols_, temporal_window_size_, search_window_size_, search_window_size_); + + for (int i = row_from; i <= row_to; i++) { + for (int j = 0; j < cols_; j++) { + int search_window_y = i - search_window_half_size_; + int search_window_x = j - search_window_half_size_; + + // calc dist_sums + if (j == 0) { + calcDistSumsForFirstElementInRow(i, dist_sums, col_dist_sums, up_col_dist_sums); + first_col_num = 0; + + } else { // calc cur dist_sums using previous dist_sums + if (i == row_from) { + calcDistSumsForElementInFirstRow(i, j, first_col_num, + dist_sums, col_dist_sums, up_col_dist_sums); + + } else { + int ay = border_size_ + i; + int ax = border_size_ + j + template_window_half_size_; + + int start_by = + border_size_ + i - search_window_half_size_; + + int start_bx = + border_size_ + j - search_window_half_size_ + template_window_half_size_; + + T a_up = main_extended_src_.at(ay - template_window_half_size_ - 1, ax); + T a_down = main_extended_src_.at(ay + template_window_half_size_, ax); + + // copy class member to local variable for optimization + int search_window_size = search_window_size_; + + for (int d = 0; d < temporal_window_size_; d++) { + Mat cur_extended_src = extended_srcs_[d]; + Array2d cur_dist_sums = dist_sums[d]; + Array2d cur_col_dist_sums = col_dist_sums[first_col_num][d]; + Array2d cur_up_col_dist_sums = up_col_dist_sums[j][d]; + for (int y = 0; y < search_window_size; y++) { + int* dist_sums_row = cur_dist_sums.row_ptr(y); + + int* col_dist_sums_row = cur_col_dist_sums.row_ptr(y); + + int* up_col_dist_sums_row = cur_up_col_dist_sums.row_ptr(y); + + const T* b_up_ptr = + cur_extended_src.ptr(start_by - template_window_half_size_ - 1 + y); + const T* b_down_ptr = + cur_extended_src.ptr(start_by + template_window_half_size_ + y); + + for (int x = 0; x < search_window_size; x++) { + dist_sums_row[x] -= col_dist_sums_row[x]; + + col_dist_sums_row[x] = up_col_dist_sums_row[x] + + calcUpDownDist( + a_up, a_down, + b_up_ptr[start_bx + x], b_down_ptr[start_bx + x] + ); + + dist_sums_row[x] += col_dist_sums_row[x]; + + up_col_dist_sums_row[x] = col_dist_sums_row[x]; + + } + } + } + } + + first_col_num = (first_col_num + 1) % template_window_size_; + } + + // calc weights + int weights_sum = 0; + + int estimation[channels_count_]; + for (int channel_num = 0; channel_num < channels_count_; channel_num++) { + estimation[channel_num] = 0; + } + for (int d = 0; d < temporal_window_size_; d++) { + for (int y = 0; y < search_window_size_; y++) { + const T* cur_row_ptr = + extended_srcs_[d].ptr(border_size_ + search_window_y + y); + + int* dist_sums_row = dist_sums.row_ptr(d, y); + + for (int x = 0; x < search_window_size_; x++) { + int almostAvgDist = + dist_sums_row[x] >> almost_template_window_size_sq_bin_shift; + + int weight = almost_dist2weight[almostAvgDist]; + weights_sum += weight; + + T p = cur_row_ptr[border_size_ + search_window_x + x]; + incWithWeight(estimation, weight, p); + } + } + } + + if (weights_sum > 0) { + for (int channel_num = 0; channel_num < channels_count_; channel_num++) { + estimation[channel_num] = + cvRound(((double)estimation[channel_num]) / weights_sum); + } + + dst_.at(i,j) = saturateCastFromArray(estimation); + + } else { // weights_sum == 0 + dst_.at(i,j) = extended_srcs_[temporal_window_half_size_].at(i,j); + } + } + } +} + +template +inline void FastNlMeansMultiDenoisingInvoker::calcDistSumsForFirstElementInRow( + int i, + Array3d& dist_sums, + Array4d& col_dist_sums, + Array4d& up_col_dist_sums) const +{ + int j = 0; + + for (int d = 0; d < temporal_window_size_; d++) { + Mat cur_extended_src = extended_srcs_[d]; + for (int y = 0; y < search_window_size_; y++) { + for (int x = 0; x < search_window_size_; x++) { + dist_sums[d][y][x] = 0; + for (int tx = 0; tx < template_window_size_; tx++) { + col_dist_sums[tx][d][y][x] = 0; + } + + int start_y = i + y - search_window_half_size_; + int start_x = j + x - search_window_half_size_; + + int* dist_sums_ptr = &dist_sums[d][y][x]; + int* col_dist_sums_ptr = &col_dist_sums[0][d][y][x]; + int col_dist_sums_step = col_dist_sums.step_size(0); + for (int tx = -template_window_half_size_; tx <= template_window_half_size_; tx++) { + for (int ty = -template_window_half_size_; ty <= template_window_half_size_; ty++) { + int dist = calcDist( + main_extended_src_.at( + border_size_ + i + ty, border_size_ + j + tx), + cur_extended_src.at( + border_size_ + start_y + ty, border_size_ + start_x + tx) + ); + + *dist_sums_ptr += dist; + *col_dist_sums_ptr += dist; + } + col_dist_sums_ptr += col_dist_sums_step; + } + + up_col_dist_sums[j][d][y][x] = col_dist_sums[template_window_size_ - 1][d][y][x]; + } + } + } +} + +template +inline void FastNlMeansMultiDenoisingInvoker::calcDistSumsForElementInFirstRow( + int i, + int j, + int first_col_num, + Array3d& dist_sums, + Array4d& col_dist_sums, + Array4d& up_col_dist_sums) const +{ + int ay = border_size_ + i; + int ax = border_size_ + j + template_window_half_size_; + + int start_by = border_size_ + i - search_window_half_size_; + int start_bx = border_size_ + j - search_window_half_size_ + template_window_half_size_; + + int new_last_col_num = first_col_num; + + for (int d = 0; d < temporal_window_size_; d++) { + Mat cur_extended_src = extended_srcs_[d]; + for (int y = 0; y < search_window_size_; y++) { + for (int x = 0; x < search_window_size_; x++) { + dist_sums[d][y][x] -= col_dist_sums[first_col_num][d][y][x]; + + col_dist_sums[new_last_col_num][d][y][x] = 0; + int by = start_by + y; + int bx = start_bx + x; + + int* col_dist_sums_ptr = &col_dist_sums[new_last_col_num][d][y][x]; + for (int ty = -template_window_half_size_; ty <= template_window_half_size_; ty++) { + *col_dist_sums_ptr += + calcDist( + main_extended_src_.at(ay + ty, ax), + cur_extended_src.at(by + ty, bx) + ); + } + + dist_sums[d][y][x] += col_dist_sums[new_last_col_num][d][y][x]; + + up_col_dist_sums[j][d][y][x] = col_dist_sums[new_last_col_num][d][y][x]; + } + } + } +} + +#endif diff --git a/modules/photo/test/test_denoising.cpp b/modules/photo/test/test_denoising.cpp new file mode 100644 index 0000000000..39aa699811 --- /dev/null +++ b/modules/photo/test/test_denoising.cpp @@ -0,0 +1,213 @@ +/*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. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. +// Copyright (C) 2009, Willow Garage Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// 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 the copyright holders 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*/ + +#include "test_precomp.hpp" +#include "opencv2/photo/denoising.hpp" +#include + +using namespace cv; +using namespace std; + +class CV_DenoisingGrayscaleTest : public cvtest::BaseTest +{ +public: + CV_DenoisingGrayscaleTest(); + ~CV_DenoisingGrayscaleTest(); +protected: + void run(int); +}; + +CV_DenoisingGrayscaleTest::CV_DenoisingGrayscaleTest() {} +CV_DenoisingGrayscaleTest::~CV_DenoisingGrayscaleTest() {} + +void CV_DenoisingGrayscaleTest::run( int ) +{ + string folder = string(ts->get_data_path()) + "denoising/"; + Mat orig = imread(folder + "lena_noised_gaussian_sigma=10.png", 0); + Mat exp = imread(folder + "lena_noised_denoised_grayscale_tw=7_sw=21_h=10.png", 0); + + if (orig.empty() || exp.empty()) + { + ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA ); + return; + } + + Mat res; + fastNlMeansDenoising(orig, res, 7, 21, 10); + + if (norm(res - exp) > 0) { + ts->set_failed_test_info( cvtest::TS::FAIL_MISMATCH ); + } else { + ts->set_failed_test_info(cvtest::TS::OK); + } +} + +class CV_DenoisingColoredTest : public cvtest::BaseTest +{ +public: + CV_DenoisingColoredTest(); + ~CV_DenoisingColoredTest(); +protected: + void run(int); +}; + +CV_DenoisingColoredTest::CV_DenoisingColoredTest() {} +CV_DenoisingColoredTest::~CV_DenoisingColoredTest() {} + +void CV_DenoisingColoredTest::run( int ) +{ + string folder = string(ts->get_data_path()) + "denoising/"; + Mat orig = imread(folder + "lena_noised_gaussian_sigma=10.png", 1); + Mat exp = imread(folder + "lena_noised_denoised_lab12_tw=7_sw=21_h=10_h2=10.png", 1); + + if (orig.empty() || exp.empty()) + { + ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA ); + return; + } + + Mat res; + fastNlMeansDenoisingColored(orig, res, 7, 21, 10, 10); + + if (norm(res - exp) > 0) { + ts->set_failed_test_info( cvtest::TS::FAIL_MISMATCH ); + } else { + ts->set_failed_test_info(cvtest::TS::OK); + } +} + +class CV_DenoisingGrayscaleMultiTest : public cvtest::BaseTest +{ +public: + CV_DenoisingGrayscaleMultiTest(); + ~CV_DenoisingGrayscaleMultiTest(); +protected: + void run(int); +}; + +CV_DenoisingGrayscaleMultiTest::CV_DenoisingGrayscaleMultiTest() {} +CV_DenoisingGrayscaleMultiTest::~CV_DenoisingGrayscaleMultiTest() {} + +void CV_DenoisingGrayscaleMultiTest::run( int ) +{ + string folder = string(ts->get_data_path()) + "denoising/"; + + const int imgs_count = 3; + vector src_imgs(imgs_count); + src_imgs[0] = imread(folder + "lena_noised_gaussian_sigma=20_multi_0.png", 0); + src_imgs[1] = imread(folder + "lena_noised_gaussian_sigma=20_multi_1.png", 0); + src_imgs[2] = imread(folder + "lena_noised_gaussian_sigma=20_multi_2.png", 0); + + Mat exp = imread(folder + "lena_noised_denoised_multi_tw=7_sw=21_h=15.png", 0); + + bool have_empty_src = false; + for (int i = 0; i < imgs_count; i++) { + have_empty_src |= src_imgs[i].empty(); + } + + if (have_empty_src || exp.empty()) + { + ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA ); + return; + } + + Mat res; + fastNlMeansDenoisingMulti(src_imgs, imgs_count / 2, imgs_count, res, 7, 21, 15); + + if (norm(res - exp) > 0) { + ts->set_failed_test_info( cvtest::TS::FAIL_MISMATCH ); + } else { + ts->set_failed_test_info(cvtest::TS::OK); + } +} + +class CV_DenoisingColoredMultiTest : public cvtest::BaseTest +{ +public: + CV_DenoisingColoredMultiTest(); + ~CV_DenoisingColoredMultiTest(); +protected: + void run(int); +}; + +CV_DenoisingColoredMultiTest::CV_DenoisingColoredMultiTest() {} +CV_DenoisingColoredMultiTest::~CV_DenoisingColoredMultiTest() {} + +void CV_DenoisingColoredMultiTest::run( int ) +{ + string folder = string(ts->get_data_path()) + "denoising/"; + + const int imgs_count = 3; + vector src_imgs(imgs_count); + src_imgs[0] = imread(folder + "lena_noised_gaussian_sigma=20_multi_0.png", 1); + src_imgs[1] = imread(folder + "lena_noised_gaussian_sigma=20_multi_1.png", 1); + src_imgs[2] = imread(folder + "lena_noised_gaussian_sigma=20_multi_2.png", 1); + + Mat exp = imread(folder + "lena_noised_denoised_multi_lab12_tw=7_sw=21_h=10_h2=15.png", 1); + + bool have_empty_src = false; + for (int i = 0; i < imgs_count; i++) { + have_empty_src |= src_imgs[i].empty(); + } + + if (have_empty_src || exp.empty()) + { + ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_TEST_DATA ); + return; + } + + Mat res; + fastNlMeansDenoisingColoredMulti(src_imgs, imgs_count / 2, imgs_count, res, 7, 21, 10, 15); + + if (norm(res - exp) > 0) { + ts->set_failed_test_info( cvtest::TS::FAIL_MISMATCH ); + } else { + ts->set_failed_test_info(cvtest::TS::OK); + } +} + + +TEST(Imgproc_DenoisingGrayscale, regression) { CV_DenoisingGrayscaleTest test; test.safe_run(); } +TEST(Imgproc_DenoisingColored, regression) { CV_DenoisingColoredTest test; test.safe_run(); } +TEST(Imgproc_DenoisingGrayscaleMulti, regression) { CV_DenoisingGrayscaleMultiTest test; test.safe_run(); } +TEST(Imgproc_DenoisingColoredMulti, regression) { CV_DenoisingColoredMultiTest test; test.safe_run(); } +