2012-08-29 20:49:07 +08:00
|
|
|
/*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
|
2013-03-21 17:31:51 +08:00
|
|
|
// and/or other materials provided with the distribution.
|
2012-08-29 20:49:07 +08:00
|
|
|
//
|
|
|
|
// * 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
|
2013-03-21 17:31:51 +08:00
|
|
|
// any express or implied warranties, including, but not limited to, the implied
|
2012-08-29 20:49:07 +08:00
|
|
|
// 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"
|
|
|
|
|
2013-07-24 16:01:34 +08:00
|
|
|
#include "opencv2/photo/cuda.hpp"
|
2013-07-23 18:12:04 +08:00
|
|
|
#include "opencv2/core/private.cuda.hpp"
|
2013-06-04 17:32:35 +08:00
|
|
|
|
|
|
|
#include "opencv2/opencv_modules.hpp"
|
|
|
|
|
2013-07-23 19:24:10 +08:00
|
|
|
#ifdef HAVE_OPENCV_CUDAARITHM
|
|
|
|
# include "opencv2/cudaarithm.hpp"
|
2013-06-04 17:32:35 +08:00
|
|
|
#endif
|
|
|
|
|
2013-07-23 20:59:34 +08:00
|
|
|
#ifdef HAVE_OPENCV_CUDAIMGPROC
|
|
|
|
# include "opencv2/cudaimgproc.hpp"
|
2013-06-04 17:32:35 +08:00
|
|
|
#endif
|
|
|
|
|
2012-08-29 20:49:07 +08:00
|
|
|
using namespace cv;
|
2013-08-28 19:45:13 +08:00
|
|
|
using namespace cv::cuda;
|
2012-08-29 20:49:07 +08:00
|
|
|
|
2013-07-23 20:59:34 +08:00
|
|
|
#if !defined (HAVE_CUDA) || !defined(HAVE_OPENCV_CUDAARITHM) || !defined(HAVE_OPENCV_CUDAIMGPROC)
|
2012-08-29 20:49:07 +08:00
|
|
|
|
2015-01-15 21:45:35 +08:00
|
|
|
void cv::cuda::nonLocalMeans(InputArray, OutputArray, float, int, int, int, Stream&) { throw_no_cuda(); }
|
|
|
|
void cv::cuda::fastNlMeansDenoising(InputArray, OutputArray, float, int, int, Stream&) { throw_no_cuda(); }
|
|
|
|
void cv::cuda::fastNlMeansDenoisingColored(InputArray, OutputArray, float, float, int, int, Stream&) { throw_no_cuda(); }
|
2012-10-09 01:48:56 +08:00
|
|
|
|
2012-08-29 20:49:07 +08:00
|
|
|
#else
|
|
|
|
|
2012-09-27 22:11:06 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Non Local Means Denosing (brute force)
|
2012-08-29 20:49:07 +08:00
|
|
|
|
2013-07-23 15:33:51 +08:00
|
|
|
namespace cv { namespace cuda { namespace device
|
2012-08-29 20:49:07 +08:00
|
|
|
{
|
|
|
|
namespace imgproc
|
|
|
|
{
|
|
|
|
template<typename T>
|
|
|
|
void nlm_bruteforce_gpu(const PtrStepSzb& src, PtrStepSzb dst, int search_radius, int block_radius, float h, int borderMode, cudaStream_t stream);
|
|
|
|
}
|
|
|
|
}}}
|
|
|
|
|
2015-01-15 21:45:35 +08:00
|
|
|
void cv::cuda::nonLocalMeans(InputArray _src, OutputArray _dst, float h, int search_window, int block_window, int borderMode, Stream& stream)
|
2012-08-29 20:49:07 +08:00
|
|
|
{
|
2013-07-23 15:33:51 +08:00
|
|
|
using cv::cuda::device::imgproc::nlm_bruteforce_gpu;
|
2012-08-29 20:49:07 +08:00
|
|
|
typedef void (*func_t)(const PtrStepSzb& src, PtrStepSzb dst, int search_radius, int block_radius, float h, int borderMode, cudaStream_t stream);
|
|
|
|
|
2012-09-27 22:11:06 +08:00
|
|
|
static const func_t funcs[4] = { nlm_bruteforce_gpu<uchar>, nlm_bruteforce_gpu<uchar2>, nlm_bruteforce_gpu<uchar3>, 0/*nlm_bruteforce_gpu<uchar4>,*/ };
|
2012-08-29 20:49:07 +08:00
|
|
|
|
2015-01-15 21:45:35 +08:00
|
|
|
const GpuMat src = _src.getGpuMat();
|
|
|
|
|
2012-09-27 22:11:06 +08:00
|
|
|
CV_Assert(src.type() == CV_8U || src.type() == CV_8UC2 || src.type() == CV_8UC3);
|
2012-08-29 20:49:07 +08:00
|
|
|
|
|
|
|
const func_t func = funcs[src.channels() - 1];
|
|
|
|
CV_Assert(func != 0);
|
|
|
|
|
|
|
|
int b = borderMode;
|
|
|
|
CV_Assert(b == BORDER_REFLECT101 || b == BORDER_REPLICATE || b == BORDER_CONSTANT || b == BORDER_REFLECT || b == BORDER_WRAP);
|
|
|
|
|
2015-01-15 21:45:35 +08:00
|
|
|
_dst.create(src.size(), src.type());
|
|
|
|
GpuMat dst = _dst.getGpuMat();
|
|
|
|
|
|
|
|
func(src, dst, search_window/2, block_window/2, h, borderMode, StreamAccessor::getStream(stream));
|
2012-08-29 20:49:07 +08:00
|
|
|
}
|
|
|
|
|
2013-07-23 15:33:51 +08:00
|
|
|
namespace cv { namespace cuda { namespace device
|
2012-09-27 22:11:06 +08:00
|
|
|
{
|
|
|
|
namespace imgproc
|
|
|
|
{
|
|
|
|
void nln_fast_get_buffer_size(const PtrStepSzb& src, int search_window, int block_window, int& buffer_cols, int& buffer_rows);
|
2012-08-29 20:49:07 +08:00
|
|
|
|
2012-09-27 22:11:06 +08:00
|
|
|
template<typename T>
|
|
|
|
void nlm_fast_gpu(const PtrStepSzb& src, PtrStepSzb dst, PtrStepi buffer,
|
|
|
|
int search_window, int block_window, float h, cudaStream_t stream);
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-10-04 23:36:48 +08:00
|
|
|
void fnlm_split_channels(const PtrStepSz<uchar3>& lab, PtrStepb l, PtrStep<uchar2> ab, cudaStream_t stream);
|
|
|
|
void fnlm_merge_channels(const PtrStepb& l, const PtrStep<uchar2>& ab, PtrStepSz<uchar3> lab, cudaStream_t stream);
|
|
|
|
}
|
2012-09-27 22:11:06 +08:00
|
|
|
}}}
|
|
|
|
|
2015-01-15 21:45:35 +08:00
|
|
|
void cv::cuda::fastNlMeansDenoising(InputArray _src, OutputArray _dst, float h, int search_window, int block_window, Stream& stream)
|
2012-09-27 22:11:06 +08:00
|
|
|
{
|
2015-01-15 21:45:35 +08:00
|
|
|
const GpuMat src = _src.getGpuMat();
|
|
|
|
|
2012-09-27 22:11:06 +08:00
|
|
|
CV_Assert(src.depth() == CV_8U && src.channels() < 4);
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
int border_size = search_window/2 + block_window/2;
|
2012-10-04 23:36:48 +08:00
|
|
|
Size esize = src.size() + Size(border_size, border_size) * 2;
|
|
|
|
|
2015-01-15 21:45:35 +08:00
|
|
|
BufferPool pool(stream);
|
2012-09-27 22:11:06 +08:00
|
|
|
|
2015-01-15 21:45:35 +08:00
|
|
|
GpuMat extended_src = pool.getBuffer(esize, src.type());
|
|
|
|
cv::cuda::copyMakeBorder(src, extended_src, border_size, border_size, border_size, border_size, cv::BORDER_DEFAULT, Scalar(), stream);
|
2012-10-04 23:36:48 +08:00
|
|
|
GpuMat src_hdr = extended_src(Rect(Point2i(border_size, border_size), src.size()));
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2012-10-04 23:36:48 +08:00
|
|
|
int bcols, brows;
|
2013-07-23 15:33:51 +08:00
|
|
|
device::imgproc::nln_fast_get_buffer_size(src_hdr, search_window, block_window, bcols, brows);
|
2015-01-15 21:45:35 +08:00
|
|
|
GpuMat buffer = pool.getBuffer(brows, bcols, CV_32S);
|
2012-09-27 22:11:06 +08:00
|
|
|
|
2013-07-23 15:33:51 +08:00
|
|
|
using namespace cv::cuda::device::imgproc;
|
2012-09-27 22:11:06 +08:00
|
|
|
typedef void (*nlm_fast_t)(const PtrStepSzb&, PtrStepSzb, PtrStepi, int, int, float, cudaStream_t);
|
2012-10-17 07:18:30 +08:00
|
|
|
static const nlm_fast_t funcs[] = { nlm_fast_gpu<uchar>, nlm_fast_gpu<uchar2>, nlm_fast_gpu<uchar3>, 0};
|
|
|
|
|
2015-01-15 21:45:35 +08:00
|
|
|
_dst.create(src.size(), src.type());
|
|
|
|
GpuMat dst = _dst.getGpuMat();
|
|
|
|
|
|
|
|
funcs[src.channels()-1](src_hdr, dst, buffer, search_window, block_window, h, StreamAccessor::getStream(stream));
|
2012-10-04 23:36:48 +08:00
|
|
|
}
|
|
|
|
|
2015-01-15 21:45:35 +08:00
|
|
|
void cv::cuda::fastNlMeansDenoisingColored(InputArray _src, OutputArray _dst, float h_luminance, float h_color, int search_window, int block_window, Stream& stream)
|
2012-10-17 07:18:30 +08:00
|
|
|
{
|
2015-01-15 21:45:35 +08:00
|
|
|
const GpuMat src = _src.getGpuMat();
|
|
|
|
|
2012-10-04 23:36:48 +08:00
|
|
|
CV_Assert(src.type() == CV_8UC3);
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2015-01-15 21:45:35 +08:00
|
|
|
BufferPool pool(stream);
|
|
|
|
|
|
|
|
GpuMat lab = pool.getBuffer(src.size(), src.type());
|
|
|
|
cv::cuda::cvtColor(src, lab, cv::COLOR_BGR2Lab, 0, stream);
|
2012-09-27 22:11:06 +08:00
|
|
|
|
2015-01-15 21:45:35 +08:00
|
|
|
GpuMat l = pool.getBuffer(src.size(), CV_8U);
|
|
|
|
GpuMat ab = pool.getBuffer(src.size(), CV_8UC2);
|
|
|
|
device::imgproc::fnlm_split_channels(lab, l, ab, StreamAccessor::getStream(stream));
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2015-01-15 21:45:35 +08:00
|
|
|
fastNlMeansDenoising(l, l, h_luminance, search_window, block_window, stream);
|
|
|
|
fastNlMeansDenoising(ab, ab, h_color, search_window, block_window, stream);
|
2012-09-27 22:11:06 +08:00
|
|
|
|
2015-01-15 21:45:35 +08:00
|
|
|
device::imgproc::fnlm_merge_channels(l, ab, lab, StreamAccessor::getStream(stream));
|
|
|
|
cv::cuda::cvtColor(lab, _dst, cv::COLOR_Lab2BGR, 0, stream);
|
2012-10-04 23:36:48 +08:00
|
|
|
}
|
2012-08-29 20:49:07 +08:00
|
|
|
|
2012-08-30 14:35:32 +08:00
|
|
|
#endif
|