Path: blob/master/modules/stitching/src/cuda/build_warp_maps.cu
16337 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2009, Willow Garage Inc., all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// warranties of merchantability and fitness for a particular purpose are disclaimed.32// In no event shall the Intel Corporation or contributors be liable for any direct,33// indirect, incidental, special, exemplary, or consequential damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142#if !defined CUDA_DISABLER4344#include "opencv2/core/cuda/common.hpp"45#include "opencv2/core/cuda/vec_traits.hpp"46#include "opencv2/core/cuda/vec_math.hpp"47#include "opencv2/core/cuda/saturate_cast.hpp"48#include "opencv2/core/cuda/border_interpolate.hpp"4950namespace cv { namespace cuda { namespace device51{52namespace imgproc53{54// TODO use intrinsics like __sinf and so on5556namespace build_warp_maps57{5859__constant__ float ck_rinv[9];60__constant__ float cr_kinv[9];61__constant__ float ct[3];62__constant__ float cscale;63}646566class PlaneMapper67{68public:69static __device__ __forceinline__ void mapBackward(float u, float v, float &x, float &y)70{71using namespace build_warp_maps;7273float x_ = u / cscale - ct[0];74float y_ = v / cscale - ct[1];7576float z;77x = ck_rinv[0] * x_ + ck_rinv[1] * y_ + ck_rinv[2] * (1 - ct[2]);78y = ck_rinv[3] * x_ + ck_rinv[4] * y_ + ck_rinv[5] * (1 - ct[2]);79z = ck_rinv[6] * x_ + ck_rinv[7] * y_ + ck_rinv[8] * (1 - ct[2]);8081x /= z;82y /= z;83}84};858687class CylindricalMapper88{89public:90static __device__ __forceinline__ void mapBackward(float u, float v, float &x, float &y)91{92using namespace build_warp_maps;9394u /= cscale;95float x_ = ::sinf(u);96float y_ = v / cscale;97float z_ = ::cosf(u);9899float z;100x = ck_rinv[0] * x_ + ck_rinv[1] * y_ + ck_rinv[2] * z_;101y = ck_rinv[3] * x_ + ck_rinv[4] * y_ + ck_rinv[5] * z_;102z = ck_rinv[6] * x_ + ck_rinv[7] * y_ + ck_rinv[8] * z_;103104if (z > 0) { x /= z; y /= z; }105else x = y = -1;106}107};108109110class SphericalMapper111{112public:113static __device__ __forceinline__ void mapBackward(float u, float v, float &x, float &y)114{115using namespace build_warp_maps;116117v /= cscale;118u /= cscale;119120float sinv = ::sinf(v);121float x_ = sinv * ::sinf(u);122float y_ = -::cosf(v);123float z_ = sinv * ::cosf(u);124125float z;126x = ck_rinv[0] * x_ + ck_rinv[1] * y_ + ck_rinv[2] * z_;127y = ck_rinv[3] * x_ + ck_rinv[4] * y_ + ck_rinv[5] * z_;128z = ck_rinv[6] * x_ + ck_rinv[7] * y_ + ck_rinv[8] * z_;129130if (z > 0) { x /= z; y /= z; }131else x = y = -1;132}133};134135136template <typename Mapper>137__global__ void buildWarpMapsKernel(int tl_u, int tl_v, int cols, int rows,138PtrStepf map_x, PtrStepf map_y)139{140int du = blockIdx.x * blockDim.x + threadIdx.x;141int dv = blockIdx.y * blockDim.y + threadIdx.y;142if (du < cols && dv < rows)143{144float u = tl_u + du;145float v = tl_v + dv;146float x, y;147Mapper::mapBackward(u, v, x, y);148map_x.ptr(dv)[du] = x;149map_y.ptr(dv)[du] = y;150}151}152153154void buildWarpPlaneMaps(int tl_u, int tl_v, PtrStepSzf map_x, PtrStepSzf map_y,155const float k_rinv[9], const float r_kinv[9], const float t[3],156float scale, cudaStream_t stream)157{158cudaSafeCall(cudaMemcpyToSymbol(build_warp_maps::ck_rinv, k_rinv, 9*sizeof(float)));159cudaSafeCall(cudaMemcpyToSymbol(build_warp_maps::cr_kinv, r_kinv, 9*sizeof(float)));160cudaSafeCall(cudaMemcpyToSymbol(build_warp_maps::ct, t, 3*sizeof(float)));161cudaSafeCall(cudaMemcpyToSymbol(build_warp_maps::cscale, &scale, sizeof(float)));162163int cols = map_x.cols;164int rows = map_x.rows;165166dim3 threads(32, 8);167dim3 grid(divUp(cols, threads.x), divUp(rows, threads.y));168169buildWarpMapsKernel<PlaneMapper><<<grid,threads>>>(tl_u, tl_v, cols, rows, map_x, map_y);170cudaSafeCall(cudaGetLastError());171if (stream == 0)172cudaSafeCall(cudaDeviceSynchronize());173}174175176void buildWarpCylindricalMaps(int tl_u, int tl_v, PtrStepSzf map_x, PtrStepSzf map_y,177const float k_rinv[9], const float r_kinv[9], float scale,178cudaStream_t stream)179{180cudaSafeCall(cudaMemcpyToSymbol(build_warp_maps::ck_rinv, k_rinv, 9*sizeof(float)));181cudaSafeCall(cudaMemcpyToSymbol(build_warp_maps::cr_kinv, r_kinv, 9*sizeof(float)));182cudaSafeCall(cudaMemcpyToSymbol(build_warp_maps::cscale, &scale, sizeof(float)));183184int cols = map_x.cols;185int rows = map_x.rows;186187dim3 threads(32, 8);188dim3 grid(divUp(cols, threads.x), divUp(rows, threads.y));189190buildWarpMapsKernel<CylindricalMapper><<<grid,threads>>>(tl_u, tl_v, cols, rows, map_x, map_y);191cudaSafeCall(cudaGetLastError());192if (stream == 0)193cudaSafeCall(cudaDeviceSynchronize());194}195196197void buildWarpSphericalMaps(int tl_u, int tl_v, PtrStepSzf map_x, PtrStepSzf map_y,198const float k_rinv[9], const float r_kinv[9], float scale,199cudaStream_t stream)200{201cudaSafeCall(cudaMemcpyToSymbol(build_warp_maps::ck_rinv, k_rinv, 9*sizeof(float)));202cudaSafeCall(cudaMemcpyToSymbol(build_warp_maps::cr_kinv, r_kinv, 9*sizeof(float)));203cudaSafeCall(cudaMemcpyToSymbol(build_warp_maps::cscale, &scale, sizeof(float)));204205int cols = map_x.cols;206int rows = map_x.rows;207208dim3 threads(32, 8);209dim3 grid(divUp(cols, threads.x), divUp(rows, threads.y));210211buildWarpMapsKernel<SphericalMapper><<<grid,threads>>>(tl_u, tl_v, cols, rows, map_x, map_y);212cudaSafeCall(cudaGetLastError());213if (stream == 0)214cudaSafeCall(cudaDeviceSynchronize());215}216} // namespace imgproc217}}} // namespace cv { namespace cuda { namespace cudev {218219220#endif /* CUDA_DISABLER */221222223