~paparazzi-uav/paparazzi/v5.0-manual

« back to all changes in this revision

Viewing changes to sw/ext/opencv_bebop/opencv/modules/cudastereo/src/cuda/disparity_bilateral_filter.cu

  • Committer: Paparazzi buildbot
  • Date: 2016-05-18 15:00:29 UTC
  • Revision ID: felix.ruess+docbot@gmail.com-20160518150029-e8lgzi5kvb4p7un9
Manual import commit 4b8bbb730080dac23cf816b98908dacfabe2a8ec from v5.0 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
// Third party copyrights are property of their respective owners.
 
16
//
 
17
// Redistribution and use in source and binary forms, with or without modification,
 
18
// are permitted provided that the following conditions are met:
 
19
//
 
20
//   * Redistribution's of source code must retain the above copyright notice,
 
21
//     this list of conditions and the following disclaimer.
 
22
//
 
23
//   * Redistribution's in binary form must reproduce the above copyright notice,
 
24
//     this list of conditions and the following disclaimer in the documentation
 
25
//     and/or other materials provided with the distribution.
 
26
//
 
27
//   * The name of the copyright holders may not be used to endorse or promote products
 
28
//     derived from this software without specific prior written permission.
 
29
//
 
30
// This software is provided by the copyright holders and contributors "as is" and
 
31
// any express or implied warranties, including, but not limited to, the implied
 
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
 
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
 
34
// indirect, incidental, special, exemplary, or consequential damages
 
35
// (including, but not limited to, procurement of substitute goods or services;
 
36
// loss of use, data, or profits; or business interruption) however caused
 
37
// and on any theory of liability, whether in contract, strict liability,
 
38
// or tort (including negligence or otherwise) arising in any way out of
 
39
// the use of this software, even if advised of the possibility of such damage.
 
40
//
 
41
//M*/
 
42
 
 
43
#if !defined CUDA_DISABLER
 
44
 
 
45
#include "opencv2/core/cuda/common.hpp"
 
46
#include "opencv2/core/cuda/limits.hpp"
 
47
 
 
48
#include "cuda/disparity_bilateral_filter.hpp"
 
