Path: blob/master/modules/stitching/src/warpers_cuda.cpp
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#include "precomp.hpp"43#include "opencv2/core/private.cuda.hpp"4445using namespace cv;46using namespace cv::cuda;4748#ifdef HAVE_CUDA4950namespace cv { namespace cuda { namespace device51{52namespace imgproc53{54void buildWarpPlaneMaps(int tl_u, int tl_v, PtrStepSzf map_x, PtrStepSzf map_y,55const float k_rinv[9], const float r_kinv[9], const float t[3], float scale,56cudaStream_t stream);5758void buildWarpSphericalMaps(int tl_u, int tl_v, PtrStepSzf map_x, PtrStepSzf map_y,59const float k_rinv[9], const float r_kinv[9], float scale,60cudaStream_t stream);6162void buildWarpCylindricalMaps(int tl_u, int tl_v, PtrStepSzf map_x, PtrStepSzf map_y,63const float k_rinv[9], const float r_kinv[9], float scale,64cudaStream_t stream);65}66}}}6768static void buildWarpPlaneMaps(Size src_size, Rect dst_roi, InputArray _K, InputArray _R, InputArray _T,69float scale, OutputArray _map_x, OutputArray _map_y, Stream& stream = Stream::Null())70{71CV_UNUSED(src_size);7273Mat K = _K.getMat();74Mat R = _R.getMat();75Mat T = _T.getMat();7677CV_Assert( K.size() == Size(3,3) && K.type() == CV_32FC1 );78CV_Assert( R.size() == Size(3,3) && R.type() == CV_32FC1 );79CV_Assert( (T.size() == Size(3,1) || T.size() == Size(1,3)) && T.type() == CV_32FC1 && T.isContinuous() );8081Mat K_Rinv = K * R.t();82Mat R_Kinv = R * K.inv();83CV_Assert( K_Rinv.isContinuous() );84CV_Assert( R_Kinv.isContinuous() );8586_map_x.create(dst_roi.size(), CV_32FC1);87_map_y.create(dst_roi.size(), CV_32FC1);8889GpuMat map_x = _map_x.getGpuMat();90GpuMat map_y = _map_y.getGpuMat();9192device::imgproc::buildWarpPlaneMaps(dst_roi.tl().x, dst_roi.tl().y, map_x, map_y, K_Rinv.ptr<float>(), R_Kinv.ptr<float>(),93T.ptr<float>(), scale, StreamAccessor::getStream(stream));94}9596static void buildWarpSphericalMaps(Size src_size, Rect dst_roi, InputArray _K, InputArray _R, float scale,97OutputArray _map_x, OutputArray _map_y, Stream& stream = Stream::Null())98{99CV_UNUSED(src_size);100101Mat K = _K.getMat();102Mat R = _R.getMat();103104CV_Assert( K.size() == Size(3,3) && K.type() == CV_32FC1 );105CV_Assert( R.size() == Size(3,3) && R.type() == CV_32FC1 );106107Mat K_Rinv = K * R.t();108Mat R_Kinv = R * K.inv();109CV_Assert( K_Rinv.isContinuous() );110CV_Assert( R_Kinv.isContinuous() );111112_map_x.create(dst_roi.size(), CV_32FC1);113_map_y.create(dst_roi.size(), CV_32FC1);114115GpuMat map_x = _map_x.getGpuMat();116GpuMat map_y = _map_y.getGpuMat();117118device::imgproc::buildWarpSphericalMaps(dst_roi.tl().x, dst_roi.tl().y, map_x, map_y, K_Rinv.ptr<float>(), R_Kinv.ptr<float>(), scale, StreamAccessor::getStream(stream));119}120121static void buildWarpCylindricalMaps(Size src_size, Rect dst_roi, InputArray _K, InputArray _R, float scale,122OutputArray _map_x, OutputArray _map_y, Stream& stream = Stream::Null())123{124CV_UNUSED(src_size);125126Mat K = _K.getMat();127Mat R = _R.getMat();128129CV_Assert( K.size() == Size(3,3) && K.type() == CV_32FC1 );130CV_Assert( R.size() == Size(3,3) && R.type() == CV_32FC1 );131132Mat K_Rinv = K * R.t();133Mat R_Kinv = R * K.inv();134CV_Assert( K_Rinv.isContinuous() );135CV_Assert( R_Kinv.isContinuous() );136137_map_x.create(dst_roi.size(), CV_32FC1);138_map_y.create(dst_roi.size(), CV_32FC1);139140GpuMat map_x = _map_x.getGpuMat();141GpuMat map_y = _map_y.getGpuMat();142143device::imgproc::buildWarpCylindricalMaps(dst_roi.tl().x, dst_roi.tl().y, map_x, map_y, K_Rinv.ptr<float>(), R_Kinv.ptr<float>(), scale, StreamAccessor::getStream(stream));144}145146#endif147148Rect cv::detail::PlaneWarperGpu::buildMaps(Size src_size, InputArray K, InputArray R,149cuda::GpuMat & xmap, cuda::GpuMat & ymap)150{151return buildMaps(src_size, K, R, Mat::zeros(3, 1, CV_32F), xmap, ymap);152}153154Rect cv::detail::PlaneWarperGpu::buildMaps(Size src_size, InputArray K, InputArray R, InputArray T,155cuda::GpuMat & xmap, cuda::GpuMat & ymap)156{157#ifndef HAVE_CUDA158CV_UNUSED(src_size);159CV_UNUSED(K);160CV_UNUSED(R);161CV_UNUSED(T);162CV_UNUSED(xmap);163CV_UNUSED(ymap);164throw_no_cuda();165#else166projector_.setCameraParams(K, R, T);167168Point dst_tl, dst_br;169detectResultRoi(src_size, dst_tl, dst_br);170171::buildWarpPlaneMaps(src_size, Rect(dst_tl, Point(dst_br.x + 1, dst_br.y + 1)),172K, R, T, projector_.scale, xmap, ymap);173174return Rect(dst_tl, dst_br);175#endif176}177178Point cv::detail::PlaneWarperGpu::warp(const cuda::GpuMat & src, InputArray K, InputArray R,179int interp_mode, int border_mode,180cuda::GpuMat & dst)181{182return warp(src, K, R, Mat::zeros(3, 1, CV_32F), interp_mode, border_mode, dst);183}184185186Point cv::detail::PlaneWarperGpu::warp(const cuda::GpuMat & src, InputArray K, InputArray R, InputArray T,187int interp_mode, int border_mode,188cuda::GpuMat & dst)189{190#ifndef HAVE_OPENCV_CUDAWARPING191CV_UNUSED(src);192CV_UNUSED(K);193CV_UNUSED(R);194CV_UNUSED(T);195CV_UNUSED(interp_mode);196CV_UNUSED(border_mode);197CV_UNUSED(dst);198throw_no_cuda();199#else200Rect dst_roi = buildMaps(src.size(), K, R, T, d_xmap_, d_ymap_);201dst.create(dst_roi.height + 1, dst_roi.width + 1, src.type());202cuda::remap(src, dst, d_xmap_, d_ymap_, interp_mode, border_mode);203return dst_roi.tl();204#endif205}206207Rect cv::detail::SphericalWarperGpu::buildMaps(Size src_size, InputArray K, InputArray R, cuda::GpuMat & xmap, cuda::GpuMat & ymap)208{209#ifndef HAVE_CUDA210CV_UNUSED(src_size);211CV_UNUSED(K);212CV_UNUSED(R);213CV_UNUSED(xmap);214CV_UNUSED(ymap);215throw_no_cuda();216#else217projector_.setCameraParams(K, R);218219Point dst_tl, dst_br;220detectResultRoi(src_size, dst_tl, dst_br);221222::buildWarpSphericalMaps(src_size, Rect(dst_tl, Point(dst_br.x + 1, dst_br.y + 1)),223K, R, projector_.scale, xmap, ymap);224225return Rect(dst_tl, dst_br);226#endif227}228229Point cv::detail::SphericalWarperGpu::warp(const cuda::GpuMat & src, InputArray K, InputArray R,230int interp_mode, int border_mode,231cuda::GpuMat & dst)232{233#ifndef HAVE_OPENCV_CUDAWARPING234CV_UNUSED(src);235CV_UNUSED(K);236CV_UNUSED(R);237CV_UNUSED(interp_mode);238CV_UNUSED(border_mode);239CV_UNUSED(dst);240throw_no_cuda();241#else242Rect dst_roi = buildMaps(src.size(), K, R, d_xmap_, d_ymap_);243dst.create(dst_roi.height + 1, dst_roi.width + 1, src.type());244cuda::remap(src, dst, d_xmap_, d_ymap_, interp_mode, border_mode);245return dst_roi.tl();246#endif247}248249250Rect cv::detail::CylindricalWarperGpu::buildMaps(Size src_size, InputArray K, InputArray R,251cuda::GpuMat & xmap, cuda::GpuMat & ymap)252{253#ifndef HAVE_CUDA254CV_UNUSED(src_size);255CV_UNUSED(K);256CV_UNUSED(R);257CV_UNUSED(xmap);258CV_UNUSED(ymap);259throw_no_cuda();260#else261projector_.setCameraParams(K, R);262263Point dst_tl, dst_br;264detectResultRoi(src_size, dst_tl, dst_br);265266::buildWarpCylindricalMaps(src_size, Rect(dst_tl, Point(dst_br.x + 1, dst_br.y + 1)),267K, R, projector_.scale, xmap, ymap);268269return Rect(dst_tl, dst_br);270#endif271}272273Point cv::detail::CylindricalWarperGpu::warp(const cuda::GpuMat & src, InputArray K, InputArray R,274int interp_mode, int border_mode,275cuda::GpuMat & dst)276{277#ifndef HAVE_OPENCV_CUDAWARPING278CV_UNUSED(src);279CV_UNUSED(K);280CV_UNUSED(R);281CV_UNUSED(interp_mode);282CV_UNUSED(border_mode);283CV_UNUSED(dst);284throw_no_cuda();285#else286Rect dst_roi = buildMaps(src.size(), K, R, d_xmap_, d_ymap_);287dst.create(dst_roi.height + 1, dst_roi.width + 1, src.type());288cuda::remap(src, dst, d_xmap_, d_ymap_, interp_mode, border_mode);289return dst_roi.tl();290#endif291}292293294