added GPU bilateral filter + tests
[profile/ivi/opencv.git] / modules / gpu / src / cuda / nlm.cu
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
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.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
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.
17 //
18 // Redistribution and use in source and binary forms, with or without modification,
19 // are permitted provided that the following conditions are met:
20 //
21 //   * Redistribution's of source code must retain the above copyright notice,
22 //     this list of conditions and the following disclaimer.
23 //
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.
27 //
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.
30 //
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.
41 //
42 //M*/
43
44 #include "internal_shared.hpp"
45
46 #include "opencv2/gpu/device/vec_traits.hpp"
47 #include "opencv2/gpu/device/vec_math.hpp"
48 #include "opencv2/gpu/device/border_interpolate.hpp"
49
50 using namespace cv::gpu;
51
52 typedef unsigned char uchar;
53 typedef unsigned short ushort;
54
55 //////////////////////////////////////////////////////////////////////////////////
56 /// Non local means denosings
57
58 namespace cv { namespace gpu { namespace device
59 {
60     namespace imgproc
61     {
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; }
66
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)
69         {
70             typedef typename TypeVec<float, VecTraits<T>::cn>::vec_type value_type;
71
72             const int x = blockDim.x * blockIdx.x + threadIdx.x;
73             const int y = blockDim.y * blockIdx.y + threadIdx.y;
74
75             if (x >= src.cols || y >= src.rows)
76                 return;
77
78             float block_radius2_inv = -1.f/(block_radius * block_radius);
79
80             value_type sum1 = VecTraits<value_type>::all(0);
81             float sum2 = 0.f;
82
83             for(float cy = -search_radius; cy <= search_radius; ++cy)
84                 for(float cx = -search_radius; cx <= search_radius; ++cx)
85                 {
86                     float color2 = 0;
87                     for(float by = -block_radius; by <= block_radius; ++by)
88                         for(float bx = -block_radius; bx <= block_radius; ++bx)
89                         {
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);
93                         }
94
95                     float dist2 = cx * cx + cy * cy;
96                     float w = __expf(color2 * h2_inv_half + dist2 * block_radius2_inv);
97                     
98                     sum1 = sum1 + saturate_cast<value_type>(src(y + cy, x + cy)) * w;
99                     sum2 += w;
100                 }
101
102             dst(y, x) = saturate_cast<T>(sum1 / sum2);
103
104         }
105
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)
108         {
109             dim3 block (32, 8);
110             dim3 grid (divUp (src.cols, block.x), divUp (src.rows, block.y));
111
112             B<T> b(src.rows, src.cols);
113
114             float h2_inv_half = -0.5f/(h * h * VecTraits<T>::cn);
115
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 () );
119
120             if (stream == 0)
121                 cudaSafeCall( cudaDeviceSynchronize() );
122         }
123
124         template<typename T>
125         void nlm_bruteforce_gpu(const PtrStepSzb& src, PtrStepSzb dst, int search_radius, int block_radius, float h, int borderMode, cudaStream_t stream)
126         {
127             typedef void (*func_t)(const PtrStepSzb src, PtrStepSzb dst, int search_radius, int block_radius, float h, cudaStream_t stream);
128
129             static func_t funcs[] = 
130             {
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>,
136             };
137             funcs[borderMode](src, dst, search_radius, block_radius, h, stream);
138         }
139
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);
142     }
143 }}}