1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Copyright (C) 1993-2011, NVIDIA Corporation, all rights reserved.
16 // Third party copyrights are property of their respective owners.
18 // Redistribution and use in source and binary forms, with or without modification,
19 // are permitted provided that the following conditions are met:
21 // * Redistribution's of source code must retain the above copyright notice,
22 // this list of conditions and the following disclaimer.
24 // * Redistribution's in binary form must reproduce the above copyright notice,
25 // this list of conditions and the following disclaimer in the documentation
26 // and/or other materials provided with the distribution.
28 // * The name of the copyright holders may not be used to endorse or promote products
29 // derived from this software without specific prior written permission.
31 // This software is provided by the copyright holders and contributors "as is" and
32 // any express or bpied warranties, including, but not limited to, the bpied
33 // warranties of merchantability and fitness for a particular purpose are disclaimed.
34 // In no event shall the Intel Corporation or contributors be liable for any direct,
35 // indirect, incidental, special, exemplary, or consequential damages
36 // (including, but not limited to, procurement of substitute goods or services;
37 // loss of use, data, or profits; or business interruption) however caused
38 // and on any theory of liability, whether in contract, strict liability,
39 // or tort (including negligence or otherwise) arising in any way out of
40 // the use of this software, even if advised of the possibility of such damage.
44 #include "internal_shared.hpp"
46 #include "opencv2/gpu/device/vec_traits.hpp"
47 #include "opencv2/gpu/device/vec_math.hpp"
48 #include "opencv2/gpu/device/border_interpolate.hpp"
50 using namespace cv::gpu;
52 typedef unsigned char uchar;
53 typedef unsigned short ushort;
55 //////////////////////////////////////////////////////////////////////////////////
56 /// Non local means denosings
58 namespace cv { namespace gpu { namespace device
62 __device__ __forceinline__ float norm2(const float& v) { return v*v; }
63 __device__ __forceinline__ float norm2(const float2& v) { return v.x*v.x + v.y*v.y; }
64 __device__ __forceinline__ float norm2(const float3& v) { return v.x*v.x + v.y*v.y + v.z*v.z; }
65 __device__ __forceinline__ float norm2(const float4& v) { return v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w; }
67 template<typename T, typename B>
68 __global__ void nlm_kernel(const PtrStepSz<T> src, PtrStep<T> dst, const B b, int search_radius, int block_radius, float h2_inv_half)
70 typedef typename TypeVec<float, VecTraits<T>::cn>::vec_type value_type;
72 const int x = blockDim.x * blockIdx.x + threadIdx.x;
73 const int y = blockDim.y * blockIdx.y + threadIdx.y;
75 if (x >= src.cols || y >= src.rows)
78 float block_radius2_inv = -1.f/(block_radius * block_radius);
80 value_type sum1 = VecTraits<value_type>::all(0);
83 for(float cy = -search_radius; cy <= search_radius; ++cy)
84 for(float cx = -search_radius; cx <= search_radius; ++cx)
87 for(float by = -block_radius; by <= block_radius; ++by)
88 for(float bx = -block_radius; bx <= block_radius; ++bx)
90 value_type v1 = saturate_cast<value_type>(src(y + by, x + bx));
91 value_type v2 = saturate_cast<value_type>(src(y + cy + by, x + cx + bx));
92 color2 += norm2(v1 - v2);
95 float dist2 = cx * cx + cy * cy;
96 float w = __expf(color2 * h2_inv_half + dist2 * block_radius2_inv);
98 sum1 = sum1 + saturate_cast<value_type>(src(y + cy, x + cy)) * w;
102 dst(y, x) = saturate_cast<T>(sum1 / sum2);
106 template<typename T, template <typename> class B>
107 void nlm_caller(const PtrStepSzb src, PtrStepSzb dst, int search_radius, int block_radius, float h, cudaStream_t stream)
110 dim3 grid (divUp (src.cols, block.x), divUp (src.rows, block.y));
112 B<T> b(src.rows, src.cols);
114 float h2_inv_half = -0.5f/(h * h * VecTraits<T>::cn);
116 cudaSafeCall( cudaFuncSetCacheConfig (nlm_kernel<T, B<T> >, cudaFuncCachePreferL1) );
117 nlm_kernel<<<grid, block>>>((PtrStepSz<T>)src, (PtrStepSz<T>)dst, b, search_radius, block_radius, h2_inv_half);
118 cudaSafeCall ( cudaGetLastError () );
121 cudaSafeCall( cudaDeviceSynchronize() );
125 void nlm_bruteforce_gpu(const PtrStepSzb& src, PtrStepSzb dst, int search_radius, int block_radius, float h, int borderMode, cudaStream_t stream)
127 typedef void (*func_t)(const PtrStepSzb src, PtrStepSzb dst, int search_radius, int block_radius, float h, cudaStream_t stream);
129 static func_t funcs[] =
131 nlm_caller<T, BrdReflect101>,
132 nlm_caller<T, BrdReplicate>,
133 nlm_caller<T, BrdConstant>,
134 nlm_caller<T, BrdReflect>,
135 nlm_caller<T, BrdWrap>,
137 funcs[borderMode](src, dst, search_radius, block_radius, h, stream);
140 template void nlm_bruteforce_gpu<uchar>(const PtrStepSzb&, PtrStepSzb, int, int, float, int, cudaStream_t);
141 template void nlm_bruteforce_gpu<uchar3>(const PtrStepSzb&, PtrStepSzb, int, int, float, int, cudaStream_t);