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
// Third party copyrights are property of their respective owners.
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
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.
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.
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.
43
#if !defined CUDA_DISABLER
45
#include "opencv2/core/cuda/common.hpp"
46
#include "opencv2/core/cuda/transform.hpp"
47
#include "opencv2/core/cuda/functional.hpp"
48
#include "opencv2/core/cuda/reduce.hpp"
50
namespace cv { namespace cuda { namespace device
52
/////////////////////////////////// reprojectImageTo3D ///////////////////////////////////////////////
54
__constant__ float cq[16];
56
template <typename T, typename D>
57
__global__ void reprojectImageTo3D(const PtrStepSz<T> disp, PtrStep<D> xyz)
59
const int x = blockIdx.x * blockDim.x + threadIdx.x;
60
const int y = blockIdx.y * blockDim.y + threadIdx.y;
62
if (y >= disp.rows || x >= disp.cols)
65
const float qx = x * cq[ 0] + y * cq[ 1] + cq[ 3];
66
const float qy = x * cq[ 4] + y * cq[ 5] + cq[ 7];
67
const float qz = x * cq[ 8] + y * cq[ 9] + cq[11];
68
const float qw = x * cq[12] + y * cq[13] + cq[15];
70
const T d = disp(y, x);
72
const float iW = 1.f / (qw + cq[14] * d);
74
D v = VecTraits<D>::all(1.0f);
75
v.x = (qx + cq[2] * d) * iW;
76
v.y = (qy + cq[6] * d) * iW;
77
v.z = (qz + cq[10] * d) * iW;
82
template <typename T, typename D>
83
void reprojectImageTo3D_gpu(const PtrStepSzb disp, PtrStepSzb xyz, const float* q, cudaStream_t stream)
86
dim3 grid(divUp(disp.cols, block.x), divUp(disp.rows, block.y));
88
cudaSafeCall( cudaMemcpyToSymbol(cq, q, 16 * sizeof(float)) );
90
reprojectImageTo3D<T, D><<<grid, block, 0, stream>>>((PtrStepSz<T>)disp, (PtrStepSz<D>)xyz);
91
cudaSafeCall( cudaGetLastError() );
94
cudaSafeCall( cudaDeviceSynchronize() );
97
template void reprojectImageTo3D_gpu<uchar, float3>(const PtrStepSzb disp, PtrStepSzb xyz, const float* q, cudaStream_t stream);
98
template void reprojectImageTo3D_gpu<uchar, float4>(const PtrStepSzb disp, PtrStepSzb xyz, const float* q, cudaStream_t stream);
99
template void reprojectImageTo3D_gpu<short, float3>(const PtrStepSzb disp, PtrStepSzb xyz, const float* q, cudaStream_t stream);
100
template void reprojectImageTo3D_gpu<short, float4>(const PtrStepSzb disp, PtrStepSzb xyz, const float* q, cudaStream_t stream);
101
template void reprojectImageTo3D_gpu<int, float3>(const PtrStepSzb disp, PtrStepSzb xyz, const float* q, cudaStream_t stream);
102
template void reprojectImageTo3D_gpu<int, float4>(const PtrStepSzb disp, PtrStepSzb xyz, const float* q, cudaStream_t stream);
103
template void reprojectImageTo3D_gpu<float, float3>(const PtrStepSzb disp, PtrStepSzb xyz, const float* q, cudaStream_t stream);
104
template void reprojectImageTo3D_gpu<float, float4>(const PtrStepSzb disp, PtrStepSzb xyz, const float* q, cudaStream_t stream);
106
/////////////////////////////////// drawColorDisp ///////////////////////////////////////////////
108
template <typename T>
109
__device__ unsigned int cvtPixel(T d, int ndisp, float S = 1, float V = 1)
111
unsigned int H = ((ndisp-d) * 240)/ndisp;
113
unsigned int hi = (H/60) % 6;
114
float f = H/60.f - H/60;
115
float p = V * (1 - S);
116
float q = V * (1 - f * S);
117
float t = V * (1 - (1 - f) * S);
121
if (hi == 0) //R = V, G = t, B = p
128
if (hi == 1) // R = q, G = V, B = p
135
if (hi == 2) // R = p, G = V, B = t
142
if (hi == 3) // R = p, G = q, B = V
149
if (hi == 4) // R = t, G = p, B = V
156
if (hi == 5) // R = V, G = p, B = q
162
const unsigned int b = (unsigned int)(::max(0.f, ::min(res.x, 1.f)) * 255.f);
163
const unsigned int g = (unsigned int)(::max(0.f, ::min(res.y, 1.f)) * 255.f);
164
const unsigned int r = (unsigned int)(::max(0.f, ::min(res.z, 1.f)) * 255.f);
165
const unsigned int a = 255U;
167
return (a << 24) + (r << 16) + (g << 8) + b;
170
__global__ void drawColorDisp(uchar* disp, size_t disp_step, uchar* out_image, size_t out_step, int width, int height, int ndisp)
172
const int x = (blockIdx.x * blockDim.x + threadIdx.x) << 2;
173
const int y = blockIdx.y * blockDim.y + threadIdx.y;
175
if(x < width && y < height)
177
uchar4 d4 = *(uchar4*)(disp + y * disp_step + x);
180
res.x = cvtPixel(d4.x, ndisp);
181
res.y = cvtPixel(d4.y, ndisp);
182
res.z = cvtPixel(d4.z, ndisp);
183
res.w = cvtPixel(d4.w, ndisp);
185
uint4* line = (uint4*)(out_image + y * out_step);
190
__global__ void drawColorDisp(short* disp, size_t disp_step, uchar* out_image, size_t out_step, int width, int height, int ndisp)
192
const int x = (blockIdx.x * blockDim.x + threadIdx.x) << 1;
193
const int y = blockIdx.y * blockDim.y + threadIdx.y;
195
if(x < width && y < height)
197
short2 d2 = *(short2*)(disp + y * disp_step + x);
200
res.x = cvtPixel(d2.x, ndisp);
201
res.y = cvtPixel(d2.y, ndisp);
203
uint2* line = (uint2*)(out_image + y * out_step);
208
__global__ void drawColorDisp(int* disp, size_t disp_step, uchar* out_image, size_t out_step, int width, int height, int ndisp)
210
const int x = blockIdx.x * blockDim.x + threadIdx.x;
211
const int y = blockIdx.y * blockDim.y + threadIdx.y;
213
if(x < width && y < height)
215
uint *line = (uint*)(out_image + y * out_step);
216
line[x] = cvtPixel(disp[y*disp_step + x], ndisp);
220
__global__ void drawColorDisp(float* disp, size_t disp_step, uchar* out_image, size_t out_step, int width, int height, int ndisp)
222
const int x = blockIdx.x * blockDim.x + threadIdx.x;
223
const int y = blockIdx.y * blockDim.y + threadIdx.y;
225
if(x < width && y < height)
227
uint *line = (uint*)(out_image + y * out_step);
228
line[x] = cvtPixel(disp[y*disp_step + x], ndisp);
232
void drawColorDisp_gpu(const PtrStepSzb& src, const PtrStepSzb& dst, int ndisp, const cudaStream_t& stream)
234
dim3 threads(16, 16, 1);
236
grid.x = divUp(src.cols, threads.x << 2);
237
grid.y = divUp(src.rows, threads.y);
239
drawColorDisp<<<grid, threads, 0, stream>>>(src.data, src.step, dst.data, dst.step, src.cols, src.rows, ndisp);
240
cudaSafeCall( cudaGetLastError() );
243
cudaSafeCall( cudaDeviceSynchronize() );
246
void drawColorDisp_gpu(const PtrStepSz<short>& src, const PtrStepSzb& dst, int ndisp, const cudaStream_t& stream)
248
dim3 threads(32, 8, 1);
250
grid.x = divUp(src.cols, threads.x << 1);
251
grid.y = divUp(src.rows, threads.y);
253
drawColorDisp<<<grid, threads, 0, stream>>>(src.data, src.step / sizeof(short), dst.data, dst.step, src.cols, src.rows, ndisp);
254
cudaSafeCall( cudaGetLastError() );
257
cudaSafeCall( cudaDeviceSynchronize() );
260
void drawColorDisp_gpu(const PtrStepSz<int>& src, const PtrStepSzb& dst, int ndisp, const cudaStream_t& stream)
262
dim3 threads(32, 8, 1);
264
grid.x = divUp(src.cols, threads.x);
265
grid.y = divUp(src.rows, threads.y);
267
drawColorDisp<<<grid, threads, 0, stream>>>(src.data, src.step / sizeof(int), dst.data, dst.step, src.cols, src.rows, ndisp);
268
cudaSafeCall( cudaGetLastError() );
271
cudaSafeCall( cudaDeviceSynchronize() );
274
void drawColorDisp_gpu(const PtrStepSz<float>& src, const PtrStepSzb& dst, int ndisp, const cudaStream_t& stream)
276
dim3 threads(32, 8, 1);
278
grid.x = divUp(src.cols, threads.x);
279
grid.y = divUp(src.rows, threads.y);
281
drawColorDisp<<<grid, threads, 0, stream>>>(src.data, src.step / sizeof(float), dst.data, dst.step, src.cols, src.rows, ndisp);
282
cudaSafeCall( cudaGetLastError() );
285
cudaSafeCall( cudaDeviceSynchronize() );
287
}}} // namespace cv { namespace cuda { namespace cudev
290
#endif /* CUDA_DISABLER */