Path: blob/master/modules/photo/src/denoising.cuda.cpp
16339 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2009, Willow Garage Inc., all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// warranties of merchantability and fitness for a particular purpose are disclaimed.32// In no event shall the Intel Corporation or contributors be liable for any direct,33// indirect, incidental, special, exemplary, or consequential damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142#include "precomp.hpp"4344#include "opencv2/photo/cuda.hpp"45#include "opencv2/core/private.cuda.hpp"4647#include "opencv2/opencv_modules.hpp"4849#ifdef HAVE_OPENCV_CUDAARITHM50# include "opencv2/cudaarithm.hpp"51#endif5253#ifdef HAVE_OPENCV_CUDAIMGPROC54# include "opencv2/cudaimgproc.hpp"55#endif5657using namespace cv;58using namespace cv::cuda;5960#if !defined (HAVE_CUDA) || !defined(HAVE_OPENCV_CUDAARITHM) || !defined(HAVE_OPENCV_CUDAIMGPROC)6162void cv::cuda::nonLocalMeans(InputArray, OutputArray, float, int, int, int, Stream&) { throw_no_cuda(); }63void cv::cuda::fastNlMeansDenoising(InputArray, OutputArray, float, int, int, Stream&) { throw_no_cuda(); }64void cv::cuda::fastNlMeansDenoisingColored(InputArray, OutputArray, float, float, int, int, Stream&) { throw_no_cuda(); }6566#else6768//////////////////////////////////////////////////////////////////////////////////69//// Non Local Means Denosing (brute force)7071namespace cv { namespace cuda { namespace device72{73namespace imgproc74{75template<typename T>76void nlm_bruteforce_gpu(const PtrStepSzb& src, PtrStepSzb dst, int search_radius, int block_radius, float h, int borderMode, cudaStream_t stream);77}78}}}7980void cv::cuda::nonLocalMeans(InputArray _src, OutputArray _dst, float h, int search_window, int block_window, int borderMode, Stream& stream)81{82using cv::cuda::device::imgproc::nlm_bruteforce_gpu;83typedef void (*func_t)(const PtrStepSzb& src, PtrStepSzb dst, int search_radius, int block_radius, float h, int borderMode, cudaStream_t stream);8485static const func_t funcs[4] = { nlm_bruteforce_gpu<uchar>, nlm_bruteforce_gpu<uchar2>, nlm_bruteforce_gpu<uchar3>, 0/*nlm_bruteforce_gpu<uchar4>,*/ };8687const GpuMat src = _src.getGpuMat();8889CV_Assert(src.type() == CV_8U || src.type() == CV_8UC2 || src.type() == CV_8UC3);9091const func_t func = funcs[src.channels() - 1];92CV_Assert(func != 0);9394int b = borderMode;95CV_Assert(b == BORDER_REFLECT101 || b == BORDER_REPLICATE || b == BORDER_CONSTANT || b == BORDER_REFLECT || b == BORDER_WRAP);9697_dst.create(src.size(), src.type());98GpuMat dst = _dst.getGpuMat();99100func(src, dst, search_window/2, block_window/2, h, borderMode, StreamAccessor::getStream(stream));101}102103namespace cv { namespace cuda { namespace device104{105namespace imgproc106{107void nln_fast_get_buffer_size(const PtrStepSzb& src, int search_window, int block_window, int& buffer_cols, int& buffer_rows);108109template<typename T>110void nlm_fast_gpu(const PtrStepSzb& src, PtrStepSzb dst, PtrStepi buffer,111int search_window, int block_window, float h, cudaStream_t stream);112113void fnlm_split_channels(const PtrStepSz<uchar3>& lab, PtrStepb l, PtrStep<uchar2> ab, cudaStream_t stream);114void fnlm_merge_channels(const PtrStepb& l, const PtrStep<uchar2>& ab, PtrStepSz<uchar3> lab, cudaStream_t stream);115}116}}}117118void cv::cuda::fastNlMeansDenoising(InputArray _src, OutputArray _dst, float h, int search_window, int block_window, Stream& stream)119{120const GpuMat src = _src.getGpuMat();121122CV_Assert(src.depth() == CV_8U && src.channels() < 4);123124int border_size = search_window/2 + block_window/2;125Size esize = src.size() + Size(border_size, border_size) * 2;126127BufferPool pool(stream);128129GpuMat extended_src = pool.getBuffer(esize, src.type());130cv::cuda::copyMakeBorder(src, extended_src, border_size, border_size, border_size, border_size, cv::BORDER_DEFAULT, Scalar(), stream);131GpuMat src_hdr = extended_src(Rect(Point2i(border_size, border_size), src.size()));132133int bcols, brows;134device::imgproc::nln_fast_get_buffer_size(src_hdr, search_window, block_window, bcols, brows);135GpuMat buffer = pool.getBuffer(brows, bcols, CV_32S);136137using namespace cv::cuda::device::imgproc;138typedef void (*nlm_fast_t)(const PtrStepSzb&, PtrStepSzb, PtrStepi, int, int, float, cudaStream_t);139static const nlm_fast_t funcs[] = { nlm_fast_gpu<uchar>, nlm_fast_gpu<uchar2>, nlm_fast_gpu<uchar3>, 0};140141_dst.create(src.size(), src.type());142GpuMat dst = _dst.getGpuMat();143144funcs[src.channels()-1](src_hdr, dst, buffer, search_window, block_window, h, StreamAccessor::getStream(stream));145}146147void cv::cuda::fastNlMeansDenoisingColored(InputArray _src, OutputArray _dst, float h_luminance, float h_color, int search_window, int block_window, Stream& stream)148{149const GpuMat src = _src.getGpuMat();150151CV_Assert(src.type() == CV_8UC3);152153BufferPool pool(stream);154155GpuMat lab = pool.getBuffer(src.size(), src.type());156cv::cuda::cvtColor(src, lab, cv::COLOR_BGR2Lab, 0, stream);157158GpuMat l = pool.getBuffer(src.size(), CV_8U);159GpuMat ab = pool.getBuffer(src.size(), CV_8UC2);160device::imgproc::fnlm_split_channels(lab, l, ab, StreamAccessor::getStream(stream));161162fastNlMeansDenoising(l, l, h_luminance, search_window, block_window, stream);163fastNlMeansDenoising(ab, ab, h_color, search_window, block_window, stream);164165device::imgproc::fnlm_merge_channels(l, ab, lab, StreamAccessor::getStream(stream));166cv::cuda::cvtColor(lab, _dst, cv::COLOR_Lab2BGR, 0, stream);167}168169#endif170171172