49
 
 
50
namespace cv { namespace cuda { namespace device
 
51
{
 
52
    namespace disp_bilateral_filter
 
53
    {
 
54
        template <int channels>
 
55
        struct DistRgbMax
 
56
        {
 
57
            static __device__ __forceinline__ uchar calc(const uchar* a, const uchar* b)
 
58
            {
 
59
                uchar x = ::abs(a[0] - b[0]);
 
60
                uchar y = ::abs(a[1] - b[1]);
 
61
                uchar z = ::abs(a[2] - b[2]);
 
62
                return (::max(::max(x, y), z));
 
63
            }
 
64
        };
 
65
 
 
66
        template <>
 
67
        struct DistRgbMax<1>
 
68
        {
 
69
            static __device__ __forceinline__ uchar calc(const uchar* a, const uchar* b)
 
70
            {
 
71
                return ::abs(a[0] - b[0]);
 
72
            }
 
73
        };
 
74
 
 
75
        template <int channels, typename T>
 
76
        __global__ void disp_bilateral_filter(int t, T* disp, size_t disp_step,
 
77
            const uchar* img, size_t img_step, int h, int w,
 
78
            const float* ctable_color, const float * ctable_space, size_t ctable_space_step,
 
79
            int cradius,
 
80
            short cedge_disc, short cmax_disc)
 
81
        {
 
82
            const int y = blockIdx.y * blockDim.y + threadIdx.y;
 
83
            const int x = ((blockIdx.x * blockDim.x + threadIdx.x) << 1) + ((y + t) & 1);
 
84
 
 
85
            T dp[5];
 
86
 
 
87
            if (y > 0 && y < h - 1 && x > 0 && x < w - 1)
 
88
            {
 
89
                dp[0] = *(disp + (y  ) * disp_step + x + 0);
 
90
                dp[1] = *(disp + (y-1) * disp_step + x + 0);
 
91
                dp[2] = *(disp + (y  ) * disp_step + x - 1);
 
92
                dp[3] = *(disp + (y+1) * disp_step + x + 0);
 
93
                dp[4] = *(disp + (y  ) * disp_step + x + 1);
 
94
 
 
95
                if(::abs(dp[1] - dp[0]) >= cedge_disc || ::abs(dp[2] - dp[0]) >= cedge_disc || ::abs(dp[3] - dp[0]) >= cedge_disc || ::abs(dp[4] - dp[0]) >= cedge_disc)
 
96
                {
 
97
                    const int ymin = ::max(0, y - cradius);
 
98
                    const int xmin = ::max(0, x - cradius);
 
99
                    const int ymax = ::min(h - 1, y + cradius);
 
100
                    const int xmax = ::min(w - 1, x + cradius);
 
101
 
 
102
                    float cost[] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
 
103
 
 
104
                    const uchar* ic = img + y * img_step + channels * x;
 
105
 
 
106
                    for(int yi = ymin; yi <= ymax; yi++)
 
107
                    {
 
108
                        const T* disp_y = disp + yi * disp_step;
 
109
 
 
110
                        for(int xi = xmin; xi <= xmax; xi++)
 
111
                        {
 
112
                            const uchar* in = img + yi * img_step + channels * xi;
 
113
 
 
114
                            uchar dist_rgb = DistRgbMax<channels>::calc(in, ic);
 
115
 
 
116
                            const float weight = ctable_color[dist_rgb] * (ctable_space + ::abs(y-yi)* ctable_space_step)[::abs(x-xi)];
 
117
 
 
118
                            const T disp_reg = disp_y[xi];
 
119
 
 
120
                            cost[0] += ::min(cmax_disc, ::abs(disp_reg - dp[0])) * weight;
 
121
                            cost[1] += ::min(cmax_disc, ::abs(disp_reg - dp[1])) * weight;
 
122
                            cost[2] += ::min(cmax_disc, ::abs(disp_reg - dp[2])) * weight;
 
123
                            cost[3] += ::min(cmax_disc, ::abs(disp_reg - dp[3])) * weight;
 
124
                            cost[4] += ::min(cmax_disc, ::abs(disp_reg - dp[4])) * weight;
 
125
                        }
 
126
                    }
 
127
 
 
128
                    float minimum = numeric_limits<float>::max();
 
129
                    int id = 0;
 
130
 
 
131
                    if (cost[0] < minimum)
 
132
                    {
 
133
                        minimum = cost[0];
 
134
                        id = 0;
 
135
                    }
 
136
                    if (cost[1] < minimum)
 
137
                    {
 
138
                        minimum = cost[1];
 
139
                        id = 1;
 
140
                    }
 
141
                    if (cost[2] < minimum)
 
142
                    {
 
143
                        minimum = cost[2];
 
144
                        id = 2;
 
145
                    }
 
146
                    if (cost[3] < minimum)
 
147
                    {
 
148
                        minimum = cost[3];
 
149
                        id = 3;
 
150
                    }
 
151
                    if (cost[4] < minimum)
 
152
                    {
 
153
                        minimum = cost[4];
 
154
                        id = 4;
 
155
                    }
 
156
 
 
157
                    *(disp + y * disp_step + x) = dp[id];
 
158
                }
 
159
            }
 
160
        }
 
161
 
 
162
        template <typename T>
 
163
        void disp_bilateral_filter(PtrStepSz<T> disp, PtrStepSzb img, int channels, int iters, const float *table_color, const float* table_space, size_t table_step, int radius, short edge_disc, short max_disc, cudaStream_t stream)
 
164
        {
 
165
            dim3 threads(32, 8, 1);
 
166
            dim3 grid(1, 1, 1);
 
167
            grid.x = divUp(disp.cols, threads.x << 1);
 
168
            grid.y = divUp(disp.rows, threads.y);
 
169
 
 
170
            switch (channels)
 
171
            {
 
172
            case 1:
 
173
                for (int i = 0; i < iters; ++i)
 
174
                {
 
175
                    disp_bilateral_filter<1><<<grid, threads, 0, stream>>>(0, disp.data, disp.step/sizeof(T), img.data, img.step, disp.rows, disp.cols, table_color, table_space, table_step, radius, edge_disc, max_disc);
 
176
                    cudaSafeCall( cudaGetLastError() );
 
177
 
 
178
                    disp_bilateral_filter<1><<<grid, threads, 0, stream>>>(1, disp.data, disp.step/sizeof(T), img.data, img.step, disp.rows, disp.cols, table_color, table_space, table_step, radius, edge_disc, max_disc);
 
179
                    cudaSafeCall( cudaGetLastError() );
 
180
                }
 
181
                break;
 
182
            case 3:
 
183
                for (int i = 0; i < iters; ++i)
 
184
                {
 
185
                    disp_bilateral_filter<3><<<grid, threads, 0, stream>>>(0, disp.data, disp.step/sizeof(T), img.data, img.step, disp.rows, disp.cols, table_color, table_space, table_step, radius, edge_disc, max_disc);
 
186
                    cudaSafeCall( cudaGetLastError() );
 
187
 
 
188
                    disp_bilateral_filter<3><<<grid, threads, 0, stream>>>(1, disp.data, disp.step/sizeof(T), img.data, img.step, disp.rows, disp.cols, table_color, table_space, table_step, radius, edge_disc, max_disc);
 
189
                    cudaSafeCall( cudaGetLastError() );
 
190
                }
 
191
                break;
 
192
            default:
 
193
                CV_Error(cv::Error::BadNumChannels, "Unsupported channels count");
 
194
            }
 
195
 
 
196
            if (stream == 0)
 
197
                cudaSafeCall( cudaDeviceSynchronize() );
 
198
        }
 
199
 
 
200
        template void disp_bilateral_filter<uchar>(PtrStepSz<uchar> disp, PtrStepSzb img, int channels, int iters, const float *table_color, const float *table_space, size_t table_step, int radius, short, short, cudaStream_t stream);
 
201
        template void disp_bilateral_filter<short>(PtrStepSz<short> disp, PtrStepSzb img, int channels, int iters, const float *table_color, const float *table_space, size_t table_step, int radius, short, short, cudaStream_t stream);
 
202
    } // namespace bilateral_filter
 
203
}}} // namespace cv { namespace cuda { namespace cudev
 
204
 
 
205
#endif /* CUDA_DISABLER */