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) 2010-2012, Multicoreware, Inc., all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
18 // Peng Xiao, pengxiao@multicorewareinc.com
20 // Redistribution and use in source and binary forms, with or without modification,
21 // are permitted provided that the following conditions are met:
23 // * Redistribution's of source code must retain the above copyright notice,
24 // this list of conditions and the following disclaimer.
26 // * Redistribution's in binary form must reproduce the above copyright notice,
27 // this list of conditions and the following disclaimer in the documentation
28 // and/or other materials provided with the distribution.
30 // * The name of the copyright holders may not be used to endorse or promote products
31 // derived from this software without specific prior written permission.
33 // This software is provided by the copyright holders and contributors as is and
34 // any express or implied warranties, including, but not limited to, the implied
35 // warranties of merchantability and fitness for a particular purpose are disclaimed.
36 // In no event shall the Intel Corporation or contributors be liable for any direct,
37 // indirect, incidental, special, exemplary, or consequential damages
38 // (including, but not limited to, procurement of substitute goods or services;
39 // loss of use, data, or profits; or business interruption) however caused
40 // and on any theory of liability, whether in contract, strict liability,
41 // or tort (including negligence or otherwise) arising in any way out of
42 // the use of this software, even if advised of the possibility of such damage.
46 #include "precomp.hpp"
47 #include "opencl_kernels.hpp"
50 using namespace cv::ocl;
52 //////////////////////////////////////////////////////////////////////////////
55 void cv::ocl::buildWarpPlaneMaps(Size /*src_size*/, Rect dst_roi, const Mat &K, const Mat &R, const Mat &T,
56 float scale, oclMat &map_x, oclMat &map_y)
58 CV_Assert(K.size() == Size(3, 3) && K.type() == CV_32F);
59 CV_Assert(R.size() == Size(3, 3) && R.type() == CV_32F);
60 CV_Assert((T.size() == Size(3, 1) || T.size() == Size(1, 3)) && T.type() == CV_32F && T.isContinuous());
62 Mat K_Rinv = K * R.t();
63 CV_Assert(K_Rinv.isContinuous());
65 Mat KRT_mat(1, 12, CV_32FC1); // 9 + 3
66 KRT_mat(Range::all(), Range(0, 8)) = K_Rinv.reshape(1, 1);
67 KRT_mat(Range::all(), Range(9, 11)) = T;
69 oclMat KRT_oclMat(KRT_mat);
70 // transfer K_Rinv and T into a single cl_mem
71 map_x.create(dst_roi.size(), CV_32F);
72 map_y.create(dst_roi.size(), CV_32F);
74 int tl_u = dst_roi.tl().x;
75 int tl_v = dst_roi.tl().y;
77 Context *clCxt = Context::getContext();
78 string kernelName = "buildWarpPlaneMaps";
79 vector< pair<size_t, const void *> > args;
81 args.push_back( make_pair( sizeof(cl_mem), (void *)&map_x.data));
82 args.push_back( make_pair( sizeof(cl_mem), (void *)&map_y.data));
83 args.push_back( make_pair( sizeof(cl_mem), (void *)&KRT_mat.data));
84 args.push_back( make_pair( sizeof(cl_int), (void *)&tl_u));
85 args.push_back( make_pair( sizeof(cl_int), (void *)&tl_v));
86 args.push_back( make_pair( sizeof(cl_int), (void *)&map_x.cols));
87 args.push_back( make_pair( sizeof(cl_int), (void *)&map_x.rows));
88 args.push_back( make_pair( sizeof(cl_int), (void *)&map_x.step));
89 args.push_back( make_pair( sizeof(cl_int), (void *)&map_y.step));
90 args.push_back( make_pair( sizeof(cl_float), (void *)&scale));
92 size_t globalThreads[3] = {map_x.cols, map_x.rows, 1};
93 size_t localThreads[3] = {32, 8, 1};
94 openCLExecuteKernel(clCxt, &build_warps, kernelName, globalThreads, localThreads, args, -1, -1);
97 //////////////////////////////////////////////////////////////////////////////
98 // buildWarpCylyndricalMaps
100 void cv::ocl::buildWarpCylindricalMaps(Size /*src_size*/, Rect dst_roi, const Mat &K, const Mat &R, float scale,
101 oclMat &map_x, oclMat &map_y)
103 CV_Assert(K.size() == Size(3, 3) && K.type() == CV_32F);
104 CV_Assert(R.size() == Size(3, 3) && R.type() == CV_32F);
106 Mat K_Rinv = K * R.t();
107 CV_Assert(K_Rinv.isContinuous());
109 oclMat KR_oclMat(K_Rinv.reshape(1, 1));
111 map_x.create(dst_roi.size(), CV_32F);
112 map_y.create(dst_roi.size(), CV_32F);
114 int tl_u = dst_roi.tl().x;
115 int tl_v = dst_roi.tl().y;
117 Context *clCxt = Context::getContext();
118 string kernelName = "buildWarpCylindricalMaps";
119 vector< pair<size_t, const void *> > args;
121 args.push_back( make_pair( sizeof(cl_mem), (void *)&map_x.data));
122 args.push_back( make_pair( sizeof(cl_mem), (void *)&map_y.data));
123 args.push_back( make_pair( sizeof(cl_mem), (void *)&KR_oclMat.data));
124 args.push_back( make_pair( sizeof(cl_int), (void *)&tl_u));
125 args.push_back( make_pair( sizeof(cl_int), (void *)&tl_v));
126 args.push_back( make_pair( sizeof(cl_int), (void *)&map_x.cols));
127 args.push_back( make_pair( sizeof(cl_int), (void *)&map_x.rows));
128 args.push_back( make_pair( sizeof(cl_int), (void *)&map_x.step));
129 args.push_back( make_pair( sizeof(cl_int), (void *)&map_y.step));
130 args.push_back( make_pair( sizeof(cl_float), (void *)&scale));
132 size_t globalThreads[3] = {map_x.cols, map_x.rows, 1};
133 size_t localThreads[3] = {32, 8, 1};
134 openCLExecuteKernel(clCxt, &build_warps, kernelName, globalThreads, localThreads, args, -1, -1);
137 //////////////////////////////////////////////////////////////////////////////
138 // buildWarpSphericalMaps
139 void cv::ocl::buildWarpSphericalMaps(Size /*src_size*/, Rect dst_roi, const Mat &K, const Mat &R, float scale,
140 oclMat &map_x, oclMat &map_y)
142 CV_Assert(K.size() == Size(3, 3) && K.type() == CV_32F);
143 CV_Assert(R.size() == Size(3, 3) && R.type() == CV_32F);
145 Mat K_Rinv = K * R.t();
146 CV_Assert(K_Rinv.isContinuous());
148 oclMat KR_oclMat(K_Rinv.reshape(1, 1));
149 // transfer K_Rinv, R_Kinv into a single cl_mem
150 map_x.create(dst_roi.size(), CV_32F);
151 map_y.create(dst_roi.size(), CV_32F);
153 int tl_u = dst_roi.tl().x;
154 int tl_v = dst_roi.tl().y;
156 Context *clCxt = Context::getContext();
157 string kernelName = "buildWarpSphericalMaps";
158 vector< pair<size_t, const void *> > args;
160 args.push_back( make_pair( sizeof(cl_mem), (void *)&map_x.data));
161 args.push_back( make_pair( sizeof(cl_mem), (void *)&map_y.data));
162 args.push_back( make_pair( sizeof(cl_mem), (void *)&KR_oclMat.data));
163 args.push_back( make_pair( sizeof(cl_int), (void *)&tl_u));
164 args.push_back( make_pair( sizeof(cl_int), (void *)&tl_v));
165 args.push_back( make_pair( sizeof(cl_int), (void *)&map_x.cols));
166 args.push_back( make_pair( sizeof(cl_int), (void *)&map_x.rows));
167 args.push_back( make_pair( sizeof(cl_int), (void *)&map_x.step));
168 args.push_back( make_pair( sizeof(cl_int), (void *)&map_y.step));
169 args.push_back( make_pair( sizeof(cl_float), (void *)&scale));
171 size_t globalThreads[3] = {map_x.cols, map_x.rows, 1};
172 size_t localThreads[3] = {32, 8, 1};
173 openCLExecuteKernel(clCxt, &build_warps, kernelName, globalThreads, localThreads, args, -1, -1);
177 void cv::ocl::buildWarpAffineMaps(const Mat &M, bool inverse, Size dsize, oclMat &xmap, oclMat &ymap)
180 CV_Assert(M.rows == 2 && M.cols == 3);
182 xmap.create(dsize, CV_32FC1);
183 ymap.create(dsize, CV_32FC1);
186 Mat coeffsMat(2, 3, CV_32F, (void *)coeffs);
189 M.convertTo(coeffsMat, coeffsMat.type());
193 invertAffineTransform(M, iM);
194 iM.convertTo(coeffsMat, coeffsMat.type());
197 oclMat coeffsOclMat(coeffsMat.reshape(1, 1));
199 Context *clCxt = Context::getContext();
200 string kernelName = "buildWarpAffineMaps";
201 vector< pair<size_t, const void *> > args;
203 args.push_back( make_pair( sizeof(cl_mem), (void *)&xmap.data));
204 args.push_back( make_pair( sizeof(cl_mem), (void *)&ymap.data));
205 args.push_back( make_pair( sizeof(cl_mem), (void *)&coeffsOclMat.data));
206 args.push_back( make_pair( sizeof(cl_int), (void *)&xmap.cols));
207 args.push_back( make_pair( sizeof(cl_int), (void *)&xmap.rows));
208 args.push_back( make_pair( sizeof(cl_int), (void *)&xmap.step));
209 args.push_back( make_pair( sizeof(cl_int), (void *)&ymap.step));
211 size_t globalThreads[3] = {xmap.cols, xmap.rows, 1};
212 size_t localThreads[3] = {32, 8, 1};
213 openCLExecuteKernel(clCxt, &build_warps, kernelName, globalThreads, localThreads, args, -1, -1);
216 void cv::ocl::buildWarpPerspectiveMaps(const Mat &M, bool inverse, Size dsize, oclMat &xmap, oclMat &ymap)
219 CV_Assert(M.rows == 3 && M.cols == 3);
221 xmap.create(dsize, CV_32FC1);
222 ymap.create(dsize, CV_32FC1);
225 Mat coeffsMat(3, 3, CV_32F, (void *)coeffs);
228 M.convertTo(coeffsMat, coeffsMat.type());
233 iM.convertTo(coeffsMat, coeffsMat.type());
236 oclMat coeffsOclMat(coeffsMat.reshape(1, 1));
238 Context *clCxt = Context::getContext();
239 string kernelName = "buildWarpPerspectiveMaps";
240 vector< pair<size_t, const void *> > args;
242 args.push_back( make_pair( sizeof(cl_mem), (void *)&xmap.data));
243 args.push_back( make_pair( sizeof(cl_mem), (void *)&ymap.data));
244 args.push_back( make_pair( sizeof(cl_mem), (void *)&coeffsOclMat.data));
245 args.push_back( make_pair( sizeof(cl_int), (void *)&xmap.cols));
246 args.push_back( make_pair( sizeof(cl_int), (void *)&xmap.rows));
247 args.push_back( make_pair( sizeof(cl_int), (void *)&xmap.step));
248 args.push_back( make_pair( sizeof(cl_int), (void *)&ymap.step));
250 size_t globalThreads[3] = {xmap.cols, xmap.rows, 1};
251 size_t localThreads[3] = {32, 8, 1};
252 openCLExecuteKernel(clCxt, &build_warps, kernelName, globalThreads, localThreads, args, -1, -1);