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, Institute Of Software Chinese Academy Of Science, all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
16 // Third party copyrights are property of their respective owners.
19 // Niko Li, newlife20080214@gmail.com
20 // Jia Haipeng, jiahaipeng95@gmail.com
21 // Shengen Yan, yanshengen@gmail.com
22 // Rock Li, Rock.Li@amd.com
23 // Zero Lin, Zero.Lin@amd.com
24 // Zhang Ying, zhangying913@gmail.com
25 // Xu Pang, pangxu010@163.com
26 // Wu Zailong, bullet@yeah.net
27 // Wenju He, wenju@multicorewareinc.com
28 // Peng Xiao, pengxiao@outlook.com
29 // Sen Liu, swjtuls1987@126.com
31 // Redistribution and use in source and binary forms, with or without modification,
32 // are permitted provided that the following conditions are met:
34 // * Redistribution's of source code must retain the above copyright notice,
35 // this list of conditions and the following disclaimer.
37 // * Redistribution's in binary form must reproduce the above copyright notice,
38 // this list of conditions and the following disclaimer in the documentation
39 // and/or other materials provided with the distribution.
41 // * The name of the copyright holders may not be used to endorse or promote products
42 // derived from this software without specific prior written permission.
44 // This software is provided by the copyright holders and contributors "as is" and
45 // any express or implied warranties, including, but not limited to, the implied
46 // warranties of merchantability and fitness for a particular purpose are disclaimed.
47 // In no event shall the Intel Corporation or contributors be liable for any direct,
48 // indirect, incidental, special, exemplary, or consequential damages
49 // (including, but not limited to, procurement of substitute goods or services;
50 // loss of use, data, or profits; or business interruption) however caused
51 // and on any theory of liability, whether in contract, strict liability,
52 // or tort (including negligence or otherwise) arising in any way out of
53 // the use of this software, even if advised of the possibility of such damage.
57 #include "precomp.hpp"
58 #include "opencl_kernels.hpp"
61 using namespace cv::ocl;
67 ////////////////////////////////////OpenCL call wrappers////////////////////////////
69 template <typename T> struct index_and_sizeof;
70 template <> struct index_and_sizeof<char>
74 template <> struct index_and_sizeof<unsigned char>
78 template <> struct index_and_sizeof<short>
82 template <> struct index_and_sizeof<unsigned short>
86 template <> struct index_and_sizeof<int>
90 template <> struct index_and_sizeof<float>
94 template <> struct index_and_sizeof<double>
99 /////////////////////////////////////////////////////////////////////////////////////
102 static std::vector<uchar> scalarToVector(const cv::Scalar & sc, int depth, int ocn, int cn)
104 CV_Assert(ocn == cn || (ocn == 4 && cn == 3));
106 static const int sizeMap[] = { sizeof(uchar), sizeof(char), sizeof(ushort),
107 sizeof(short), sizeof(int), sizeof(float), sizeof(double) };
109 int elemSize1 = sizeMap[depth];
110 int bufSize = elemSize1 * ocn;
111 std::vector<uchar> _buf(bufSize);
112 uchar * buf = &_buf[0];
113 scalarToRawData(sc, buf, CV_MAKE_TYPE(depth, cn));
114 memset(buf + elemSize1 * cn, 0, (ocn - cn) * elemSize1);
119 static void threshold_runner(const oclMat &src, oclMat &dst, double thresh, double maxVal, int thresholdType)
121 bool ival = src.depth() < CV_32F;
122 int cn = src.channels(), vecSize = 4, depth = src.depth();
123 std::vector<uchar> thresholdValue = scalarToVector(cv::Scalar::all(ival ? cvFloor(thresh) : thresh), dst.depth(),
124 dst.oclchannels(), dst.channels());
125 std::vector<uchar> maxValue = scalarToVector(cv::Scalar::all(maxVal), dst.depth(), dst.oclchannels(), dst.channels());
127 const char * const thresholdMap[] = { "THRESH_BINARY", "THRESH_BINARY_INV", "THRESH_TRUNC",
128 "THRESH_TOZERO", "THRESH_TOZERO_INV" };
129 const char * const channelMap[] = { "", "", "2", "4", "4" };
130 const char * const typeMap[] = { "uchar", "char", "ushort", "short", "int", "float", "double" };
131 std::string buildOptions = format("-D T=%s%s -D %s", typeMap[depth], channelMap[cn], thresholdMap[thresholdType]);
133 int elemSize = src.elemSize();
134 int src_step = src.step / elemSize, src_offset = src.offset / elemSize;
135 int dst_step = dst.step / elemSize, dst_offset = dst.offset / elemSize;
137 std::vector< std::pair<size_t, const void *> > args;
138 args.push_back( std::make_pair(sizeof(cl_mem), (void *)&src.data));
139 args.push_back( std::make_pair(sizeof(cl_int), (void *)&src_offset));
140 args.push_back( std::make_pair(sizeof(cl_int), (void *)&src_step));
141 args.push_back( std::make_pair(sizeof(cl_mem), (void *)&dst.data));
142 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst_offset));
143 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst_step));
144 args.push_back( std::make_pair(thresholdValue.size(), (void *)&thresholdValue[0]));
145 args.push_back( std::make_pair(maxValue.size(), (void *)&maxValue[0]));
147 int max_index = dst.cols, cols = dst.cols;
148 if (cn == 1 && vecSize > 1)
150 CV_Assert(((vecSize - 1) & vecSize) == 0 && vecSize <= 16);
151 cols = divUp(cols, vecSize);
152 buildOptions += format(" -D VECTORIZED -D VT=%s%d -D VLOADN=vload%d -D VECSIZE=%d -D VSTOREN=vstore%d",
153 typeMap[depth], vecSize, vecSize, vecSize, vecSize);
155 int vecSizeBytes = vecSize * dst.elemSize1();
156 if ((dst.offset % dst.step) % vecSizeBytes == 0 && dst.step % vecSizeBytes == 0)
157 buildOptions += " -D DST_ALIGNED";
158 if ((src.offset % src.step) % vecSizeBytes == 0 && src.step % vecSizeBytes == 0)
159 buildOptions += " -D SRC_ALIGNED";
161 args.push_back( std::make_pair(sizeof(cl_int), (void *)&max_index));
164 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.rows));
165 args.push_back( std::make_pair(sizeof(cl_int), (void *)&cols));
167 size_t localThreads[3] = { 16, 16, 1 };
168 size_t globalThreads[3] = { cols, dst.rows, 1 };
170 openCLExecuteKernel(src.clCxt, &imgproc_threshold, "threshold", globalThreads, localThreads, args,
171 -1, -1, buildOptions.c_str());
174 double threshold(const oclMat &src, oclMat &dst, double thresh, double maxVal, int thresholdType)
176 CV_Assert(thresholdType == THRESH_BINARY || thresholdType == THRESH_BINARY_INV || thresholdType == THRESH_TRUNC
177 || thresholdType == THRESH_TOZERO || thresholdType == THRESH_TOZERO_INV);
179 dst.create(src.size(), src.type());
180 threshold_runner(src, dst, thresh, maxVal, thresholdType);
185 ////////////////////////////////////////////////////////////////////////////////////////////
186 /////////////////////////////// remap //////////////////////////////////////////////////
187 ////////////////////////////////////////////////////////////////////////////////////////////
189 void remap( const oclMat &src, oclMat &dst, oclMat &map1, oclMat &map2, int interpolation, int borderType, const Scalar &borderValue )
191 Context *clCxt = src.clCxt;
192 bool supportsDouble = clCxt->supportsFeature(FEATURE_CL_DOUBLE);
193 if (!supportsDouble && src.depth() == CV_64F)
195 CV_Error(CV_OpenCLDoubleNotSupported, "Selected device does not support double");
202 CV_Assert(interpolation == INTER_LINEAR || interpolation == INTER_NEAREST);
203 CV_Assert((map1.type() == CV_16SC2 && (map2.empty() || (map2.type() == CV_16UC1 || map2.type() == CV_16SC1)) ) ||
204 (map1.type() == CV_32FC2 && !map2.data) ||
205 (map1.type() == CV_32FC1 && map2.type() == CV_32FC1));
206 CV_Assert(!map2.data || map2.size() == map1.size());
207 CV_Assert(borderType == BORDER_CONSTANT || borderType == BORDER_REPLICATE || borderType == BORDER_WRAP
208 || borderType == BORDER_REFLECT_101 || borderType == BORDER_REFLECT);
210 dst.create(map1.size(), src.type());
212 const char * const typeMap[] = { "uchar", "char", "ushort", "short", "int", "float", "double" };
213 const char * const channelMap[] = { "", "", "2", "4", "4" };
214 const char * const interMap[] = { "INTER_NEAREST", "INTER_LINEAR", "INTER_CUBIC", "INTER_LINEAR", "INTER_LANCZOS" };
215 const char * const borderMap[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", "BORDER_WRAP",
216 "BORDER_REFLECT_101", "BORDER_TRANSPARENT" };
218 String kernelName = "remap";
219 if (map1.type() == CV_32FC2 && map2.empty())
220 kernelName = kernelName + "_32FC2";
221 else if (map1.type() == CV_16SC2)
223 kernelName = kernelName + "_16SC2";
225 kernelName = kernelName + "_16UC1";
227 else if (map1.type() == CV_32FC1 && map2.type() == CV_32FC1)
228 kernelName = kernelName + "_2_32FC1";
230 CV_Error(Error::StsBadArg, "Unsupported map types");
232 int ocn = dst.oclchannels();
233 size_t localThreads[3] = { 256, 1, 1 };
234 size_t globalThreads[3] = { dst.cols, dst.rows, 1 };
236 Mat scalar(1, 1, CV_MAKE_TYPE(dst.depth(), ocn), borderValue);
237 String buildOptions = format("-D %s -D %s -D T=%s%s", interMap[interpolation],
238 borderMap[borderType], typeMap[src.depth()], channelMap[ocn]);
240 if (interpolation != INTER_NEAREST)
242 int wdepth = std::max(CV_32F, dst.depth());
243 buildOptions = buildOptions
244 + format(" -D WT=%s%s -D convertToT=convert_%s%s%s -D convertToWT=convert_%s%s"
245 " -D convertToWT2=convert_%s2 -D WT2=%s2",
246 typeMap[wdepth], channelMap[ocn],
247 typeMap[src.depth()], channelMap[ocn], src.depth() < CV_32F ? "_sat_rte" : "",
248 typeMap[wdepth], channelMap[ocn],
249 typeMap[wdepth], typeMap[wdepth]);
252 int src_step = src.step / src.elemSize(), src_offset = src.offset / src.elemSize();
253 int map1_step = map1.step / map1.elemSize(), map1_offset = map1.offset / map1.elemSize();
254 int map2_step = map2.step / map2.elemSize(), map2_offset = map2.offset / map2.elemSize();
255 int dst_step = dst.step / dst.elemSize(), dst_offset = dst.offset / dst.elemSize();
257 std::vector< std::pair<size_t, const void *> > args;
258 args.push_back( std::make_pair(sizeof(cl_mem), (void *)&src.data));
259 args.push_back( std::make_pair(sizeof(cl_mem), (void *)&dst.data));
260 args.push_back( std::make_pair(sizeof(cl_mem), (void *)&map1.data));
262 args.push_back( std::make_pair(sizeof(cl_mem), (void *)&map2.data));
263 args.push_back( std::make_pair(sizeof(cl_int), (void *)&src_offset));
264 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst_offset));
265 args.push_back( std::make_pair(sizeof(cl_int), (void *)&map1_offset));
267 args.push_back( std::make_pair(sizeof(cl_int), (void *)&map2_offset));
268 args.push_back( std::make_pair(sizeof(cl_int), (void *)&src_step));
269 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst_step));
270 args.push_back( std::make_pair(sizeof(cl_int), (void *)&map1_step));
272 args.push_back( std::make_pair(sizeof(cl_int), (void *)&map2_step));
273 args.push_back( std::make_pair(sizeof(cl_int), (void *)&src.cols));
274 args.push_back( std::make_pair(sizeof(cl_int), (void *)&src.rows));
275 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.cols));
276 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.rows));
277 args.push_back( std::make_pair(scalar.elemSize(), (void *)scalar.data));
279 openCLExecuteKernel(clCxt, &imgproc_remap, kernelName, globalThreads, localThreads, args, -1, -1, buildOptions.c_str());
282 ////////////////////////////////////////////////////////////////////////////////////////////
285 static void resize_gpu( const oclMat &src, oclMat &dst, double fx, double fy, int interpolation)
287 float ifx = 1.f / fx, ify = 1.f / fy;
288 int src_step = src.step / src.elemSize(), src_offset = src.offset / src.elemSize();
289 int dst_step = dst.step / dst.elemSize(), dst_offset = dst.offset / dst.elemSize();
290 int ocn = interpolation == INTER_LINEAR ? dst.oclchannels() : -1;
291 int depth = interpolation == INTER_LINEAR ? dst.depth() : -1;
293 const char * const interMap[] = { "NN", "LN", "CUBIC", "AREA", "LAN4" };
294 std::string kernelName = std::string("resize") + interMap[interpolation];
296 const char * const typeMap[] = { "uchar", "uchar", "ushort", "ushort", "int", "int", "double" };
297 const char * const channelMap[] = { "" , "", "2", "4", "4" };
298 std::string buildOption = format("-D %s -D T=%s%s", interMap[interpolation], typeMap[dst.depth()], channelMap[dst.oclchannels()]);
300 //TODO: improve this kernel
301 size_t blkSizeX = 16, blkSizeY = 16;
303 if (src.type() == CV_8UC1 && interpolation == INTER_LINEAR)
305 size_t cols = (dst.cols + dst.offset % 4 + 3) / 4;
306 glbSizeX = cols % blkSizeX == 0 && cols != 0 ? cols : (cols / blkSizeX + 1) * blkSizeX;
311 size_t globalThreads[3] = { glbSizeX, dst.rows, 1 };
312 size_t localThreads[3] = { blkSizeX, blkSizeY, 1 };
314 std::vector< std::pair<size_t, const void *> > args;
315 args.push_back( std::make_pair(sizeof(cl_mem), (void *)&dst.data));
316 args.push_back( std::make_pair(sizeof(cl_mem), (void *)&src.data));
317 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst_offset));
318 args.push_back( std::make_pair(sizeof(cl_int), (void *)&src_offset));
319 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst_step));
320 args.push_back( std::make_pair(sizeof(cl_int), (void *)&src_step));
321 args.push_back( std::make_pair(sizeof(cl_int), (void *)&src.cols));
322 args.push_back( std::make_pair(sizeof(cl_int), (void *)&src.rows));
323 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.cols));
324 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.rows));
325 args.push_back( std::make_pair(sizeof(cl_float), (void *)&ifx));
326 args.push_back( std::make_pair(sizeof(cl_float), (void *)&ify));
328 openCLExecuteKernel(src.clCxt, &imgproc_resize, kernelName, globalThreads, localThreads, args,
329 ocn, depth, buildOption.c_str());
332 void resize(const oclMat &src, oclMat &dst, Size dsize, double fx, double fy, int interpolation)
334 CV_Assert(src.type() == CV_8UC1 || src.type() == CV_8UC3 || src.type() == CV_8UC4
335 || src.type() == CV_32FC1 || src.type() == CV_32FC3 || src.type() == CV_32FC4);
336 CV_Assert(interpolation == INTER_LINEAR || interpolation == INTER_NEAREST);
337 CV_Assert(dsize.area() > 0 || (fx > 0 && fy > 0));
339 if (dsize.area() == 0)
341 dsize = Size(saturate_cast<int>(src.cols * fx), saturate_cast<int>(src.rows * fy));
342 CV_Assert(dsize.area() > 0);
346 fx = (double)dsize.width / src.cols;
347 fy = (double)dsize.height / src.rows;
350 dst.create(dsize, src.type());
352 resize_gpu( src, dst, fx, fy, interpolation);
355 ////////////////////////////////////////////////////////////////////////
358 void medianFilter(const oclMat &src, oclMat &dst, int m)
360 CV_Assert( m % 2 == 1 && m > 1 );
361 CV_Assert( (src.depth() == CV_8U || src.depth() == CV_32F) && (src.channels() == 1 || src.channels() == 4));
362 dst.create(src.size(), src.type());
364 int srcStep = src.step / src.elemSize(), dstStep = dst.step / dst.elemSize();
365 int srcOffset = src.offset / src.elemSize(), dstOffset = dst.offset / dst.elemSize();
367 Context *clCxt = src.clCxt;
369 std::vector< std::pair<size_t, const void *> > args;
370 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&src.data));
371 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst.data));
372 args.push_back( std::make_pair( sizeof(cl_int), (void *)&srcOffset));
373 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dstOffset));
374 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.cols));
375 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.rows));
376 args.push_back( std::make_pair( sizeof(cl_int), (void *)&srcStep));
377 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dstStep));
379 size_t globalThreads[3] = {(src.cols + 18) / 16 * 16, (src.rows + 15) / 16 * 16, 1};
380 size_t localThreads[3] = {16, 16, 1};
384 String kernelName = "medianFilter3";
385 openCLExecuteKernel(clCxt, &imgproc_median, kernelName, globalThreads, localThreads, args, src.oclchannels(), src.depth());
389 String kernelName = "medianFilter5";
390 openCLExecuteKernel(clCxt, &imgproc_median, kernelName, globalThreads, localThreads, args, src.oclchannels(), src.depth());
393 CV_Error(Error::StsBadArg, "Non-supported filter length");
396 ////////////////////////////////////////////////////////////////////////
399 void copyMakeBorder(const oclMat &src, oclMat &dst, int top, int bottom, int left, int right, int bordertype, const Scalar &scalar)
401 if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
403 CV_Error(Error::OpenCLDoubleNotSupported, "Selected device does not support double");
409 CV_Assert(top >= 0 && bottom >= 0 && left >= 0 && right >= 0);
411 if( (_src.wholecols != _src.cols || _src.wholerows != _src.rows) && (bordertype & BORDER_ISOLATED) == 0 )
415 _src.locateROI(wholeSize, ofs);
416 int dtop = std::min(ofs.y, top);
417 int dbottom = std::min(wholeSize.height - _src.rows - ofs.y, bottom);
418 int dleft = std::min(ofs.x, left);
419 int dright = std::min(wholeSize.width - _src.cols - ofs.x, right);
420 _src.adjustROI(dtop, dbottom, dleft, dright);
426 bordertype &= ~cv::BORDER_ISOLATED;
428 dst.create(_src.rows + top + bottom, _src.cols + left + right, _src.type());
429 int srcStep = _src.step / _src.elemSize(), dstStep = dst.step / dst.elemSize();
430 int srcOffset = _src.offset / _src.elemSize(), dstOffset = dst.offset / dst.elemSize();
431 int depth = _src.depth(), ochannels = _src.oclchannels();
433 int __bordertype[] = { BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT, BORDER_WRAP, BORDER_REFLECT_101 };
434 const char *borderstr[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", "BORDER_WRAP", "BORDER_REFLECT_101" };
436 int bordertype_index = -1;
437 for (int i = 0, end = sizeof(__bordertype) / sizeof(int); i < end; i++)
438 if (__bordertype[i] == bordertype)
440 bordertype_index = i;
443 if (bordertype_index < 0)
444 CV_Error(Error::StsBadArg, "Unsupported border type");
446 size_t localThreads[3] = { 16, 16, 1 };
447 size_t globalThreads[3] = { dst.cols, dst.rows, 1 };
449 std::vector< std::pair<size_t, const void *> > args;
450 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&_src.data));
451 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst.data));
452 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.cols));
453 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.rows));
454 args.push_back( std::make_pair( sizeof(cl_int), (void *)&_src.cols));
455 args.push_back( std::make_pair( sizeof(cl_int), (void *)&_src.rows));
456 args.push_back( std::make_pair( sizeof(cl_int), (void *)&srcStep));
457 args.push_back( std::make_pair( sizeof(cl_int), (void *)&srcOffset));
458 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dstStep));
459 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dstOffset));
460 args.push_back( std::make_pair( sizeof(cl_int), (void *)&top));
461 args.push_back( std::make_pair( sizeof(cl_int), (void *)&left));
463 const char * const typeMap[] = { "uchar", "char", "ushort", "short", "int", "float", "double" };
464 const char * const channelMap[] = { "", "", "2", "4", "4" };
465 std::string buildOptions = format("-D GENTYPE=%s%s -D %s",
466 typeMap[depth], channelMap[ochannels],
467 borderstr[bordertype_index]);
469 int cn = src.channels(), ocn = src.oclchannels();
470 int bufSize = src.elemSize1() * ocn;
471 AutoBuffer<uchar> _buf(bufSize);
472 uchar * buf = (uchar *)_buf;
473 scalarToRawData(scalar, buf, dst.type());
474 memset(buf + src.elemSize1() * cn, 0, (ocn - cn) * src.elemSize1());
476 args.push_back( std::make_pair( bufSize , (void *)buf ));
478 openCLExecuteKernel(src.clCxt, &imgproc_copymakeboder, "copymakeborder", globalThreads,
479 localThreads, args, -1, -1, buildOptions.c_str());
482 ////////////////////////////////////////////////////////////////////////
489 void convert_coeffs(F *M)
491 double D = M[0] * M[4] - M[1] * M[3];
492 D = D != 0 ? 1. / D : 0;
493 double A11 = M[4] * D, A22 = M[0] * D;
498 double b1 = -M[0] * M[2] - M[1] * M[5];
499 double b2 = -M[3] * M[2] - M[4] * M[5];
504 double invert(double *M)
506 #define Sd(y,x) (Sd[y*3+x])
507 #define Dd(y,x) (Dd[y*3+x])
508 #define det3(m) (m(0,0)*(m(1,1)*m(2,2) - m(1,2)*m(2,1)) - \
509 m(0,1)*(m(1,0)*m(2,2) - m(1,2)*m(2,0)) + \
510 m(0,2)*(m(1,0)*m(2,1) - m(1,1)*m(2,0)))
521 t[0] = (Sd(1, 1) * Sd(2, 2) - Sd(1, 2) * Sd(2, 1)) * d;
522 t[1] = (Sd(0, 2) * Sd(2, 1) - Sd(0, 1) * Sd(2, 2)) * d;
523 t[2] = (Sd(0, 1) * Sd(1, 2) - Sd(0, 2) * Sd(1, 1)) * d;
525 t[3] = (Sd(1, 2) * Sd(2, 0) - Sd(1, 0) * Sd(2, 2)) * d;
526 t[4] = (Sd(0, 0) * Sd(2, 2) - Sd(0, 2) * Sd(2, 0)) * d;
527 t[5] = (Sd(0, 2) * Sd(1, 0) - Sd(0, 0) * Sd(1, 2)) * d;
529 t[6] = (Sd(1, 0) * Sd(2, 1) - Sd(1, 1) * Sd(2, 0)) * d;
530 t[7] = (Sd(0, 1) * Sd(2, 0) - Sd(0, 0) * Sd(2, 1)) * d;
531 t[8] = (Sd(0, 0) * Sd(1, 1) - Sd(0, 1) * Sd(1, 0)) * d;
546 void warpAffine_gpu(const oclMat &src, oclMat &dst, F coeffs[2][3], int interpolation)
548 CV_Assert( (src.oclchannels() == dst.oclchannels()) );
549 int srcStep = src.step1();
550 int dstStep = dst.step1();
551 float float_coeffs[2][3];
554 Context *clCxt = src.clCxt;
555 String s[3] = {"NN", "Linear", "Cubic"};
556 String kernelName = "warpAffine" + s[interpolation];
558 if (src.clCxt->supportsFeature(FEATURE_CL_DOUBLE))
561 coeffs_cm = clCreateBuffer(*(cl_context*)clCxt->getOpenCLContextPtr(), CL_MEM_READ_WRITE, sizeof(F) * 2 * 3, NULL, &st );
562 openCLVerifyCall(st);
563 openCLSafeCall(clEnqueueWriteBuffer(*(cl_command_queue*)clCxt->getOpenCLCommandQueuePtr(), (cl_mem)coeffs_cm, 1, 0,
564 sizeof(F) * 2 * 3, coeffs, 0, 0, 0));
569 for(int m = 0; m < 2; m++)
570 for(int n = 0; n < 3; n++)
571 float_coeffs[m][n] = coeffs[m][n];
573 coeffs_cm = clCreateBuffer(*(cl_context*)clCxt->getOpenCLContextPtr(), CL_MEM_READ_WRITE, sizeof(float) * 2 * 3, NULL, &st );
574 openCLSafeCall(clEnqueueWriteBuffer(*(cl_command_queue*)clCxt->getOpenCLCommandQueuePtr(), (cl_mem)coeffs_cm,
575 1, 0, sizeof(float) * 2 * 3, float_coeffs, 0, 0, 0));
578 //TODO: improve this kernel
579 size_t blkSizeX = 16, blkSizeY = 16;
583 if (src.type() == CV_8UC1 && interpolation != 2)
585 cols = (dst.cols + dst.offset % 4 + 3) / 4;
586 glbSizeX = cols % blkSizeX == 0 ? cols : (cols / blkSizeX + 1) * blkSizeX;
591 glbSizeX = dst.cols % blkSizeX == 0 ? dst.cols : (dst.cols / blkSizeX + 1) * blkSizeX;
594 size_t glbSizeY = dst.rows % blkSizeY == 0 ? dst.rows : (dst.rows / blkSizeY + 1) * blkSizeY;
595 size_t globalThreads[3] = {glbSizeX, glbSizeY, 1};
596 size_t localThreads[3] = {blkSizeX, blkSizeY, 1};
598 std::vector< std::pair<size_t, const void *> > args;
600 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&src.data));
601 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&dst.data));
602 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.cols));
603 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.rows));
604 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.cols));
605 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.rows));
606 args.push_back(std::make_pair(sizeof(cl_int), (void *)&srcStep));
607 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dstStep));
608 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.offset));
609 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.offset));
610 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&coeffs_cm));
611 args.push_back(std::make_pair(sizeof(cl_int), (void *)&cols));
613 openCLExecuteKernel(clCxt, &imgproc_warpAffine, kernelName, globalThreads, localThreads, args, src.oclchannels(), src.depth());
614 openCLSafeCall(clReleaseMemObject(coeffs_cm));
617 void warpPerspective_gpu(const oclMat &src, oclMat &dst, double coeffs[3][3], int interpolation)
619 CV_Assert( (src.oclchannels() == dst.oclchannels()) );
620 int srcStep = src.step1();
621 int dstStep = dst.step1();
622 float float_coeffs[3][3];
625 Context *clCxt = src.clCxt;
626 String s[3] = {"NN", "Linear", "Cubic"};
627 String kernelName = "warpPerspective" + s[interpolation];
629 if (src.clCxt->supportsFeature(FEATURE_CL_DOUBLE))
632 coeffs_cm = clCreateBuffer(*(cl_context*)clCxt->getOpenCLContextPtr(), CL_MEM_READ_WRITE, sizeof(double) * 3 * 3, NULL, &st );
633 openCLVerifyCall(st);
634 openCLSafeCall(clEnqueueWriteBuffer(*(cl_command_queue*)clCxt->getOpenCLCommandQueuePtr(), (cl_mem)coeffs_cm, 1, 0,
635 sizeof(double) * 3 * 3, coeffs, 0, 0, 0));
640 for(int m = 0; m < 3; m++)
641 for(int n = 0; n < 3; n++)
642 float_coeffs[m][n] = coeffs[m][n];
644 coeffs_cm = clCreateBuffer(*(cl_context*)clCxt->getOpenCLContextPtr(), CL_MEM_READ_WRITE, sizeof(float) * 3 * 3, NULL, &st );
645 openCLVerifyCall(st);
646 openCLSafeCall(clEnqueueWriteBuffer(*(cl_command_queue*)clCxt->getOpenCLCommandQueuePtr(), (cl_mem)coeffs_cm, 1, 0,
647 sizeof(float) * 3 * 3, float_coeffs, 0, 0, 0));
650 //TODO: improve this kernel
651 size_t blkSizeX = 16, blkSizeY = 16;
654 if (src.type() == CV_8UC1 && interpolation == 0)
656 cols = (dst.cols + dst.offset % 4 + 3) / 4;
657 glbSizeX = cols % blkSizeX == 0 ? cols : (cols / blkSizeX + 1) * blkSizeX;
662 glbSizeX = dst.cols % blkSizeX == 0 ? dst.cols : (dst.cols / blkSizeX + 1) * blkSizeX;
665 size_t glbSizeY = dst.rows % blkSizeY == 0 ? dst.rows : (dst.rows / blkSizeY + 1) * blkSizeY;
666 size_t globalThreads[3] = {glbSizeX, glbSizeY, 1};
667 size_t localThreads[3] = {blkSizeX, blkSizeY, 1};
669 std::vector< std::pair<size_t, const void *> > args;
671 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&src.data));
672 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&dst.data));
673 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.cols));
674 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.rows));
675 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.cols));
676 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.rows));
677 args.push_back(std::make_pair(sizeof(cl_int), (void *)&srcStep));
678 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dstStep));
679 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.offset));
680 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.offset));
681 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&coeffs_cm));
682 args.push_back(std::make_pair(sizeof(cl_int), (void *)&cols));
684 openCLExecuteKernel(clCxt, &imgproc_warpPerspective, kernelName, globalThreads, localThreads, args, src.oclchannels(), src.depth());
685 openCLSafeCall(clReleaseMemObject(coeffs_cm));
689 void warpAffine(const oclMat &src, oclMat &dst, const Mat &M, Size dsize, int flags)
691 int interpolation = flags & INTER_MAX;
693 CV_Assert((src.depth() == CV_8U || src.depth() == CV_32F) && src.oclchannels() != 2 && src.oclchannels() != 3);
694 CV_Assert(interpolation == INTER_NEAREST || interpolation == INTER_LINEAR || interpolation == INTER_CUBIC);
696 dst.create(dsize, src.type());
698 CV_Assert(M.rows == 2 && M.cols == 3);
700 int warpInd = (flags & WARP_INVERSE_MAP) >> 4;
704 Mat coeffsMat(2, 3, CV_64F, (void *)coeffsM);
705 M.convertTo(coeffsMat, coeffsMat.type());
707 convert_coeffs(coeffsM);
709 for(int i = 0; i < 2; ++i)
710 for(int j = 0; j < 3; ++j)
711 coeffs[i][j] = coeffsM[i*3+j];
713 warpAffine_gpu(src, dst, coeffs, interpolation);
716 void warpPerspective(const oclMat &src, oclMat &dst, const Mat &M, Size dsize, int flags)
718 int interpolation = flags & INTER_MAX;
720 CV_Assert((src.depth() == CV_8U || src.depth() == CV_32F) && src.oclchannels() != 2 && src.oclchannels() != 3);
721 CV_Assert(interpolation == INTER_NEAREST || interpolation == INTER_LINEAR || interpolation == INTER_CUBIC);
723 dst.create(dsize, src.type());
726 CV_Assert(M.rows == 3 && M.cols == 3);
728 int warpInd = (flags & WARP_INVERSE_MAP) >> 4;
732 Mat coeffsMat(3, 3, CV_64F, (void *)coeffsM);
733 M.convertTo(coeffsMat, coeffsMat.type());
737 for(int i = 0; i < 3; ++i)
738 for(int j = 0; j < 3; ++j)
739 coeffs[i][j] = coeffsM[i*3+j];
741 warpPerspective_gpu(src, dst, coeffs, interpolation);
744 ////////////////////////////////////////////////////////////////////////
747 void integral(const oclMat &src, oclMat &sum, oclMat &sqsum, int sdepth)
749 CV_Assert(src.type() == CV_8UC1);
750 if (!src.clCxt->supportsFeature(ocl::FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
752 CV_Error(Error::OpenCLDoubleNotSupported, "Select device doesn't support double");
758 sdepth = CV_MAT_DEPTH(sdepth);
759 int type = CV_MAKE_TYPE(sdepth, 1);
762 int offset = src.offset / vlen;
763 int pre_invalid = src.offset % vlen;
764 int vcols = (pre_invalid + src.cols + vlen - 1) / vlen;
766 oclMat t_sum , t_sqsum;
767 int w = src.cols + 1, h = src.rows + 1;
769 char build_option[250];
770 if(Context::getContext()->supportsFeature(ocl::FEATURE_CL_DOUBLE))
772 t_sqsum.create(src.cols, src.rows, CV_64FC1);
773 sqsum.create(h, w, CV_64FC1);
774 sprintf(build_option, "-D TYPE=double -D TYPE4=double4 -D convert_TYPE4=convert_double4");
778 t_sqsum.create(src.cols, src.rows, CV_32FC1);
779 sqsum.create(h, w, CV_32FC1);
780 sprintf(build_option, "-D TYPE=float -D TYPE4=float4 -D convert_TYPE4=convert_float4");
783 t_sum.create(src.cols, src.rows, type);
784 sum.create(h, w, type);
786 int sum_offset = sum.offset / sum.elemSize();
787 int sqsum_offset = sqsum.offset / sqsum.elemSize();
789 std::vector<std::pair<size_t , const void *> > args;
790 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&src.data ));
791 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&t_sum.data ));
792 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&t_sqsum.data ));
793 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&offset ));
794 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&pre_invalid ));
795 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.rows ));
796 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.cols ));
797 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.step ));
798 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.step));
799 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sqsum.step));
800 size_t gt[3] = {((vcols + 1) / 2) * 256, 1, 1}, lt[3] = {256, 1, 1};
801 openCLExecuteKernel(src.clCxt, &imgproc_integral, "integral_cols", gt, lt, args, -1, sdepth, build_option);
804 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&t_sum.data ));
805 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&t_sqsum.data ));
806 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&sum.data ));
807 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&sqsum.data ));
808 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.rows ));
809 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.cols ));
810 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.step ));
811 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sqsum.step));
812 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sum.step));
813 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sqsum.step));
814 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sum_offset));
815 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sqsum_offset));
816 size_t gt2[3] = {t_sum.cols * 32, 1, 1}, lt2[3] = {256, 1, 1};
817 openCLExecuteKernel(src.clCxt, &imgproc_integral, "integral_rows", gt2, lt2, args, -1, sdepth, build_option);
820 void integral(const oclMat &src, oclMat &sum, int sdepth)
822 CV_Assert(src.type() == CV_8UC1);
824 int offset = src.offset / vlen;
825 int pre_invalid = src.offset % vlen;
826 int vcols = (pre_invalid + src.cols + vlen - 1) / vlen;
830 sdepth = CV_MAT_DEPTH(sdepth);
831 int type = CV_MAKE_TYPE(sdepth, 1);
834 int w = src.cols + 1, h = src.rows + 1;
836 t_sum.create(src.cols, src.rows, type);
837 sum.create(h, w, type);
839 int sum_offset = sum.offset / vlen;
840 std::vector<std::pair<size_t , const void *> > args;
841 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&src.data ));
842 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&t_sum.data ));
843 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&offset ));
844 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&pre_invalid ));
845 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.rows ));
846 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.cols ));
847 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.step ));
848 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.step));
849 size_t gt[3] = {((vcols + 1) / 2) * 256, 1, 1}, lt[3] = {256, 1, 1};
850 openCLExecuteKernel(src.clCxt, &imgproc_integral_sum, "integral_sum_cols", gt, lt, args, -1, sdepth);
853 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&t_sum.data ));
854 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&sum.data ));
855 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.rows ));
856 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.cols ));
857 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.step ));
858 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sum.step));
859 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sum_offset));
860 size_t gt2[3] = {t_sum.cols * 32, 1, 1}, lt2[3] = {256, 1, 1};
861 openCLExecuteKernel(src.clCxt, &imgproc_integral_sum, "integral_sum_rows", gt2, lt2, args, -1, sdepth);
864 /////////////////////// corner //////////////////////////////
866 static void extractCovData(const oclMat &src, oclMat &Dx, oclMat &Dy,
867 int blockSize, int ksize, int borderType)
869 CV_Assert(src.type() == CV_8UC1 || src.type() == CV_32FC1);
870 double scale = static_cast<double>(1 << ((ksize > 0 ? ksize : 3) - 1)) * blockSize;
874 if (src.depth() == CV_8U)
884 Context* clCxt = Context::getContext();
885 if(clCxt->supportsFeature(FEATURE_CL_INTEL_DEVICE) && src.type() == CV_8UC1 &&
886 src.cols % 8 == 0 && src.rows % 8 == 0 &&
888 (borderType ==cv::BORDER_REFLECT ||
889 borderType == cv::BORDER_REPLICATE ||
890 borderType ==cv::BORDER_REFLECT101 ||
891 borderType ==cv::BORDER_WRAP))
893 Dx.create(src.size(), CV_32FC1);
894 Dy.create(src.size(), CV_32FC1);
896 const unsigned int block_x = 8;
897 const unsigned int block_y = 8;
899 unsigned int src_pitch = src.step;
900 unsigned int dst_pitch = Dx.cols;
902 float _scale = scale;
904 std::vector<std::pair<size_t , const void *> > args;
905 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&src.data ));
906 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&Dx.data ));
907 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&Dy.data ));
908 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.cols ));
909 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.rows ));
910 args.push_back( std::make_pair( sizeof(cl_uint) , (void *)&src_pitch ));
911 args.push_back( std::make_pair( sizeof(cl_uint) , (void *)&dst_pitch ));
912 args.push_back( std::make_pair( sizeof(cl_float) , (void *)&_scale ));
913 size_t gt2[3] = {src.cols, src.rows, 1}, lt2[3] = {block_x, block_y, 1};
915 String option = "-D BLK_X=8 -D BLK_Y=8";
918 case cv::BORDER_REPLICATE:
919 option = option + " -D BORDER_REPLICATE";
921 case cv::BORDER_REFLECT:
922 option = option + " -D BORDER_REFLECT";
924 case cv::BORDER_REFLECT101:
925 option = option + " -D BORDER_REFLECT101";
927 case cv::BORDER_WRAP:
928 option = option + " -D BORDER_WRAP";
931 openCLExecuteKernel(src.clCxt, &imgproc_sobel3, "sobel3", gt2, lt2, args, -1, -1, option.c_str() );
935 Sobel(src, Dx, CV_32F, 1, 0, ksize, scale, 0, borderType);
936 Sobel(src, Dy, CV_32F, 0, 1, ksize, scale, 0, borderType);
941 Scharr(src, Dx, CV_32F, 1, 0, scale, 0, borderType);
942 Scharr(src, Dy, CV_32F, 0, 1, scale, 0, borderType);
944 CV_Assert(Dx.offset == 0 && Dy.offset == 0);
947 static void corner_ocl(const cv::ocl::ProgramEntry* source, String kernelName, int block_size, float k, oclMat &Dx, oclMat &Dy,
948 oclMat &dst, int border_type)
953 case cv::BORDER_CONSTANT:
954 sprintf(borderType, "BORDER_CONSTANT");
956 case cv::BORDER_REFLECT101:
957 sprintf(borderType, "BORDER_REFLECT101");
959 case cv::BORDER_REFLECT:
960 sprintf(borderType, "BORDER_REFLECT");
962 case cv::BORDER_REPLICATE:
963 sprintf(borderType, "BORDER_REPLICATE");
966 CV_Error(Error::StsBadFlag, "BORDER type is not supported!");
969 std::string buildOptions = format("-D anX=%d -D anY=%d -D ksX=%d -D ksY=%d -D %s",
970 block_size / 2, block_size / 2, block_size, block_size, borderType);
972 size_t blockSizeX = 256, blockSizeY = 1;
973 size_t gSize = blockSizeX - block_size / 2 * 2;
974 size_t globalSizeX = (Dx.cols) % gSize == 0 ? Dx.cols / gSize * blockSizeX : (Dx.cols / gSize + 1) * blockSizeX;
975 size_t rows_per_thread = 2;
976 size_t globalSizeY = ((Dx.rows + rows_per_thread - 1) / rows_per_thread) % blockSizeY == 0 ?
977 ((Dx.rows + rows_per_thread - 1) / rows_per_thread) :
978 (((Dx.rows + rows_per_thread - 1) / rows_per_thread) / blockSizeY + 1) * blockSizeY;
980 size_t gt[3] = { globalSizeX, globalSizeY, 1 };
981 size_t lt[3] = { blockSizeX, blockSizeY, 1 };
982 std::vector<std::pair<size_t , const void *> > args;
983 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&Dx.data ));
984 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&Dy.data));
985 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&dst.data));
986 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&Dx.offset ));
987 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&Dx.wholerows ));
988 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&Dx.wholecols ));
989 args.push_back( std::make_pair(sizeof(cl_int), (void *)&Dx.step));
990 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&Dy.offset ));
991 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&Dy.wholerows ));
992 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&Dy.wholecols ));
993 args.push_back( std::make_pair(sizeof(cl_int), (void *)&Dy.step));
994 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.offset));
995 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.rows));
996 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.cols));
997 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.step));
998 args.push_back( std::make_pair( sizeof(cl_float) , (void *)&k));
1000 openCLExecuteKernel(dst.clCxt, source, kernelName, gt, lt, args, -1, -1, buildOptions.c_str());
1003 void cornerHarris(const oclMat &src, oclMat &dst, int blockSize, int ksize,
1004 double k, int borderType)
1007 cornerHarris_dxdy(src, dst, dx, dy, blockSize, ksize, k, borderType);
1010 void cornerHarris_dxdy(const oclMat &src, oclMat &dst, oclMat &dx, oclMat &dy, int blockSize, int ksize,
1011 double k, int borderType)
1013 if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
1015 CV_Error(Error::OpenCLDoubleNotSupported, "Selected device doesn't support double");
1019 CV_Assert(borderType == cv::BORDER_CONSTANT || borderType == cv::BORDER_REFLECT101 || borderType == cv::BORDER_REPLICATE
1020 || borderType == cv::BORDER_REFLECT);
1022 extractCovData(src, dx, dy, blockSize, ksize, borderType);
1023 dst.create(src.size(), CV_32FC1);
1024 corner_ocl(&imgproc_calcHarris, "calcHarris", blockSize, static_cast<float>(k), dx, dy, dst, borderType);
1027 void cornerMinEigenVal(const oclMat &src, oclMat &dst, int blockSize, int ksize, int borderType)
1030 cornerMinEigenVal_dxdy(src, dst, dx, dy, blockSize, ksize, borderType);
1033 void cornerMinEigenVal_dxdy(const oclMat &src, oclMat &dst, oclMat &dx, oclMat &dy, int blockSize, int ksize, int borderType)
1035 if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
1037 CV_Error(Error::OpenCLDoubleNotSupported, "Selected device doesn't support double");
1041 CV_Assert(borderType == cv::BORDER_CONSTANT || borderType == cv::BORDER_REFLECT101 ||
1042 borderType == cv::BORDER_REPLICATE || borderType == cv::BORDER_REFLECT);
1044 extractCovData(src, dx, dy, blockSize, ksize, borderType);
1045 dst.create(src.size(), CV_32F);
1047 corner_ocl(&imgproc_calcMinEigenVal, "calcMinEigenVal", blockSize, 0, dx, dy, dst, borderType);
1050 /////////////////////////////////// MeanShiftfiltering ///////////////////////////////////////////////
1052 static void meanShiftFiltering_gpu(const oclMat &src, oclMat dst, int sp, int sr, int maxIter, float eps)
1054 CV_Assert( (src.cols == dst.cols) && (src.rows == dst.rows) );
1055 CV_Assert( !(dst.step & 0x3) );
1057 //Arrange the NDRange
1058 int col = src.cols, row = src.rows;
1059 int ltx = 16, lty = 8;
1060 if (src.cols % ltx != 0)
1061 col = (col / ltx + 1) * ltx;
1062 if (src.rows % lty != 0)
1063 row = (row / lty + 1) * lty;
1065 size_t globalThreads[3] = {col, row, 1};
1066 size_t localThreads[3] = {ltx, lty, 1};
1069 std::vector<std::pair<size_t , const void *> > args;
1070 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&dst.data ));
1071 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst.step ));
1072 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&src.data ));
1073 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.step ));
1074 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst.offset ));
1075 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.offset ));
1076 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst.cols ));
1077 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst.rows ));
1078 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sp ));
1079 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sr ));
1080 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&maxIter ));
1081 args.push_back( std::make_pair( sizeof(cl_float) , (void *)&eps ));
1083 openCLExecuteKernel(src.clCxt, &meanShift, "meanshift_kernel", globalThreads, localThreads, args, -1, -1);
1086 void meanShiftFiltering(const oclMat &src, oclMat &dst, int sp, int sr, TermCriteria criteria)
1089 CV_Error(Error::StsBadArg, "The input image is empty");
1091 if ( src.depth() != CV_8U || src.oclchannels() != 4 )
1092 CV_Error(Error::StsUnsupportedFormat, "Only 8-bit, 4-channel images are supported");
1094 dst.create( src.size(), CV_8UC4 );
1096 if ( !(criteria.type & TermCriteria::MAX_ITER) )
1097 criteria.maxCount = 5;
1099 int maxIter = std::min(std::max(criteria.maxCount, 1), 100);
1102 if ( !(criteria.type & TermCriteria::EPS) )
1104 eps = (float)std::max(criteria.epsilon, 0.0);
1106 meanShiftFiltering_gpu(src, dst, sp, sr, maxIter, eps);
1109 static void meanShiftProc_gpu(const oclMat &src, oclMat dstr, oclMat dstsp, int sp, int sr, int maxIter, float eps)
1112 CV_Assert( (src.cols == dstr.cols) && (src.rows == dstr.rows) &&
1113 (src.rows == dstsp.rows) && (src.cols == dstsp.cols));
1114 CV_Assert( !(dstsp.step & 0x3) );
1116 //Arrange the NDRange
1117 int col = src.cols, row = src.rows;
1118 int ltx = 16, lty = 8;
1119 if (src.cols % ltx != 0)
1120 col = (col / ltx + 1) * ltx;
1121 if (src.rows % lty != 0)
1122 row = (row / lty + 1) * lty;
1124 size_t globalThreads[3] = {col, row, 1};
1125 size_t localThreads[3] = {ltx, lty, 1};
1128 std::vector<std::pair<size_t , const void *> > args;
1129 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&src.data ));
1130 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&dstr.data ));
1131 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&dstsp.data ));
1132 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.step ));
1133 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dstr.step ));
1134 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dstsp.step ));
1135 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.offset ));
1136 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dstr.offset ));
1137 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dstsp.offset ));
1138 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dstr.cols ));
1139 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dstr.rows ));
1140 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sp ));
1141 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sr ));
1142 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&maxIter ));
1143 args.push_back( std::make_pair( sizeof(cl_float) , (void *)&eps ));
1145 openCLExecuteKernel(src.clCxt, &meanShift, "meanshiftproc_kernel", globalThreads, localThreads, args, -1, -1);
1148 void meanShiftProc(const oclMat &src, oclMat &dstr, oclMat &dstsp, int sp, int sr, TermCriteria criteria)
1151 CV_Error(Error::StsBadArg, "The input image is empty");
1153 if ( src.depth() != CV_8U || src.oclchannels() != 4 )
1154 CV_Error(Error::StsUnsupportedFormat, "Only 8-bit, 4-channel images are supported");
1156 // if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE))
1158 // CV_Error(Error::OpenCLDoubleNotSupportedNotSupported, "Selected device doesn't support double, so a deviation exists.\nIf the accuracy is acceptable, the error can be ignored.\n");
1162 dstr.create( src.size(), CV_8UC4 );
1163 dstsp.create( src.size(), CV_16SC2 );
1165 if ( !(criteria.type & TermCriteria::MAX_ITER) )
1166 criteria.maxCount = 5;
1168 int maxIter = std::min(std::max(criteria.maxCount, 1), 100);
1171 if ( !(criteria.type & TermCriteria::EPS) )
1173 eps = (float)std::max(criteria.epsilon, 0.0);
1175 meanShiftProc_gpu(src, dstr, dstsp, sp, sr, maxIter, eps);
1178 ///////////////////////////////////////////////////////////////////////////////////////////////////
1179 ////////////////////////////////////////////////////hist///////////////////////////////////////////////
1180 /////////////////////////////////////////////////////////////////////////////////////////////////////
1182 namespace histograms
1184 const int PARTIAL_HISTOGRAM256_COUNT = 256;
1185 const int HISTOGRAM256_BIN_COUNT = 256;
1187 ///////////////////////////////calcHist/////////////////////////////////////////////////////////////////
1188 static void calc_sub_hist(const oclMat &mat_src, const oclMat &mat_sub_hist)
1190 using namespace histograms;
1192 int depth = mat_src.depth();
1194 size_t localThreads[3] = { HISTOGRAM256_BIN_COUNT, 1, 1 };
1195 size_t globalThreads[3] = { PARTIAL_HISTOGRAM256_COUNT *localThreads[0], 1, 1};
1198 int dataWidth_bits = 4;
1199 int mask = dataWidth - 1;
1201 int cols = mat_src.cols * mat_src.oclchannels();
1202 int src_offset = mat_src.offset;
1203 int hist_step = mat_sub_hist.step >> 2;
1204 int left_col = 0, right_col = 0;
1206 if (cols >= dataWidth * 2 - 1)
1208 left_col = dataWidth - (src_offset & mask);
1210 src_offset += left_col;
1212 right_col = cols & mask;
1220 globalThreads[0] = 0;
1223 std::vector<std::pair<size_t , const void *> > args;
1224 if (globalThreads[0] != 0)
1226 int tempcols = cols >> dataWidth_bits;
1227 int inc_x = globalThreads[0] % tempcols;
1228 int inc_y = globalThreads[0] / tempcols;
1229 src_offset >>= dataWidth_bits;
1230 int src_step = mat_src.step >> dataWidth_bits;
1231 int datacount = tempcols * mat_src.rows;
1233 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_src.data));
1234 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src_step));
1235 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src_offset));
1236 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_sub_hist.data));
1237 args.push_back( std::make_pair( sizeof(cl_int), (void *)&datacount));
1238 args.push_back( std::make_pair( sizeof(cl_int), (void *)&tempcols));
1239 args.push_back( std::make_pair( sizeof(cl_int), (void *)&inc_x));
1240 args.push_back( std::make_pair( sizeof(cl_int), (void *)&inc_y));
1241 args.push_back( std::make_pair( sizeof(cl_int), (void *)&hist_step));
1243 openCLExecuteKernel(mat_src.clCxt, &imgproc_histogram, "calc_sub_hist", globalThreads, localThreads, args, -1, depth);
1246 if (left_col != 0 || right_col != 0)
1248 src_offset = mat_src.offset;
1249 localThreads[0] = 1;
1250 localThreads[1] = 256;
1251 globalThreads[0] = left_col + right_col;
1252 globalThreads[1] = mat_src.rows;
1255 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_src.data));
1256 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src.step));
1257 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src_offset));
1258 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_sub_hist.data));
1259 args.push_back( std::make_pair( sizeof(cl_int), (void *)&left_col));
1260 args.push_back( std::make_pair( sizeof(cl_int), (void *)&cols));
1261 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src.rows));
1262 args.push_back( std::make_pair( sizeof(cl_int), (void *)&hist_step));
1264 openCLExecuteKernel(mat_src.clCxt, &imgproc_histogram, "calc_sub_hist_border", globalThreads, localThreads, args, -1, depth);
1268 static void merge_sub_hist(const oclMat &sub_hist, oclMat &mat_hist)
1270 using namespace histograms;
1272 size_t localThreads[3] = { 256, 1, 1 };
1273 size_t globalThreads[3] = { HISTOGRAM256_BIN_COUNT *localThreads[0], 1, 1};
1274 int src_step = sub_hist.step >> 2;
1276 std::vector<std::pair<size_t , const void *> > args;
1277 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&sub_hist.data));
1278 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_hist.data));
1279 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src_step));
1281 openCLExecuteKernel(sub_hist.clCxt, &imgproc_histogram, "merge_hist", globalThreads, localThreads, args, -1, -1);
1284 void calcHist(const oclMat &mat_src, oclMat &mat_hist)
1286 using namespace histograms;
1287 CV_Assert(mat_src.type() == CV_8UC1);
1288 mat_hist.create(1, 256, CV_32SC1);
1290 oclMat buf(PARTIAL_HISTOGRAM256_COUNT, HISTOGRAM256_BIN_COUNT, CV_32SC1);
1293 calc_sub_hist(mat_src, buf);
1294 merge_sub_hist(buf, mat_hist);
1297 ///////////////////////////////////equalizeHist/////////////////////////////////////////////////////
1298 void equalizeHist(const oclMat &mat_src, oclMat &mat_dst)
1300 mat_dst.create(mat_src.rows, mat_src.cols, CV_8UC1);
1302 oclMat mat_hist(1, 256, CV_32SC1);
1304 calcHist(mat_src, mat_hist);
1306 size_t localThreads[3] = { 256, 1, 1};
1307 size_t globalThreads[3] = { 256, 1, 1};
1308 oclMat lut(1, 256, CV_8UC1);
1309 int total = mat_src.rows * mat_src.cols;
1311 std::vector<std::pair<size_t , const void *> > args;
1312 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&lut.data));
1313 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_hist.data));
1314 args.push_back( std::make_pair( sizeof(int), (void *)&total));
1316 openCLExecuteKernel(mat_src.clCxt, &imgproc_histogram, "calLUT", globalThreads, localThreads, args, -1, -1);
1317 LUT(mat_src, lut, mat_dst);
1320 ////////////////////////////////////////////////////////////////////////
1324 static void calcLut(const oclMat &src, oclMat &dst,
1325 const int tilesX, const int tilesY, const cv::Size tileSize,
1326 const int clipLimit, const float lutScale)
1329 tile_size.s[0] = tileSize.width;
1330 tile_size.s[1] = tileSize.height;
1332 std::vector<std::pair<size_t , const void *> > args;
1333 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&src.data ));
1334 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst.data ));
1335 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.step ));
1336 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.step ));
1337 args.push_back( std::make_pair( sizeof(cl_int2), (void *)&tile_size ));
1338 args.push_back( std::make_pair( sizeof(cl_int), (void *)&tilesX ));
1339 args.push_back( std::make_pair( sizeof(cl_int), (void *)&clipLimit ));
1340 args.push_back( std::make_pair( sizeof(cl_float), (void *)&lutScale ));
1341 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.offset ));
1342 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.offset ));
1344 String kernelName = "calcLut";
1345 size_t localThreads[3] = { 32, 8, 1 };
1346 size_t globalThreads[3] = { tilesX * localThreads[0], tilesY * localThreads[1], 1 };
1347 bool is_cpu = isCpuDevice();
1349 openCLExecuteKernel(Context::getContext(), &imgproc_clahe, kernelName, globalThreads, localThreads, args, -1, -1, (char*)"-D CPU");
1352 cl_kernel kernel = openCLGetKernelFromSource(Context::getContext(), &imgproc_clahe, kernelName);
1353 int wave_size = (int)queryWaveFrontSize(kernel);
1354 openCLSafeCall(clReleaseKernel(kernel));
1356 std::string opt = format("-D WAVE_SIZE=%d", wave_size);
1357 openCLExecuteKernel(Context::getContext(), &imgproc_clahe, kernelName, globalThreads, localThreads, args, -1, -1, opt.c_str());
1361 static void transform(const oclMat &src, oclMat &dst, const oclMat &lut,
1362 const int tilesX, const int tilesY, const Size & tileSize)
1365 tile_size.s[0] = tileSize.width;
1366 tile_size.s[1] = tileSize.height;
1368 std::vector<std::pair<size_t , const void *> > args;
1369 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&src.data ));
1370 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst.data ));
1371 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&lut.data ));
1372 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.step ));
1373 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.step ));
1374 args.push_back( std::make_pair( sizeof(cl_int), (void *)&lut.step ));
1375 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.cols ));
1376 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.rows ));
1377 args.push_back( std::make_pair( sizeof(cl_int2), (void *)&tile_size ));
1378 args.push_back( std::make_pair( sizeof(cl_int), (void *)&tilesX ));
1379 args.push_back( std::make_pair( sizeof(cl_int), (void *)&tilesY ));
1380 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.offset ));
1381 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.offset ));
1382 args.push_back( std::make_pair( sizeof(cl_int), (void *)&lut.offset ));
1384 size_t localThreads[3] = { 32, 8, 1 };
1385 size_t globalThreads[3] = { src.cols, src.rows, 1 };
1387 openCLExecuteKernel(Context::getContext(), &imgproc_clahe, "transform", globalThreads, localThreads, args, -1, -1);
1393 class CLAHE_Impl : public cv::CLAHE
1396 CLAHE_Impl(double clipLimit = 40.0, int tilesX = 8, int tilesY = 8);
1398 cv::AlgorithmInfo* info() const;
1400 void apply(cv::InputArray src, cv::OutputArray dst);
1402 void setClipLimit(double clipLimit);
1403 double getClipLimit() const;
1405 void setTilesGridSize(cv::Size tileGridSize);
1406 cv::Size getTilesGridSize() const;
1408 void collectGarbage();
1419 CLAHE_Impl::CLAHE_Impl(double clipLimit, int tilesX, int tilesY) :
1420 clipLimit_(clipLimit), tilesX_(tilesX), tilesY_(tilesY)
1424 CV_INIT_ALGORITHM(CLAHE_Impl, "CLAHE_OCL",
1425 obj.info()->addParam(obj, "clipLimit", obj.clipLimit_);
1426 obj.info()->addParam(obj, "tilesX", obj.tilesX_);
1427 obj.info()->addParam(obj, "tilesY", obj.tilesY_))
1429 void CLAHE_Impl::apply(cv::InputArray src_raw, cv::OutputArray dst_raw)
1431 oclMat& src = getOclMatRef(src_raw);
1432 oclMat& dst = getOclMatRef(dst_raw);
1433 CV_Assert( src.type() == CV_8UC1 );
1435 dst.create( src.size(), src.type() );
1437 const int histSize = 256;
1439 ensureSizeIsEnough(tilesX_ * tilesY_, histSize, CV_8UC1, lut_);
1444 if (src.cols % tilesX_ == 0 && src.rows % tilesY_ == 0)
1446 tileSize = cv::Size(src.cols / tilesX_, src.rows / tilesY_);
1451 ocl::copyMakeBorder(src, srcExt_, 0, tilesY_ - (src.rows % tilesY_), 0,
1452 tilesX_ - (src.cols % tilesX_), BORDER_REFLECT_101, Scalar::all(0));
1454 tileSize = Size(srcExt_.cols / tilesX_, srcExt_.rows / tilesY_);
1455 srcForLut = srcExt_;
1458 const int tileSizeTotal = tileSize.area();
1459 const float lutScale = static_cast<float>(histSize - 1) / tileSizeTotal;
1462 if (clipLimit_ > 0.0)
1464 clipLimit = static_cast<int>(clipLimit_ * tileSizeTotal / histSize);
1465 clipLimit = std::max(clipLimit, 1);
1468 clahe::calcLut(srcForLut, lut_, tilesX_, tilesY_, tileSize, clipLimit, lutScale);
1469 clahe::transform(src, dst, lut_, tilesX_, tilesY_, tileSize);
1472 void CLAHE_Impl::setClipLimit(double clipLimit)
1474 clipLimit_ = clipLimit;
1477 double CLAHE_Impl::getClipLimit() const
1482 void CLAHE_Impl::setTilesGridSize(cv::Size tileGridSize)
1484 tilesX_ = tileGridSize.width;
1485 tilesY_ = tileGridSize.height;
1488 cv::Size CLAHE_Impl::getTilesGridSize() const
1490 return cv::Size(tilesX_, tilesY_);
1493 void CLAHE_Impl::collectGarbage()
1500 cv::Ptr<cv::CLAHE> createCLAHE(double clipLimit, cv::Size tileGridSize)
1502 return makePtr<CLAHE_Impl>(clipLimit, tileGridSize.width, tileGridSize.height);
1505 //////////////////////////////////bilateralFilter////////////////////////////////////////////////////
1507 static void oclbilateralFilter_8u( const oclMat &src, oclMat &dst, int d,
1508 double sigma_color, double sigma_space,
1511 int cn = src.channels();
1512 int i, j, maxk, radius;
1514 CV_Assert( (src.channels() == 1 || src.channels() == 3) &&
1515 src.type() == dst.type() && src.size() == dst.size() &&
1516 src.data != dst.data );
1518 if ( sigma_color <= 0 )
1520 if ( sigma_space <= 0 )
1523 double gauss_color_coeff = -0.5 / (sigma_color * sigma_color);
1524 double gauss_space_coeff = -0.5 / (sigma_space * sigma_space);
1527 radius = cvRound(sigma_space * 1.5);
1530 radius = MAX(radius, 1);
1534 copyMakeBorder( src, temp, radius, radius, radius, radius, borderType );
1536 std::vector<float> _color_weight(cn * 256);
1537 std::vector<float> _space_weight(d * d);
1538 std::vector<int> _space_ofs(d * d);
1539 float *color_weight = &_color_weight[0];
1540 float *space_weight = &_space_weight[0];
1541 int *space_ofs = &_space_ofs[0];
1543 int dst_step_in_pixel = dst.step / dst.elemSize();
1544 int dst_offset_in_pixel = dst.offset / dst.elemSize();
1545 int temp_step_in_pixel = temp.step / temp.elemSize();
1547 // initialize color-related bilateral filter coefficients
1548 for( i = 0; i < 256 * cn; i++ )
1549 color_weight[i] = (float)std::exp(i * i * gauss_color_coeff);
1551 // initialize space-related bilateral filter coefficients
1552 for( i = -radius, maxk = 0; i <= radius; i++ )
1553 for( j = -radius; j <= radius; j++ )
1555 double r = std::sqrt((double)i * i + (double)j * j);
1558 space_weight[maxk] = (float)std::exp(r * r * gauss_space_coeff);
1559 space_ofs[maxk++] = (int)(i * temp_step_in_pixel + j);
1562 oclMat oclcolor_weight(1, cn * 256, CV_32FC1, color_weight);
1563 oclMat oclspace_weight(1, d * d, CV_32FC1, space_weight);
1564 oclMat oclspace_ofs(1, d * d, CV_32SC1, space_ofs);
1566 String kernelName = "bilateral";
1567 size_t localThreads[3] = { 16, 16, 1 };
1568 size_t globalThreads[3] = { dst.cols, dst.rows, 1 };
1570 if ((dst.type() == CV_8UC1) && ((dst.offset & 3) == 0) && ((dst.cols & 3) == 0))
1572 kernelName = "bilateral2";
1573 globalThreads[0] = dst.cols >> 2;
1576 std::vector<std::pair<size_t , const void *> > args;
1577 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst.data ));
1578 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&temp.data ));
1579 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.rows ));
1580 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.cols ));
1581 args.push_back( std::make_pair( sizeof(cl_int), (void *)&maxk ));
1582 args.push_back( std::make_pair( sizeof(cl_int), (void *)&radius ));
1583 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst_step_in_pixel ));
1584 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst_offset_in_pixel ));
1585 args.push_back( std::make_pair( sizeof(cl_int), (void *)&temp_step_in_pixel ));
1586 args.push_back( std::make_pair( sizeof(cl_int), (void *)&temp.rows ));
1587 args.push_back( std::make_pair( sizeof(cl_int), (void *)&temp.cols ));
1588 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&oclcolor_weight.data ));
1589 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&oclspace_weight.data ));
1590 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&oclspace_ofs.data ));
1592 openCLExecuteKernel(src.clCxt, &imgproc_bilateral, kernelName, globalThreads, localThreads, args, dst.oclchannels(), dst.depth());
1595 void bilateralFilter(const oclMat &src, oclMat &dst, int radius, double sigmaclr, double sigmaspc, int borderType)
1597 dst.create( src.size(), src.type() );
1598 if ( src.depth() == CV_8U )
1599 oclbilateralFilter_8u( src, dst, radius, sigmaclr, sigmaspc, borderType );
1601 CV_Error(Error::StsUnsupportedFormat, "Bilateral filtering is only implemented for CV_8U images");
1606 //////////////////////////////////mulSpectrums////////////////////////////////////////////////////
1607 void cv::ocl::mulSpectrums(const oclMat &a, const oclMat &b, oclMat &c, int /*flags*/, float scale, bool conjB)
1609 CV_Assert(a.type() == CV_32FC2);
1610 CV_Assert(b.type() == CV_32FC2);
1612 c.create(a.size(), CV_32FC2);
1614 size_t lt[3] = { 16, 16, 1 };
1615 size_t gt[3] = { a.cols, a.rows, 1 };
1617 String kernelName = conjB ? "mulAndScaleSpectrumsKernel_CONJ":"mulAndScaleSpectrumsKernel";
1619 std::vector<std::pair<size_t , const void *> > args;
1620 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&a.data ));
1621 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&b.data ));
1622 args.push_back( std::make_pair( sizeof(cl_float), (void *)&scale));
1623 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&c.data ));
1624 args.push_back( std::make_pair( sizeof(cl_int), (void *)&a.cols ));
1625 args.push_back( std::make_pair( sizeof(cl_int), (void *)&a.rows));
1626 args.push_back( std::make_pair( sizeof(cl_int), (void *)&a.step ));
1628 Context *clCxt = Context::getContext();
1629 openCLExecuteKernel(clCxt, &imgproc_mulAndScaleSpectrums, kernelName, gt, lt, args, -1, -1);
1631 //////////////////////////////////convolve////////////////////////////////////////////////////
1632 // ported from CUDA module
1633 void cv::ocl::ConvolveBuf::create(Size image_size, Size templ_size)
1635 result_size = Size(image_size.width - templ_size.width + 1,
1636 image_size.height - templ_size.height + 1);
1638 block_size = user_block_size;
1639 if (user_block_size.width == 0 || user_block_size.height == 0)
1640 block_size = estimateBlockSize(result_size, templ_size);
1642 dft_size.width = 1 << int(ceil(std::log(block_size.width + templ_size.width - 1.) / std::log(2.)));
1643 dft_size.height = 1 << int(ceil(std::log(block_size.height + templ_size.height - 1.) / std::log(2.)));
1645 // CUFFT has hard-coded kernels for power-of-2 sizes (up to 8192),
1646 // see CUDA Toolkit 4.1 CUFFT Library Programming Guide
1647 //if (dft_size.width > 8192)
1648 dft_size.width = getOptimalDFTSize(block_size.width + templ_size.width - 1.);
1649 //if (dft_size.height > 8192)
1650 dft_size.height = getOptimalDFTSize(block_size.height + templ_size.height - 1.);
1652 // To avoid wasting time doing small DFTs
1653 dft_size.width = std::max(dft_size.width, 512);
1654 dft_size.height = std::max(dft_size.height, 512);
1656 image_block.create(dft_size, CV_32F);
1657 templ_block.create(dft_size, CV_32F);
1658 result_data.create(dft_size, CV_32F);
1660 //spect_len = dft_size.height * (dft_size.width / 2 + 1);
1661 image_spect.create(dft_size.height, dft_size.width / 2 + 1, CV_32FC2);
1662 templ_spect.create(dft_size.height, dft_size.width / 2 + 1, CV_32FC2);
1663 result_spect.create(dft_size.height, dft_size.width / 2 + 1, CV_32FC2);
1665 // Use maximum result matrix block size for the estimated DFT block size
1666 block_size.width = std::min(dft_size.width - templ_size.width + 1, result_size.width);
1667 block_size.height = std::min(dft_size.height - templ_size.height + 1, result_size.height);
1670 Size cv::ocl::ConvolveBuf::estimateBlockSize(Size result_size, Size /*templ_size*/)
1672 int width = (result_size.width + 2) / 3;
1673 int height = (result_size.height + 2) / 3;
1674 width = std::min(width, result_size.width);
1675 height = std::min(height, result_size.height);
1676 return Size(width, height);
1679 static void convolve_run_fft(const oclMat &image, const oclMat &templ, oclMat &result, bool ccorr, ConvolveBuf& buf)
1681 #if defined HAVE_CLAMDFFT
1682 CV_Assert(image.type() == CV_32F);
1683 CV_Assert(templ.type() == CV_32F);
1685 buf.create(image.size(), templ.size());
1686 result.create(buf.result_size, CV_32F);
1688 Size& block_size = buf.block_size;
1689 Size& dft_size = buf.dft_size;
1691 oclMat& image_block = buf.image_block;
1692 oclMat& templ_block = buf.templ_block;
1693 oclMat& result_data = buf.result_data;
1695 oclMat& image_spect = buf.image_spect;
1696 oclMat& templ_spect = buf.templ_spect;
1697 oclMat& result_spect = buf.result_spect;
1699 oclMat templ_roi = templ;
1700 copyMakeBorder(templ_roi, templ_block, 0, templ_block.rows - templ_roi.rows, 0,
1701 templ_block.cols - templ_roi.cols, 0, Scalar());
1703 cv::ocl::dft(templ_block, templ_spect, dft_size);
1705 // Process all blocks of the result matrix
1706 for (int y = 0; y < result.rows; y += block_size.height)
1708 for (int x = 0; x < result.cols; x += block_size.width)
1710 Size image_roi_size(std::min(x + dft_size.width, image.cols) - x,
1711 std::min(y + dft_size.height, image.rows) - y);
1712 Rect roi0(x, y, image_roi_size.width, image_roi_size.height);
1714 oclMat image_roi(image, roi0);
1716 copyMakeBorder(image_roi, image_block, 0, image_block.rows - image_roi.rows,
1717 0, image_block.cols - image_roi.cols, 0, Scalar());
1719 cv::ocl::dft(image_block, image_spect, dft_size);
1721 mulSpectrums(image_spect, templ_spect, result_spect, 0,
1722 1.f / dft_size.area(), ccorr);
1724 cv::ocl::dft(result_spect, result_data, dft_size, cv::DFT_INVERSE | cv::DFT_REAL_OUTPUT);
1726 Size result_roi_size(std::min(x + block_size.width, result.cols) - x,
1727 std::min(y + block_size.height, result.rows) - y);
1729 Rect roi1(x, y, result_roi_size.width, result_roi_size.height);
1730 Rect roi2(0, 0, result_roi_size.width, result_roi_size.height);
1732 oclMat result_roi(result, roi1);
1733 oclMat result_block(result_data, roi2);
1735 result_block.copyTo(result_roi);
1740 CV_Error(Error::OpenCLNoAMDBlasFft, "OpenCL DFT is not implemented");
1741 #define UNUSED(x) (void)(x);
1742 UNUSED(image) UNUSED(templ) UNUSED(result) UNUSED(ccorr) UNUSED(buf)
1747 static void convolve_run(const oclMat &src, const oclMat &temp1, oclMat &dst, String kernelName, const cv::ocl::ProgramEntry* source)
1749 CV_Assert(src.depth() == CV_32FC1);
1750 CV_Assert(temp1.depth() == CV_32F);
1751 CV_Assert(temp1.cols <= 17 && temp1.rows <= 17);
1753 dst.create(src.size(), src.type());
1755 CV_Assert(src.cols == dst.cols && src.rows == dst.rows);
1756 CV_Assert(src.type() == dst.type());
1758 size_t localThreads[3] = { 16, 16, 1 };
1759 size_t globalThreads[3] = { dst.cols, dst.rows, 1 };
1761 int src_step = src.step / src.elemSize(), src_offset = src.offset / src.elemSize();
1762 int dst_step = dst.step / dst.elemSize(), dst_offset = dst.offset / dst.elemSize();
1763 int temp1_step = temp1.step / temp1.elemSize(), temp1_offset = temp1.offset / temp1.elemSize();
1765 std::vector<std::pair<size_t , const void *> > args;
1766 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&src.data ));
1767 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&temp1.data ));
1768 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst.data ));
1769 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.rows ));
1770 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.cols ));
1771 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src_step ));
1772 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst_step ));
1773 args.push_back( std::make_pair( sizeof(cl_int), (void *)&temp1_step ));
1774 args.push_back( std::make_pair( sizeof(cl_int), (void *)&temp1.rows ));
1775 args.push_back( std::make_pair( sizeof(cl_int), (void *)&temp1.cols ));
1776 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src_offset ));
1777 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst_offset ));
1778 args.push_back( std::make_pair( sizeof(cl_int), (void *)&temp1_offset ));
1780 openCLExecuteKernel(src.clCxt, source, kernelName, globalThreads, localThreads, args, -1, dst.depth());
1783 void cv::ocl::convolve(const oclMat &x, const oclMat &t, oclMat &y, bool ccorr)
1785 CV_Assert(x.depth() == CV_32F);
1786 CV_Assert(t.depth() == CV_32F);
1787 y.create(x.size(), x.type());
1788 String kernelName = "convolve";
1789 if(t.cols > 17 || t.rows > 17)
1792 convolve_run_fft(x, t, y, ccorr, buf);
1796 CV_Assert(ccorr == false);
1797 convolve_run(x, t, y, kernelName, &imgproc_convolve);
1800 void cv::ocl::convolve(const oclMat &image, const oclMat &templ, oclMat &result, bool ccorr, ConvolveBuf& buf)
1802 result.create(image.size(), image.type());
1803 convolve_run_fft(image, templ, result, ccorr, buf);