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 // Sen Liu, swjtuls1987@126.com
30 // Redistribution and use in source and binary forms, with or without modification,
31 // are permitted provided that the following conditions are met:
33 // * Redistribution's of source code must retain the above copyright notice,
34 // this list of conditions and the following disclaimer.
36 // * Redistribution's in binary form must reproduce the above copyright notice,
37 // this list of conditions and the following disclaimer in the documentation
38 // and/or other materials provided with the distribution.
40 // * The name of the copyright holders may not be used to endorse or promote products
41 // derived from this software without specific prior written permission.
43 // This software is provided by the copyright holders and contributors "as is" and
44 // any express or implied warranties, including, but not limited to, the implied
45 // warranties of merchantability and fitness for a particular purpose are disclaimed.
46 // In no event shall the Intel Corporation or contributors be liable for any direct,
47 // indirect, incidental, special, exemplary, or consequential damages
48 // (including, but not limited to, procurement of substitute goods or services;
49 // loss of use, data, or profits; or business interruption) however caused
50 // and on any theory of liability, whether in contract, strict liability,
51 // or tort (including negligence or otherwise) arising in any way out of
52 // the use of this software, even if advised of the possibility of such damage.
56 #include "precomp.hpp"
57 #include "opencl_kernels.hpp"
60 using namespace cv::ocl;
66 ////////////////////////////////////OpenCL call wrappers////////////////////////////
68 template <typename T> struct index_and_sizeof;
69 template <> struct index_and_sizeof<char>
73 template <> struct index_and_sizeof<unsigned char>
77 template <> struct index_and_sizeof<short>
81 template <> struct index_and_sizeof<unsigned short>
85 template <> struct index_and_sizeof<int>
89 template <> struct index_and_sizeof<float>
93 template <> struct index_and_sizeof<double>
98 /////////////////////////////////////////////////////////////////////////////////////
101 static std::vector<uchar> scalarToVector(const cv::Scalar & sc, int depth, int ocn, int cn)
103 CV_Assert(ocn == cn || (ocn == 4 && cn == 3));
105 static const int sizeMap[] = { sizeof(uchar), sizeof(char), sizeof(ushort),
106 sizeof(short), sizeof(int), sizeof(float), sizeof(double) };
108 int elemSize1 = sizeMap[depth];
109 int bufSize = elemSize1 * ocn;
110 std::vector<uchar> _buf(bufSize);
111 uchar * buf = &_buf[0];
112 scalarToRawData(sc, buf, CV_MAKE_TYPE(depth, cn));
113 memset(buf + elemSize1 * cn, 0, (ocn - cn) * elemSize1);
118 static void threshold_runner(const oclMat &src, oclMat &dst, double thresh, double maxVal, int thresholdType)
120 bool ival = src.depth() < CV_32F;
121 int cn = src.channels(), vecSize = 4, depth = src.depth();
122 std::vector<uchar> thresholdValue = scalarToVector(cv::Scalar::all(ival ? cvFloor(thresh) : thresh), dst.depth(),
123 dst.oclchannels(), dst.channels());
124 std::vector<uchar> maxValue = scalarToVector(cv::Scalar::all(maxVal), dst.depth(), dst.oclchannels(), dst.channels());
126 const char * const thresholdMap[] = { "THRESH_BINARY", "THRESH_BINARY_INV", "THRESH_TRUNC",
127 "THRESH_TOZERO", "THRESH_TOZERO_INV" };
128 const char * const channelMap[] = { "", "", "2", "4", "4" };
129 const char * const typeMap[] = { "uchar", "char", "ushort", "short", "int", "float", "double" };
130 std::string buildOptions = format("-D T=%s%s -D %s", typeMap[depth], channelMap[cn], thresholdMap[thresholdType]);
132 int elemSize = src.elemSize();
133 int src_step = src.step / elemSize, src_offset = src.offset / elemSize;
134 int dst_step = dst.step / elemSize, dst_offset = dst.offset / elemSize;
136 vector< pair<size_t, const void *> > args;
137 args.push_back( make_pair(sizeof(cl_mem), (void *)&src.data));
138 args.push_back( make_pair(sizeof(cl_int), (void *)&src_offset));
139 args.push_back( make_pair(sizeof(cl_int), (void *)&src_step));
140 args.push_back( make_pair(sizeof(cl_mem), (void *)&dst.data));
141 args.push_back( make_pair(sizeof(cl_int), (void *)&dst_offset));
142 args.push_back( make_pair(sizeof(cl_int), (void *)&dst_step));
143 args.push_back( make_pair(thresholdValue.size(), (void *)&thresholdValue[0]));
144 args.push_back( make_pair(maxValue.size(), (void *)&maxValue[0]));
146 int max_index = dst.cols, cols = dst.cols;
147 if (cn == 1 && vecSize > 1)
149 CV_Assert(((vecSize - 1) & vecSize) == 0 && vecSize <= 16);
150 cols = divUp(cols, vecSize);
151 buildOptions += format(" -D VECTORIZED -D VT=%s%d -D VLOADN=vload%d -D VECSIZE=%d -D VSTOREN=vstore%d",
152 typeMap[depth], vecSize, vecSize, vecSize, vecSize);
154 int vecSizeBytes = vecSize * dst.elemSize1();
155 if ((dst.offset % dst.step) % vecSizeBytes == 0 && dst.step % vecSizeBytes == 0)
156 buildOptions += " -D DST_ALIGNED";
157 if ((src.offset % src.step) % vecSizeBytes == 0 && src.step % vecSizeBytes == 0)
158 buildOptions += " -D SRC_ALIGNED";
160 args.push_back( make_pair(sizeof(cl_int), (void *)&max_index));
163 args.push_back( make_pair(sizeof(cl_int), (void *)&dst.rows));
164 args.push_back( make_pair(sizeof(cl_int), (void *)&cols));
166 size_t localThreads[3] = { 16, 16, 1 };
167 size_t globalThreads[3] = { cols, dst.rows, 1 };
169 openCLExecuteKernel(src.clCxt, &imgproc_threshold, "threshold", globalThreads, localThreads, args,
170 -1, -1, buildOptions.c_str());
173 double threshold(const oclMat &src, oclMat &dst, double thresh, double maxVal, int thresholdType)
175 CV_Assert(thresholdType == THRESH_BINARY || thresholdType == THRESH_BINARY_INV || thresholdType == THRESH_TRUNC
176 || thresholdType == THRESH_TOZERO || thresholdType == THRESH_TOZERO_INV);
178 dst.create(src.size(), src.type());
179 threshold_runner(src, dst, thresh, maxVal, thresholdType);
184 ////////////////////////////////////////////////////////////////////////////////////////////
185 /////////////////////////////// remap //////////////////////////////////////////////////
186 ////////////////////////////////////////////////////////////////////////////////////////////
188 void remap( const oclMat &src, oclMat &dst, oclMat &map1, oclMat &map2, int interpolation, int borderType, const Scalar &borderValue )
190 Context *clCxt = src.clCxt;
191 bool supportsDouble = clCxt->supportsFeature(FEATURE_CL_DOUBLE);
192 if (!supportsDouble && src.depth() == CV_64F)
194 CV_Error(CV_OpenCLDoubleNotSupported, "Selected device does not support double");
198 CV_Assert(interpolation == INTER_LINEAR || interpolation == INTER_NEAREST
199 || interpolation == INTER_CUBIC || interpolation == INTER_LANCZOS4);
200 CV_Assert((map1.type() == CV_16SC2 && !map2.data) || (map1.type() == CV_32FC2 && !map2.data) ||
201 (map1.type() == CV_32FC1 && map2.type() == CV_32FC1));
202 CV_Assert(!map2.data || map2.size() == map1.size());
203 CV_Assert(borderType == BORDER_CONSTANT || borderType == BORDER_REPLICATE || borderType == BORDER_WRAP
204 || borderType == BORDER_REFLECT_101 || borderType == BORDER_REFLECT);
206 dst.create(map1.size(), src.type());
208 const char * const typeMap[] = { "uchar", "char", "ushort", "short", "int", "float", "double" };
209 const char * const channelMap[] = { "", "", "2", "4", "4" };
210 const char * const interMap[] = { "INTER_NEAREST", "INTER_LINEAR", "INTER_CUBIC", "INTER_LINEAR", "INTER_LANCZOS" };
211 const char * const borderMap[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", "BORDER_WRAP",
212 "BORDER_REFLECT_101", "BORDER_TRANSPARENT" };
214 string kernelName = "remap";
215 if ( map1.type() == CV_32FC2 && !map2.data )
216 kernelName += "_32FC2";
217 else if (map1.type() == CV_16SC2 && !map2.data)
218 kernelName += "_16SC2";
219 else if (map1.type() == CV_32FC1 && map2.type() == CV_32FC1)
220 kernelName += "_2_32FC1";
222 CV_Error(CV_StsBadArg, "Unsupported map types");
224 int ocn = dst.oclchannels();
225 size_t localThreads[3] = { 16, 16, 1};
226 size_t globalThreads[3] = { dst.cols, dst.rows, 1};
228 Mat scalar(1, 1, CV_MAKE_TYPE(dst.depth(), ocn), borderValue);
229 std::string buildOptions = format("-D %s -D %s -D T=%s%s", interMap[interpolation],
230 borderMap[borderType], typeMap[src.depth()], channelMap[ocn]);
232 if (interpolation != INTER_NEAREST)
234 int wdepth = std::max(CV_32F, dst.depth());
236 wdepth = std::min(CV_32F, wdepth);
238 buildOptions += format(" -D WT=%s%s -D convertToT=convert_%s%s%s -D convertToWT=convert_%s%s"
239 " -D convertToWT2=convert_%s2 -D WT2=%s2",
240 typeMap[wdepth], channelMap[ocn],
241 typeMap[src.depth()], channelMap[ocn], src.depth() < CV_32F ? "_sat_rte" : "",
242 typeMap[wdepth], channelMap[ocn],
243 typeMap[wdepth], typeMap[wdepth]);
246 int src_step = src.step / src.elemSize(), src_offset = src.offset / src.elemSize();
247 int map1_step = map1.step / map1.elemSize(), map1_offset = map1.offset / map1.elemSize();
248 int map2_step = map2.step / map2.elemSize(), map2_offset = map2.offset / map2.elemSize();
249 int dst_step = dst.step / dst.elemSize(), dst_offset = dst.offset / dst.elemSize();
251 vector< pair<size_t, const void *> > args;
252 args.push_back( make_pair(sizeof(cl_mem), (void *)&src.data));
253 args.push_back( make_pair(sizeof(cl_mem), (void *)&dst.data));
254 args.push_back( make_pair(sizeof(cl_mem), (void *)&map1.data));
256 args.push_back( make_pair(sizeof(cl_mem), (void *)&map2.data));
257 args.push_back( make_pair(sizeof(cl_int), (void *)&src_offset));
258 args.push_back( make_pair(sizeof(cl_int), (void *)&dst_offset));
259 args.push_back( make_pair(sizeof(cl_int), (void *)&map1_offset));
261 args.push_back( make_pair(sizeof(cl_int), (void *)&map2_offset));
262 args.push_back( make_pair(sizeof(cl_int), (void *)&src_step));
263 args.push_back( make_pair(sizeof(cl_int), (void *)&dst_step));
264 args.push_back( make_pair(sizeof(cl_int), (void *)&map1_step));
266 args.push_back( make_pair(sizeof(cl_int), (void *)&map2_step));
267 args.push_back( make_pair(sizeof(cl_int), (void *)&src.cols));
268 args.push_back( make_pair(sizeof(cl_int), (void *)&src.rows));
269 args.push_back( make_pair(sizeof(cl_int), (void *)&dst.cols));
270 args.push_back( make_pair(sizeof(cl_int), (void *)&dst.rows));
271 args.push_back( make_pair(scalar.elemSize(), (void *)scalar.data));
273 openCLExecuteKernel(clCxt, &imgproc_remap, kernelName, globalThreads, localThreads, args, -1, -1, buildOptions.c_str());
276 ////////////////////////////////////////////////////////////////////////////////////////////
279 static void resize_gpu( const oclMat &src, oclMat &dst, double fx, double fy, int interpolation)
281 CV_Assert( (src.channels() == dst.channels()) );
282 Context *clCxt = src.clCxt;
285 double ifx_d = 1. / fx;
286 double ify_d = 1. / fy;
287 int srcStep_in_pixel = src.step1() / src.oclchannels();
288 int srcoffset_in_pixel = src.offset / src.elemSize();
289 int dstStep_in_pixel = dst.step1() / dst.oclchannels();
290 int dstoffset_in_pixel = dst.offset / dst.elemSize();
293 if (interpolation == INTER_LINEAR)
294 kernelName = "resizeLN";
295 else if (interpolation == INTER_NEAREST)
296 kernelName = "resizeNN";
298 //TODO: improve this kernel
299 size_t blkSizeX = 16, blkSizeY = 16;
301 if (src.type() == CV_8UC1)
303 size_t cols = (dst.cols + dst.offset % 4 + 3) / 4;
304 glbSizeX = cols % blkSizeX == 0 && cols != 0 ? cols : (cols / blkSizeX + 1) * blkSizeX;
307 glbSizeX = dst.cols % blkSizeX == 0 && dst.cols != 0 ? dst.cols : (dst.cols / blkSizeX + 1) * blkSizeX;
309 size_t glbSizeY = dst.rows % blkSizeY == 0 && dst.rows != 0 ? dst.rows : (dst.rows / blkSizeY + 1) * blkSizeY;
310 size_t globalThreads[3] = {glbSizeX, glbSizeY, 1};
311 size_t localThreads[3] = {blkSizeX, blkSizeY, 1};
313 vector< pair<size_t, const void *> > args;
314 if (interpolation == INTER_NEAREST)
316 args.push_back( make_pair(sizeof(cl_mem), (void *)&dst.data));
317 args.push_back( make_pair(sizeof(cl_mem), (void *)&src.data));
318 args.push_back( make_pair(sizeof(cl_int), (void *)&dstoffset_in_pixel));
319 args.push_back( make_pair(sizeof(cl_int), (void *)&srcoffset_in_pixel));
320 args.push_back( make_pair(sizeof(cl_int), (void *)&dstStep_in_pixel));
321 args.push_back( make_pair(sizeof(cl_int), (void *)&srcStep_in_pixel));
322 args.push_back( make_pair(sizeof(cl_int), (void *)&src.cols));
323 args.push_back( make_pair(sizeof(cl_int), (void *)&src.rows));
324 args.push_back( make_pair(sizeof(cl_int), (void *)&dst.cols));
325 args.push_back( make_pair(sizeof(cl_int), (void *)&dst.rows));
326 if (src.clCxt->supportsFeature(FEATURE_CL_DOUBLE))
328 args.push_back( make_pair(sizeof(cl_double), (void *)&ifx_d));
329 args.push_back( make_pair(sizeof(cl_double), (void *)&ify_d));
333 args.push_back( make_pair(sizeof(cl_float), (void *)&ifx));
334 args.push_back( make_pair(sizeof(cl_float), (void *)&ify));
339 args.push_back( make_pair(sizeof(cl_mem), (void *)&dst.data));
340 args.push_back( make_pair(sizeof(cl_mem), (void *)&src.data));
341 args.push_back( make_pair(sizeof(cl_int), (void *)&dstoffset_in_pixel));
342 args.push_back( make_pair(sizeof(cl_int), (void *)&srcoffset_in_pixel));
343 args.push_back( make_pair(sizeof(cl_int), (void *)&dstStep_in_pixel));
344 args.push_back( make_pair(sizeof(cl_int), (void *)&srcStep_in_pixel));
345 args.push_back( make_pair(sizeof(cl_int), (void *)&src.cols));
346 args.push_back( make_pair(sizeof(cl_int), (void *)&src.rows));
347 args.push_back( make_pair(sizeof(cl_int), (void *)&dst.cols));
348 args.push_back( make_pair(sizeof(cl_int), (void *)&dst.rows));
349 args.push_back( make_pair(sizeof(cl_float), (void *)&ifx));
350 args.push_back( make_pair(sizeof(cl_float), (void *)&ify));
353 openCLExecuteKernel(clCxt, &imgproc_resize, kernelName, globalThreads, localThreads, args, src.oclchannels(), src.depth());
356 void resize(const oclMat &src, oclMat &dst, Size dsize,
357 double fx, double fy, int interpolation)
359 CV_Assert(src.type() == CV_8UC1 || src.type() == CV_8UC3 || src.type() == CV_8UC4
360 || src.type() == CV_32FC1 || src.type() == CV_32FC3 || src.type() == CV_32FC4);
361 CV_Assert(interpolation == INTER_LINEAR || interpolation == INTER_NEAREST);
362 CV_Assert( src.size().area() > 0 );
363 CV_Assert( !(dsize == Size()) || (fx > 0 && fy > 0) );
365 if (!(dsize == Size()) && (fx > 0 && fy > 0))
366 if (dsize.width != (int)(src.cols * fx) || dsize.height != (int)(src.rows * fy))
367 CV_Error(CV_StsUnmatchedSizes, "invalid dsize and fx, fy!");
369 if ( dsize == Size() )
370 dsize = Size(saturate_cast<int>(src.cols * fx), saturate_cast<int>(src.rows * fy));
373 fx = (double)dsize.width / src.cols;
374 fy = (double)dsize.height / src.rows;
377 dst.create(dsize, src.type());
379 if ( interpolation == INTER_NEAREST || interpolation == INTER_LINEAR )
381 resize_gpu( src, dst, fx, fy, interpolation);
385 CV_Error(CV_StsUnsupportedFormat, "Non-supported interpolation method");
388 ////////////////////////////////////////////////////////////////////////
391 void medianFilter(const oclMat &src, oclMat &dst, int m)
393 CV_Assert( m % 2 == 1 && m > 1 );
394 CV_Assert( (src.depth() == CV_8U || src.depth() == CV_32F) && (src.channels() == 1 || src.channels() == 4));
395 dst.create(src.size(), src.type());
397 int srcStep = src.step / src.elemSize(), dstStep = dst.step / dst.elemSize();
398 int srcOffset = src.offset / src.elemSize(), dstOffset = dst.offset / dst.elemSize();
400 Context *clCxt = src.clCxt;
402 vector< pair<size_t, const void *> > args;
403 args.push_back( make_pair( sizeof(cl_mem), (void *)&src.data));
404 args.push_back( make_pair( sizeof(cl_mem), (void *)&dst.data));
405 args.push_back( make_pair( sizeof(cl_int), (void *)&srcOffset));
406 args.push_back( make_pair( sizeof(cl_int), (void *)&dstOffset));
407 args.push_back( make_pair( sizeof(cl_int), (void *)&src.cols));
408 args.push_back( make_pair( sizeof(cl_int), (void *)&src.rows));
409 args.push_back( make_pair( sizeof(cl_int), (void *)&srcStep));
410 args.push_back( make_pair( sizeof(cl_int), (void *)&dstStep));
412 size_t globalThreads[3] = {(src.cols + 18) / 16 * 16, (src.rows + 15) / 16 * 16, 1};
413 size_t localThreads[3] = {16, 16, 1};
417 string kernelName = "medianFilter3";
418 openCLExecuteKernel(clCxt, &imgproc_median, kernelName, globalThreads, localThreads, args, src.oclchannels(), src.depth());
422 string kernelName = "medianFilter5";
423 openCLExecuteKernel(clCxt, &imgproc_median, kernelName, globalThreads, localThreads, args, src.oclchannels(), src.depth());
426 CV_Error(CV_StsBadArg, "Non-supported filter length");
429 ////////////////////////////////////////////////////////////////////////
432 void copyMakeBorder(const oclMat &src, oclMat &dst, int top, int bottom, int left, int right, int bordertype, const Scalar &scalar)
434 if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
436 CV_Error(CV_OpenCLDoubleNotSupported, "Selected device does not support double");
442 CV_Assert(top >= 0 && bottom >= 0 && left >= 0 && right >= 0);
444 if( (_src.wholecols != _src.cols || _src.wholerows != _src.rows) && (bordertype & BORDER_ISOLATED) == 0 )
448 _src.locateROI(wholeSize, ofs);
449 int dtop = std::min(ofs.y, top);
450 int dbottom = std::min(wholeSize.height - _src.rows - ofs.y, bottom);
451 int dleft = std::min(ofs.x, left);
452 int dright = std::min(wholeSize.width - _src.cols - ofs.x, right);
453 _src.adjustROI(dtop, dbottom, dleft, dright);
459 bordertype &= ~cv::BORDER_ISOLATED;
461 dst.create(_src.rows + top + bottom, _src.cols + left + right, _src.type());
462 int srcStep = _src.step / _src.elemSize(), dstStep = dst.step / dst.elemSize();
463 int srcOffset = _src.offset / _src.elemSize(), dstOffset = dst.offset / dst.elemSize();
464 int depth = _src.depth(), ochannels = _src.oclchannels();
466 int __bordertype[] = { BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT, BORDER_WRAP, BORDER_REFLECT_101 };
467 const char *borderstr[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", "BORDER_WRAP", "BORDER_REFLECT_101" };
469 int bordertype_index = -1;
470 for (int i = 0, end = sizeof(__bordertype) / sizeof(int); i < end; i++)
471 if (__bordertype[i] == bordertype)
473 bordertype_index = i;
476 if (bordertype_index < 0)
477 CV_Error(CV_StsBadArg, "Unsupported border type");
479 size_t localThreads[3] = { 16, 16, 1 };
480 size_t globalThreads[3] = { dst.cols, dst.rows, 1 };
482 vector< pair<size_t, const void *> > args;
483 args.push_back( make_pair( sizeof(cl_mem), (void *)&_src.data));
484 args.push_back( make_pair( sizeof(cl_mem), (void *)&dst.data));
485 args.push_back( make_pair( sizeof(cl_int), (void *)&dst.cols));
486 args.push_back( make_pair( sizeof(cl_int), (void *)&dst.rows));
487 args.push_back( make_pair( sizeof(cl_int), (void *)&_src.cols));
488 args.push_back( make_pair( sizeof(cl_int), (void *)&_src.rows));
489 args.push_back( make_pair( sizeof(cl_int), (void *)&srcStep));
490 args.push_back( make_pair( sizeof(cl_int), (void *)&srcOffset));
491 args.push_back( make_pair( sizeof(cl_int), (void *)&dstStep));
492 args.push_back( make_pair( sizeof(cl_int), (void *)&dstOffset));
493 args.push_back( make_pair( sizeof(cl_int), (void *)&top));
494 args.push_back( make_pair( sizeof(cl_int), (void *)&left));
496 const char * const typeMap[] = { "uchar", "char", "ushort", "short", "int", "float", "double" };
497 const char * const channelMap[] = { "", "", "2", "4", "4" };
498 std::string buildOptions = format("-D GENTYPE=%s%s -D %s",
499 typeMap[depth], channelMap[ochannels],
500 borderstr[bordertype_index]);
502 int cn = src.channels(), ocn = src.oclchannels();
503 int bufSize = src.elemSize1() * ocn;
504 AutoBuffer<uchar> _buf(bufSize);
505 uchar * buf = (uchar *)_buf;
506 scalarToRawData(scalar, buf, dst.type());
507 memset(buf + src.elemSize1() * cn, 0, (ocn - cn) * src.elemSize1());
509 args.push_back( make_pair( bufSize , (void *)buf ));
511 openCLExecuteKernel(src.clCxt, &imgproc_copymakeboder, "copymakeborder", globalThreads,
512 localThreads, args, -1, -1, buildOptions.c_str());
515 ////////////////////////////////////////////////////////////////////////
522 void convert_coeffs(F *M)
524 double D = M[0] * M[4] - M[1] * M[3];
525 D = D != 0 ? 1. / D : 0;
526 double A11 = M[4] * D, A22 = M[0] * D;
531 double b1 = -M[0] * M[2] - M[1] * M[5];
532 double b2 = -M[3] * M[2] - M[4] * M[5];
537 double invert(double *M)
539 #define Sd(y,x) (Sd[y*3+x])
540 #define Dd(y,x) (Dd[y*3+x])
541 #define det3(m) (m(0,0)*(m(1,1)*m(2,2) - m(1,2)*m(2,1)) - \
542 m(0,1)*(m(1,0)*m(2,2) - m(1,2)*m(2,0)) + \
543 m(0,2)*(m(1,0)*m(2,1) - m(1,1)*m(2,0)))
554 t[0] = (Sd(1, 1) * Sd(2, 2) - Sd(1, 2) * Sd(2, 1)) * d;
555 t[1] = (Sd(0, 2) * Sd(2, 1) - Sd(0, 1) * Sd(2, 2)) * d;
556 t[2] = (Sd(0, 1) * Sd(1, 2) - Sd(0, 2) * Sd(1, 1)) * d;
558 t[3] = (Sd(1, 2) * Sd(2, 0) - Sd(1, 0) * Sd(2, 2)) * d;
559 t[4] = (Sd(0, 0) * Sd(2, 2) - Sd(0, 2) * Sd(2, 0)) * d;
560 t[5] = (Sd(0, 2) * Sd(1, 0) - Sd(0, 0) * Sd(1, 2)) * d;
562 t[6] = (Sd(1, 0) * Sd(2, 1) - Sd(1, 1) * Sd(2, 0)) * d;
563 t[7] = (Sd(0, 1) * Sd(2, 0) - Sd(0, 0) * Sd(2, 1)) * d;
564 t[8] = (Sd(0, 0) * Sd(1, 1) - Sd(0, 1) * Sd(1, 0)) * d;
579 void warpAffine_gpu(const oclMat &src, oclMat &dst, F coeffs[2][3], int interpolation)
581 CV_Assert( (src.oclchannels() == dst.oclchannels()) );
582 int srcStep = src.step1();
583 int dstStep = dst.step1();
584 float float_coeffs[2][3];
587 Context *clCxt = src.clCxt;
588 string s[3] = {"NN", "Linear", "Cubic"};
589 string kernelName = "warpAffine" + s[interpolation];
591 if (src.clCxt->supportsFeature(FEATURE_CL_DOUBLE))
594 coeffs_cm = clCreateBuffer(*(cl_context*)clCxt->getOpenCLContextPtr(), CL_MEM_READ_WRITE, sizeof(F) * 2 * 3, NULL, &st );
595 openCLVerifyCall(st);
596 openCLSafeCall(clEnqueueWriteBuffer(*(cl_command_queue*)clCxt->getOpenCLCommandQueuePtr(), (cl_mem)coeffs_cm, 1, 0,
597 sizeof(F) * 2 * 3, coeffs, 0, 0, 0));
602 for(int m = 0; m < 2; m++)
603 for(int n = 0; n < 3; n++)
604 float_coeffs[m][n] = coeffs[m][n];
606 coeffs_cm = clCreateBuffer(*(cl_context*)clCxt->getOpenCLContextPtr(), CL_MEM_READ_WRITE, sizeof(float) * 2 * 3, NULL, &st );
607 openCLSafeCall(clEnqueueWriteBuffer(*(cl_command_queue*)clCxt->getOpenCLCommandQueuePtr(), (cl_mem)coeffs_cm,
608 1, 0, sizeof(float) * 2 * 3, float_coeffs, 0, 0, 0));
611 //TODO: improve this kernel
612 size_t blkSizeX = 16, blkSizeY = 16;
616 if (src.type() == CV_8UC1 && interpolation != 2)
618 cols = (dst.cols + dst.offset % 4 + 3) / 4;
619 glbSizeX = cols % blkSizeX == 0 ? cols : (cols / blkSizeX + 1) * blkSizeX;
624 glbSizeX = dst.cols % blkSizeX == 0 ? dst.cols : (dst.cols / blkSizeX + 1) * blkSizeX;
627 size_t glbSizeY = dst.rows % blkSizeY == 0 ? dst.rows : (dst.rows / blkSizeY + 1) * blkSizeY;
628 size_t globalThreads[3] = {glbSizeX, glbSizeY, 1};
629 size_t localThreads[3] = {blkSizeX, blkSizeY, 1};
631 vector< pair<size_t, const void *> > args;
633 args.push_back(make_pair(sizeof(cl_mem), (void *)&src.data));
634 args.push_back(make_pair(sizeof(cl_mem), (void *)&dst.data));
635 args.push_back(make_pair(sizeof(cl_int), (void *)&src.cols));
636 args.push_back(make_pair(sizeof(cl_int), (void *)&src.rows));
637 args.push_back(make_pair(sizeof(cl_int), (void *)&dst.cols));
638 args.push_back(make_pair(sizeof(cl_int), (void *)&dst.rows));
639 args.push_back(make_pair(sizeof(cl_int), (void *)&srcStep));
640 args.push_back(make_pair(sizeof(cl_int), (void *)&dstStep));
641 args.push_back(make_pair(sizeof(cl_int), (void *)&src.offset));
642 args.push_back(make_pair(sizeof(cl_int), (void *)&dst.offset));
643 args.push_back(make_pair(sizeof(cl_mem), (void *)&coeffs_cm));
644 args.push_back(make_pair(sizeof(cl_int), (void *)&cols));
646 openCLExecuteKernel(clCxt, &imgproc_warpAffine, kernelName, globalThreads, localThreads, args, src.oclchannels(), src.depth());
647 openCLSafeCall(clReleaseMemObject(coeffs_cm));
650 void warpPerspective_gpu(const oclMat &src, oclMat &dst, double coeffs[3][3], int interpolation)
652 CV_Assert( (src.oclchannels() == dst.oclchannels()) );
653 int srcStep = src.step1();
654 int dstStep = dst.step1();
655 float float_coeffs[3][3];
658 Context *clCxt = src.clCxt;
659 string s[3] = {"NN", "Linear", "Cubic"};
660 string kernelName = "warpPerspective" + s[interpolation];
662 if (src.clCxt->supportsFeature(FEATURE_CL_DOUBLE))
665 coeffs_cm = clCreateBuffer(*(cl_context*)clCxt->getOpenCLContextPtr(), CL_MEM_READ_WRITE, sizeof(double) * 3 * 3, NULL, &st );
666 openCLVerifyCall(st);
667 openCLSafeCall(clEnqueueWriteBuffer(*(cl_command_queue*)clCxt->getOpenCLCommandQueuePtr(), (cl_mem)coeffs_cm, 1, 0,
668 sizeof(double) * 3 * 3, coeffs, 0, 0, 0));
673 for(int m = 0; m < 3; m++)
674 for(int n = 0; n < 3; n++)
675 float_coeffs[m][n] = coeffs[m][n];
677 coeffs_cm = clCreateBuffer(*(cl_context*)clCxt->getOpenCLContextPtr(), CL_MEM_READ_WRITE, sizeof(float) * 3 * 3, NULL, &st );
678 openCLVerifyCall(st);
679 openCLSafeCall(clEnqueueWriteBuffer(*(cl_command_queue*)clCxt->getOpenCLCommandQueuePtr(), (cl_mem)coeffs_cm, 1, 0,
680 sizeof(float) * 3 * 3, float_coeffs, 0, 0, 0));
683 //TODO: improve this kernel
684 size_t blkSizeX = 16, blkSizeY = 16;
687 if (src.type() == CV_8UC1 && interpolation == 0)
689 cols = (dst.cols + dst.offset % 4 + 3) / 4;
690 glbSizeX = cols % blkSizeX == 0 ? cols : (cols / blkSizeX + 1) * blkSizeX;
695 glbSizeX = dst.cols % blkSizeX == 0 ? dst.cols : (dst.cols / blkSizeX + 1) * blkSizeX;
698 size_t glbSizeY = dst.rows % blkSizeY == 0 ? dst.rows : (dst.rows / blkSizeY + 1) * blkSizeY;
699 size_t globalThreads[3] = {glbSizeX, glbSizeY, 1};
700 size_t localThreads[3] = {blkSizeX, blkSizeY, 1};
702 vector< pair<size_t, const void *> > args;
704 args.push_back(make_pair(sizeof(cl_mem), (void *)&src.data));
705 args.push_back(make_pair(sizeof(cl_mem), (void *)&dst.data));
706 args.push_back(make_pair(sizeof(cl_int), (void *)&src.cols));
707 args.push_back(make_pair(sizeof(cl_int), (void *)&src.rows));
708 args.push_back(make_pair(sizeof(cl_int), (void *)&dst.cols));
709 args.push_back(make_pair(sizeof(cl_int), (void *)&dst.rows));
710 args.push_back(make_pair(sizeof(cl_int), (void *)&srcStep));
711 args.push_back(make_pair(sizeof(cl_int), (void *)&dstStep));
712 args.push_back(make_pair(sizeof(cl_int), (void *)&src.offset));
713 args.push_back(make_pair(sizeof(cl_int), (void *)&dst.offset));
714 args.push_back(make_pair(sizeof(cl_mem), (void *)&coeffs_cm));
715 args.push_back(make_pair(sizeof(cl_int), (void *)&cols));
717 openCLExecuteKernel(clCxt, &imgproc_warpPerspective, kernelName, globalThreads, localThreads, args, src.oclchannels(), src.depth());
718 openCLSafeCall(clReleaseMemObject(coeffs_cm));
722 void warpAffine(const oclMat &src, oclMat &dst, const Mat &M, Size dsize, int flags)
724 int interpolation = flags & INTER_MAX;
726 CV_Assert((src.depth() == CV_8U || src.depth() == CV_32F) && src.oclchannels() != 2 && src.oclchannels() != 3);
727 CV_Assert(interpolation == INTER_NEAREST || interpolation == INTER_LINEAR || interpolation == INTER_CUBIC);
729 dst.create(dsize, src.type());
731 CV_Assert(M.rows == 2 && M.cols == 3);
733 int warpInd = (flags & WARP_INVERSE_MAP) >> 4;
737 Mat coeffsMat(2, 3, CV_64F, (void *)coeffsM);
738 M.convertTo(coeffsMat, coeffsMat.type());
740 convert_coeffs(coeffsM);
742 for(int i = 0; i < 2; ++i)
743 for(int j = 0; j < 3; ++j)
744 coeffs[i][j] = coeffsM[i*3+j];
746 warpAffine_gpu(src, dst, coeffs, interpolation);
749 void warpPerspective(const oclMat &src, oclMat &dst, const Mat &M, Size dsize, int flags)
751 int interpolation = flags & INTER_MAX;
753 CV_Assert((src.depth() == CV_8U || src.depth() == CV_32F) && src.oclchannels() != 2 && src.oclchannels() != 3);
754 CV_Assert(interpolation == INTER_NEAREST || interpolation == INTER_LINEAR || interpolation == INTER_CUBIC);
756 dst.create(dsize, src.type());
759 CV_Assert(M.rows == 3 && M.cols == 3);
761 int warpInd = (flags & WARP_INVERSE_MAP) >> 4;
765 Mat coeffsMat(3, 3, CV_64F, (void *)coeffsM);
766 M.convertTo(coeffsMat, coeffsMat.type());
770 for(int i = 0; i < 3; ++i)
771 for(int j = 0; j < 3; ++j)
772 coeffs[i][j] = coeffsM[i*3+j];
774 warpPerspective_gpu(src, dst, coeffs, interpolation);
777 ////////////////////////////////////////////////////////////////////////
780 void integral(const oclMat &src, oclMat &sum, oclMat &sqsum)
782 CV_Assert(src.type() == CV_8UC1);
783 if (!src.clCxt->supportsFeature(ocl::FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
785 CV_Error(CV_OpenCLDoubleNotSupported, "Select device doesn't support double");
790 int offset = src.offset / vlen;
791 int pre_invalid = src.offset % vlen;
792 int vcols = (pre_invalid + src.cols + vlen - 1) / vlen;
794 oclMat t_sum , t_sqsum;
795 int w = src.cols + 1, h = src.rows + 1;
796 int depth = src.depth() == CV_8U ? CV_32S : CV_64F;
797 int type = CV_MAKE_TYPE(depth, 1);
799 t_sum.create(src.cols, src.rows, type);
800 sum.create(h, w, type);
802 t_sqsum.create(src.cols, src.rows, CV_32FC1);
803 sqsum.create(h, w, CV_32FC1);
805 int sum_offset = sum.offset / vlen;
806 int sqsum_offset = sqsum.offset / vlen;
808 vector<pair<size_t , const void *> > args;
809 args.push_back( make_pair( sizeof(cl_mem) , (void *)&src.data ));
810 args.push_back( make_pair( sizeof(cl_mem) , (void *)&t_sum.data ));
811 args.push_back( make_pair( sizeof(cl_mem) , (void *)&t_sqsum.data ));
812 args.push_back( make_pair( sizeof(cl_int) , (void *)&offset ));
813 args.push_back( make_pair( sizeof(cl_int) , (void *)&pre_invalid ));
814 args.push_back( make_pair( sizeof(cl_int) , (void *)&src.rows ));
815 args.push_back( make_pair( sizeof(cl_int) , (void *)&src.cols ));
816 args.push_back( make_pair( sizeof(cl_int) , (void *)&src.step ));
817 args.push_back( make_pair( sizeof(cl_int) , (void *)&t_sum.step));
818 size_t gt[3] = {((vcols + 1) / 2) * 256, 1, 1}, lt[3] = {256, 1, 1};
819 openCLExecuteKernel(src.clCxt, &imgproc_integral, "integral_cols", gt, lt, args, -1, depth);
822 args.push_back( make_pair( sizeof(cl_mem) , (void *)&t_sum.data ));
823 args.push_back( make_pair( sizeof(cl_mem) , (void *)&t_sqsum.data ));
824 args.push_back( make_pair( sizeof(cl_mem) , (void *)&sum.data ));
825 args.push_back( make_pair( sizeof(cl_mem) , (void *)&sqsum.data ));
826 args.push_back( make_pair( sizeof(cl_int) , (void *)&t_sum.rows ));
827 args.push_back( make_pair( sizeof(cl_int) , (void *)&t_sum.cols ));
828 args.push_back( make_pair( sizeof(cl_int) , (void *)&t_sum.step ));
829 args.push_back( make_pair( sizeof(cl_int) , (void *)&sum.step));
830 args.push_back( make_pair( sizeof(cl_int) , (void *)&sqsum.step));
831 args.push_back( make_pair( sizeof(cl_int) , (void *)&sum_offset));
832 args.push_back( make_pair( sizeof(cl_int) , (void *)&sqsum_offset));
833 size_t gt2[3] = {t_sum.cols * 32, 1, 1}, lt2[3] = {256, 1, 1};
834 openCLExecuteKernel(src.clCxt, &imgproc_integral, "integral_rows", gt2, lt2, args, -1, depth);
837 void integral(const oclMat &src, oclMat &sum)
839 CV_Assert(src.type() == CV_8UC1);
841 int offset = src.offset / vlen;
842 int pre_invalid = src.offset % vlen;
843 int vcols = (pre_invalid + src.cols + vlen - 1) / vlen;
846 int w = src.cols + 1, h = src.rows + 1;
847 int depth = src.depth() == CV_8U ? CV_32S : CV_32F;
848 int type = CV_MAKE_TYPE(depth, 1);
850 t_sum.create(src.cols, src.rows, type);
851 sum.create(h, w, type);
853 int sum_offset = sum.offset / vlen;
854 vector<pair<size_t , const void *> > args;
855 args.push_back( make_pair( sizeof(cl_mem) , (void *)&src.data ));
856 args.push_back( make_pair( sizeof(cl_mem) , (void *)&t_sum.data ));
857 args.push_back( make_pair( sizeof(cl_int) , (void *)&offset ));
858 args.push_back( make_pair( sizeof(cl_int) , (void *)&pre_invalid ));
859 args.push_back( make_pair( sizeof(cl_int) , (void *)&src.rows ));
860 args.push_back( make_pair( sizeof(cl_int) , (void *)&src.cols ));
861 args.push_back( make_pair( sizeof(cl_int) , (void *)&src.step ));
862 args.push_back( make_pair( sizeof(cl_int) , (void *)&t_sum.step));
863 size_t gt[3] = {((vcols + 1) / 2) * 256, 1, 1}, lt[3] = {256, 1, 1};
864 openCLExecuteKernel(src.clCxt, &imgproc_integral_sum, "integral_sum_cols", gt, lt, args, -1, depth);
867 args.push_back( make_pair( sizeof(cl_mem) , (void *)&t_sum.data ));
868 args.push_back( make_pair( sizeof(cl_mem) , (void *)&sum.data ));
869 args.push_back( make_pair( sizeof(cl_int) , (void *)&t_sum.rows ));
870 args.push_back( make_pair( sizeof(cl_int) , (void *)&t_sum.cols ));
871 args.push_back( make_pair( sizeof(cl_int) , (void *)&t_sum.step ));
872 args.push_back( make_pair( sizeof(cl_int) , (void *)&sum.step));
873 args.push_back( make_pair( sizeof(cl_int) , (void *)&sum_offset));
874 size_t gt2[3] = {t_sum.cols * 32, 1, 1}, lt2[3] = {256, 1, 1};
875 openCLExecuteKernel(src.clCxt, &imgproc_integral_sum, "integral_sum_rows", gt2, lt2, args, -1, depth);
878 /////////////////////// corner //////////////////////////////
880 static void extractCovData(const oclMat &src, oclMat &Dx, oclMat &Dy,
881 int blockSize, int ksize, int borderType)
883 CV_Assert(src.type() == CV_8UC1 || src.type() == CV_32FC1);
884 double scale = static_cast<double>(1 << ((ksize > 0 ? ksize : 3) - 1)) * blockSize;
888 if (src.depth() == CV_8U)
898 Context* clCxt = Context::getContext();
899 if(clCxt->supportsFeature(FEATURE_CL_INTEL_DEVICE) && src.type() == CV_8UC1 &&
900 src.cols % 8 == 0 && src.rows % 8 == 0 &&
902 (borderType ==cv::BORDER_REFLECT ||
903 borderType == cv::BORDER_REPLICATE ||
904 borderType ==cv::BORDER_REFLECT101 ||
905 borderType ==cv::BORDER_WRAP))
907 Dx.create(src.size(), CV_32FC1);
908 Dy.create(src.size(), CV_32FC1);
910 const unsigned int block_x = 8;
911 const unsigned int block_y = 8;
913 unsigned int src_pitch = src.step;
914 unsigned int dst_pitch = Dx.cols;
916 float _scale = scale;
918 std::vector<std::pair<size_t , const void *> > args;
919 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&src.data ));
920 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&Dx.data ));
921 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&Dy.data ));
922 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.cols ));
923 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.rows ));
924 args.push_back( std::make_pair( sizeof(cl_uint) , (void *)&src_pitch ));
925 args.push_back( std::make_pair( sizeof(cl_uint) , (void *)&dst_pitch ));
926 args.push_back( std::make_pair( sizeof(cl_float) , (void *)&_scale ));
927 size_t gt2[3] = {src.cols, src.rows, 1}, lt2[3] = {block_x, block_y, 1};
929 string option = "-D BLK_X=8 -D BLK_Y=8";
932 case cv::BORDER_REPLICATE:
933 option += " -D BORDER_REPLICATE";
935 case cv::BORDER_REFLECT:
936 option += " -D BORDER_REFLECT";
938 case cv::BORDER_REFLECT101:
939 option += " -D BORDER_REFLECT101";
941 case cv::BORDER_WRAP:
942 option += " -D BORDER_WRAP";
945 openCLExecuteKernel(src.clCxt, &imgproc_sobel3, "sobel3", gt2, lt2, args, -1, -1, option.c_str() );
949 Sobel(src, Dx, CV_32F, 1, 0, ksize, scale, 0, borderType);
950 Sobel(src, Dy, CV_32F, 0, 1, ksize, scale, 0, borderType);
955 Scharr(src, Dx, CV_32F, 1, 0, scale, 0, borderType);
956 Scharr(src, Dy, CV_32F, 0, 1, scale, 0, borderType);
958 CV_Assert(Dx.offset == 0 && Dy.offset == 0);
961 static void corner_ocl(const cv::ocl::ProgramEntry* source, string kernelName, int block_size, float k, oclMat &Dx, oclMat &Dy,
962 oclMat &dst, int border_type)
967 case cv::BORDER_CONSTANT:
968 sprintf(borderType, "BORDER_CONSTANT");
970 case cv::BORDER_REFLECT101:
971 sprintf(borderType, "BORDER_REFLECT101");
973 case cv::BORDER_REFLECT:
974 sprintf(borderType, "BORDER_REFLECT");
976 case cv::BORDER_REPLICATE:
977 sprintf(borderType, "BORDER_REPLICATE");
980 CV_Error(CV_StsBadFlag, "BORDER type is not supported!");
983 std::string buildOptions = format("-D anX=%d -D anY=%d -D ksX=%d -D ksY=%d -D %s",
984 block_size / 2, block_size / 2, block_size, block_size, borderType);
986 size_t blockSizeX = 256, blockSizeY = 1;
987 size_t gSize = blockSizeX - block_size / 2 * 2;
988 size_t globalSizeX = (Dx.cols) % gSize == 0 ? Dx.cols / gSize * blockSizeX : (Dx.cols / gSize + 1) * blockSizeX;
989 size_t rows_per_thread = 2;
990 size_t globalSizeY = ((Dx.rows + rows_per_thread - 1) / rows_per_thread) % blockSizeY == 0 ?
991 ((Dx.rows + rows_per_thread - 1) / rows_per_thread) :
992 (((Dx.rows + rows_per_thread - 1) / rows_per_thread) / blockSizeY + 1) * blockSizeY;
994 size_t gt[3] = { globalSizeX, globalSizeY, 1 };
995 size_t lt[3] = { blockSizeX, blockSizeY, 1 };
996 vector<pair<size_t , const void *> > args;
997 args.push_back( make_pair( sizeof(cl_mem) , (void *)&Dx.data ));
998 args.push_back( make_pair( sizeof(cl_mem) , (void *)&Dy.data));
999 args.push_back( make_pair( sizeof(cl_mem) , (void *)&dst.data));
1000 args.push_back( make_pair( sizeof(cl_int) , (void *)&Dx.offset ));
1001 args.push_back( make_pair( sizeof(cl_int) , (void *)&Dx.wholerows ));
1002 args.push_back( make_pair( sizeof(cl_int) , (void *)&Dx.wholecols ));
1003 args.push_back( make_pair(sizeof(cl_int), (void *)&Dx.step));
1004 args.push_back( make_pair( sizeof(cl_int) , (void *)&Dy.offset ));
1005 args.push_back( make_pair( sizeof(cl_int) , (void *)&Dy.wholerows ));
1006 args.push_back( make_pair( sizeof(cl_int) , (void *)&Dy.wholecols ));
1007 args.push_back( make_pair(sizeof(cl_int), (void *)&Dy.step));
1008 args.push_back( make_pair(sizeof(cl_int), (void *)&dst.offset));
1009 args.push_back( make_pair(sizeof(cl_int), (void *)&dst.rows));
1010 args.push_back( make_pair(sizeof(cl_int), (void *)&dst.cols));
1011 args.push_back( make_pair(sizeof(cl_int), (void *)&dst.step));
1012 args.push_back( make_pair( sizeof(cl_float) , (void *)&k));
1014 openCLExecuteKernel(dst.clCxt, source, kernelName, gt, lt, args, -1, -1, buildOptions.c_str());
1017 void cornerHarris(const oclMat &src, oclMat &dst, int blockSize, int ksize,
1018 double k, int borderType)
1021 cornerHarris_dxdy(src, dst, dx, dy, blockSize, ksize, k, borderType);
1024 void cornerHarris_dxdy(const oclMat &src, oclMat &dst, oclMat &dx, oclMat &dy, int blockSize, int ksize,
1025 double k, int borderType)
1027 if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
1029 CV_Error(CV_OpenCLDoubleNotSupported, "Selected device doesn't support double");
1033 CV_Assert(borderType == cv::BORDER_CONSTANT || borderType == cv::BORDER_REFLECT101 || borderType == cv::BORDER_REPLICATE
1034 || borderType == cv::BORDER_REFLECT);
1036 extractCovData(src, dx, dy, blockSize, ksize, borderType);
1037 dst.create(src.size(), CV_32FC1);
1038 corner_ocl(&imgproc_calcHarris, "calcHarris", blockSize, static_cast<float>(k), dx, dy, dst, borderType);
1041 void cornerMinEigenVal(const oclMat &src, oclMat &dst, int blockSize, int ksize, int borderType)
1044 cornerMinEigenVal_dxdy(src, dst, dx, dy, blockSize, ksize, borderType);
1047 void cornerMinEigenVal_dxdy(const oclMat &src, oclMat &dst, oclMat &dx, oclMat &dy, int blockSize, int ksize, int borderType)
1049 if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
1051 CV_Error(CV_OpenCLDoubleNotSupported, "Selected device doesn't support double");
1055 CV_Assert(borderType == cv::BORDER_CONSTANT || borderType == cv::BORDER_REFLECT101 ||
1056 borderType == cv::BORDER_REPLICATE || borderType == cv::BORDER_REFLECT);
1058 extractCovData(src, dx, dy, blockSize, ksize, borderType);
1059 dst.create(src.size(), CV_32F);
1061 corner_ocl(&imgproc_calcMinEigenVal, "calcMinEigenVal", blockSize, 0, dx, dy, dst, borderType);
1064 /////////////////////////////////// MeanShiftfiltering ///////////////////////////////////////////////
1066 static void meanShiftFiltering_gpu(const oclMat &src, oclMat dst, int sp, int sr, int maxIter, float eps)
1068 CV_Assert( (src.cols == dst.cols) && (src.rows == dst.rows) );
1069 CV_Assert( !(dst.step & 0x3) );
1071 //Arrange the NDRange
1072 int col = src.cols, row = src.rows;
1073 int ltx = 16, lty = 8;
1074 if (src.cols % ltx != 0)
1075 col = (col / ltx + 1) * ltx;
1076 if (src.rows % lty != 0)
1077 row = (row / lty + 1) * lty;
1079 size_t globalThreads[3] = {col, row, 1};
1080 size_t localThreads[3] = {ltx, lty, 1};
1083 vector<pair<size_t , const void *> > args;
1084 args.push_back( make_pair( sizeof(cl_mem) , (void *)&dst.data ));
1085 args.push_back( make_pair( sizeof(cl_int) , (void *)&dst.step ));
1086 args.push_back( make_pair( sizeof(cl_mem) , (void *)&src.data ));
1087 args.push_back( make_pair( sizeof(cl_int) , (void *)&src.step ));
1088 args.push_back( make_pair( sizeof(cl_int) , (void *)&dst.offset ));
1089 args.push_back( make_pair( sizeof(cl_int) , (void *)&src.offset ));
1090 args.push_back( make_pair( sizeof(cl_int) , (void *)&dst.cols ));
1091 args.push_back( make_pair( sizeof(cl_int) , (void *)&dst.rows ));
1092 args.push_back( make_pair( sizeof(cl_int) , (void *)&sp ));
1093 args.push_back( make_pair( sizeof(cl_int) , (void *)&sr ));
1094 args.push_back( make_pair( sizeof(cl_int) , (void *)&maxIter ));
1095 args.push_back( make_pair( sizeof(cl_float) , (void *)&eps ));
1097 openCLExecuteKernel(src.clCxt, &meanShift, "meanshift_kernel", globalThreads, localThreads, args, -1, -1);
1100 void meanShiftFiltering(const oclMat &src, oclMat &dst, int sp, int sr, TermCriteria criteria)
1103 CV_Error( CV_StsBadArg, "The input image is empty" );
1105 if ( src.depth() != CV_8U || src.oclchannels() != 4 )
1106 CV_Error( CV_StsUnsupportedFormat, "Only 8-bit, 4-channel images are supported" );
1108 dst.create( src.size(), CV_8UC4 );
1110 if ( !(criteria.type & TermCriteria::MAX_ITER) )
1111 criteria.maxCount = 5;
1113 int maxIter = std::min(std::max(criteria.maxCount, 1), 100);
1116 if ( !(criteria.type & TermCriteria::EPS) )
1118 eps = (float)std::max(criteria.epsilon, 0.0);
1120 meanShiftFiltering_gpu(src, dst, sp, sr, maxIter, eps);
1123 static void meanShiftProc_gpu(const oclMat &src, oclMat dstr, oclMat dstsp, int sp, int sr, int maxIter, float eps)
1126 CV_Assert( (src.cols == dstr.cols) && (src.rows == dstr.rows) &&
1127 (src.rows == dstsp.rows) && (src.cols == dstsp.cols));
1128 CV_Assert( !(dstsp.step & 0x3) );
1130 //Arrange the NDRange
1131 int col = src.cols, row = src.rows;
1132 int ltx = 16, lty = 8;
1133 if (src.cols % ltx != 0)
1134 col = (col / ltx + 1) * ltx;
1135 if (src.rows % lty != 0)
1136 row = (row / lty + 1) * lty;
1138 size_t globalThreads[3] = {col, row, 1};
1139 size_t localThreads[3] = {ltx, lty, 1};
1142 vector<pair<size_t , const void *> > args;
1143 args.push_back( make_pair( sizeof(cl_mem) , (void *)&src.data ));
1144 args.push_back( make_pair( sizeof(cl_mem) , (void *)&dstr.data ));
1145 args.push_back( make_pair( sizeof(cl_mem) , (void *)&dstsp.data ));
1146 args.push_back( make_pair( sizeof(cl_int) , (void *)&src.step ));
1147 args.push_back( make_pair( sizeof(cl_int) , (void *)&dstr.step ));
1148 args.push_back( make_pair( sizeof(cl_int) , (void *)&dstsp.step ));
1149 args.push_back( make_pair( sizeof(cl_int) , (void *)&src.offset ));
1150 args.push_back( make_pair( sizeof(cl_int) , (void *)&dstr.offset ));
1151 args.push_back( make_pair( sizeof(cl_int) , (void *)&dstsp.offset ));
1152 args.push_back( make_pair( sizeof(cl_int) , (void *)&dstr.cols ));
1153 args.push_back( make_pair( sizeof(cl_int) , (void *)&dstr.rows ));
1154 args.push_back( make_pair( sizeof(cl_int) , (void *)&sp ));
1155 args.push_back( make_pair( sizeof(cl_int) , (void *)&sr ));
1156 args.push_back( make_pair( sizeof(cl_int) , (void *)&maxIter ));
1157 args.push_back( make_pair( sizeof(cl_float) , (void *)&eps ));
1159 openCLExecuteKernel(src.clCxt, &meanShift, "meanshiftproc_kernel", globalThreads, localThreads, args, -1, -1);
1162 void meanShiftProc(const oclMat &src, oclMat &dstr, oclMat &dstsp, int sp, int sr, TermCriteria criteria)
1165 CV_Error( CV_StsBadArg, "The input image is empty" );
1167 if ( src.depth() != CV_8U || src.oclchannels() != 4 )
1168 CV_Error( CV_StsUnsupportedFormat, "Only 8-bit, 4-channel images are supported" );
1170 // if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE))
1172 // CV_Error( CV_OpenCLDoubleNotSupportedNotSupported, "Selected device doesn't support double, so a deviation exists.\nIf the accuracy is acceptable, the error can be ignored.\n");
1176 dstr.create( src.size(), CV_8UC4 );
1177 dstsp.create( src.size(), CV_16SC2 );
1179 if ( !(criteria.type & TermCriteria::MAX_ITER) )
1180 criteria.maxCount = 5;
1182 int maxIter = std::min(std::max(criteria.maxCount, 1), 100);
1185 if ( !(criteria.type & TermCriteria::EPS) )
1187 eps = (float)std::max(criteria.epsilon, 0.0);
1189 meanShiftProc_gpu(src, dstr, dstsp, sp, sr, maxIter, eps);
1192 ///////////////////////////////////////////////////////////////////////////////////////////////////
1193 ////////////////////////////////////////////////////hist///////////////////////////////////////////////
1194 /////////////////////////////////////////////////////////////////////////////////////////////////////
1196 namespace histograms
1198 const int PARTIAL_HISTOGRAM256_COUNT = 256;
1199 const int HISTOGRAM256_BIN_COUNT = 256;
1201 ///////////////////////////////calcHist/////////////////////////////////////////////////////////////////
1202 static void calc_sub_hist(const oclMat &mat_src, const oclMat &mat_sub_hist)
1204 using namespace histograms;
1206 int depth = mat_src.depth();
1208 size_t localThreads[3] = { HISTOGRAM256_BIN_COUNT, 1, 1 };
1209 size_t globalThreads[3] = { PARTIAL_HISTOGRAM256_COUNT *localThreads[0], 1, 1};
1212 int dataWidth_bits = 4;
1213 int mask = dataWidth - 1;
1215 int cols = mat_src.cols * mat_src.oclchannels();
1216 int src_offset = mat_src.offset;
1217 int hist_step = mat_sub_hist.step >> 2;
1218 int left_col = 0, right_col = 0;
1220 if (cols >= dataWidth * 2 - 1)
1222 left_col = dataWidth - (src_offset & mask);
1224 src_offset += left_col;
1226 right_col = cols & mask;
1234 globalThreads[0] = 0;
1237 vector<pair<size_t , const void *> > args;
1238 if (globalThreads[0] != 0)
1240 int tempcols = cols >> dataWidth_bits;
1241 int inc_x = globalThreads[0] % tempcols;
1242 int inc_y = globalThreads[0] / tempcols;
1243 src_offset >>= dataWidth_bits;
1244 int src_step = mat_src.step >> dataWidth_bits;
1245 int datacount = tempcols * mat_src.rows;
1247 args.push_back( make_pair( sizeof(cl_mem), (void *)&mat_src.data));
1248 args.push_back( make_pair( sizeof(cl_int), (void *)&src_step));
1249 args.push_back( make_pair( sizeof(cl_int), (void *)&src_offset));
1250 args.push_back( make_pair( sizeof(cl_mem), (void *)&mat_sub_hist.data));
1251 args.push_back( make_pair( sizeof(cl_int), (void *)&datacount));
1252 args.push_back( make_pair( sizeof(cl_int), (void *)&tempcols));
1253 args.push_back( make_pair( sizeof(cl_int), (void *)&inc_x));
1254 args.push_back( make_pair( sizeof(cl_int), (void *)&inc_y));
1255 args.push_back( make_pair( sizeof(cl_int), (void *)&hist_step));
1257 openCLExecuteKernel(mat_src.clCxt, &imgproc_histogram, "calc_sub_hist", globalThreads, localThreads, args, -1, depth);
1260 if (left_col != 0 || right_col != 0)
1262 src_offset = mat_src.offset;
1263 localThreads[0] = 1;
1264 localThreads[1] = 256;
1265 globalThreads[0] = left_col + right_col;
1266 globalThreads[1] = mat_src.rows;
1269 args.push_back( make_pair( sizeof(cl_mem), (void *)&mat_src.data));
1270 args.push_back( make_pair( sizeof(cl_int), (void *)&mat_src.step));
1271 args.push_back( make_pair( sizeof(cl_int), (void *)&src_offset));
1272 args.push_back( make_pair( sizeof(cl_mem), (void *)&mat_sub_hist.data));
1273 args.push_back( make_pair( sizeof(cl_int), (void *)&left_col));
1274 args.push_back( make_pair( sizeof(cl_int), (void *)&cols));
1275 args.push_back( make_pair( sizeof(cl_int), (void *)&mat_src.rows));
1276 args.push_back( make_pair( sizeof(cl_int), (void *)&hist_step));
1278 openCLExecuteKernel(mat_src.clCxt, &imgproc_histogram, "calc_sub_hist_border", globalThreads, localThreads, args, -1, depth);
1282 static void merge_sub_hist(const oclMat &sub_hist, oclMat &mat_hist)
1284 using namespace histograms;
1286 size_t localThreads[3] = { 256, 1, 1 };
1287 size_t globalThreads[3] = { HISTOGRAM256_BIN_COUNT *localThreads[0], 1, 1};
1288 int src_step = sub_hist.step >> 2;
1290 vector<pair<size_t , const void *> > args;
1291 args.push_back( make_pair( sizeof(cl_mem), (void *)&sub_hist.data));
1292 args.push_back( make_pair( sizeof(cl_mem), (void *)&mat_hist.data));
1293 args.push_back( make_pair( sizeof(cl_int), (void *)&src_step));
1295 openCLExecuteKernel(sub_hist.clCxt, &imgproc_histogram, "merge_hist", globalThreads, localThreads, args, -1, -1);
1298 void calcHist(const oclMat &mat_src, oclMat &mat_hist)
1300 using namespace histograms;
1301 CV_Assert(mat_src.type() == CV_8UC1);
1302 mat_hist.create(1, 256, CV_32SC1);
1304 oclMat buf(PARTIAL_HISTOGRAM256_COUNT, HISTOGRAM256_BIN_COUNT, CV_32SC1);
1307 calc_sub_hist(mat_src, buf);
1308 merge_sub_hist(buf, mat_hist);
1311 ///////////////////////////////////equalizeHist/////////////////////////////////////////////////////
1312 void equalizeHist(const oclMat &mat_src, oclMat &mat_dst)
1314 mat_dst.create(mat_src.rows, mat_src.cols, CV_8UC1);
1316 oclMat mat_hist(1, 256, CV_32SC1);
1318 calcHist(mat_src, mat_hist);
1320 size_t localThreads[3] = { 256, 1, 1};
1321 size_t globalThreads[3] = { 256, 1, 1};
1322 oclMat lut(1, 256, CV_8UC1);
1323 int total = mat_src.rows * mat_src.cols;
1325 vector<pair<size_t , const void *> > args;
1326 args.push_back( make_pair( sizeof(cl_mem), (void *)&lut.data));
1327 args.push_back( make_pair( sizeof(cl_mem), (void *)&mat_hist.data));
1328 args.push_back( make_pair( sizeof(int), (void *)&total));
1330 openCLExecuteKernel(mat_src.clCxt, &imgproc_histogram, "calLUT", globalThreads, localThreads, args, -1, -1);
1331 LUT(mat_src, lut, mat_dst);
1334 ////////////////////////////////////////////////////////////////////////
1338 static void calcLut(const oclMat &src, oclMat &dst,
1339 const int tilesX, const int tilesY, const cv::Size tileSize,
1340 const int clipLimit, const float lutScale)
1343 tile_size.s[0] = tileSize.width;
1344 tile_size.s[1] = tileSize.height;
1346 std::vector<pair<size_t , const void *> > args;
1347 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&src.data ));
1348 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst.data ));
1349 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.step ));
1350 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.step ));
1351 args.push_back( std::make_pair( sizeof(cl_int2), (void *)&tile_size ));
1352 args.push_back( std::make_pair( sizeof(cl_int), (void *)&tilesX ));
1353 args.push_back( std::make_pair( sizeof(cl_int), (void *)&clipLimit ));
1354 args.push_back( std::make_pair( sizeof(cl_float), (void *)&lutScale ));
1355 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.offset ));
1356 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.offset ));
1358 String kernelName = "calcLut";
1359 size_t localThreads[3] = { 32, 8, 1 };
1360 size_t globalThreads[3] = { tilesX * localThreads[0], tilesY * localThreads[1], 1 };
1361 bool is_cpu = isCpuDevice();
1363 openCLExecuteKernel(Context::getContext(), &imgproc_clahe, kernelName, globalThreads, localThreads, args, -1, -1, (char*)"-D CPU");
1366 cl_kernel kernel = openCLGetKernelFromSource(Context::getContext(), &imgproc_clahe, kernelName);
1367 int wave_size = (int)queryWaveFrontSize(kernel);
1368 openCLSafeCall(clReleaseKernel(kernel));
1370 std::string opt = format("-D WAVE_SIZE=%d", wave_size);
1371 openCLExecuteKernel(Context::getContext(), &imgproc_clahe, kernelName, globalThreads, localThreads, args, -1, -1, opt.c_str());
1375 static void transform(const oclMat &src, oclMat &dst, const oclMat &lut,
1376 const int tilesX, const int tilesY, const Size & tileSize)
1379 tile_size.s[0] = tileSize.width;
1380 tile_size.s[1] = tileSize.height;
1382 std::vector<pair<size_t , const void *> > args;
1383 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&src.data ));
1384 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst.data ));
1385 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&lut.data ));
1386 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.step ));
1387 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.step ));
1388 args.push_back( std::make_pair( sizeof(cl_int), (void *)&lut.step ));
1389 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.cols ));
1390 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.rows ));
1391 args.push_back( std::make_pair( sizeof(cl_int2), (void *)&tile_size ));
1392 args.push_back( std::make_pair( sizeof(cl_int), (void *)&tilesX ));
1393 args.push_back( std::make_pair( sizeof(cl_int), (void *)&tilesY ));
1394 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.offset ));
1395 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.offset ));
1396 args.push_back( std::make_pair( sizeof(cl_int), (void *)&lut.offset ));
1398 size_t localThreads[3] = { 32, 8, 1 };
1399 size_t globalThreads[3] = { src.cols, src.rows, 1 };
1401 openCLExecuteKernel(Context::getContext(), &imgproc_clahe, "transform", globalThreads, localThreads, args, -1, -1);
1407 class CLAHE_Impl : public cv::CLAHE
1410 CLAHE_Impl(double clipLimit = 40.0, int tilesX = 8, int tilesY = 8);
1412 cv::AlgorithmInfo* info() const;
1414 void apply(cv::InputArray src, cv::OutputArray dst);
1416 void setClipLimit(double clipLimit);
1417 double getClipLimit() const;
1419 void setTilesGridSize(cv::Size tileGridSize);
1420 cv::Size getTilesGridSize() const;
1422 void collectGarbage();
1433 CLAHE_Impl::CLAHE_Impl(double clipLimit, int tilesX, int tilesY) :
1434 clipLimit_(clipLimit), tilesX_(tilesX), tilesY_(tilesY)
1438 CV_INIT_ALGORITHM(CLAHE_Impl, "CLAHE_OCL",
1439 obj.info()->addParam(obj, "clipLimit", obj.clipLimit_);
1440 obj.info()->addParam(obj, "tilesX", obj.tilesX_);
1441 obj.info()->addParam(obj, "tilesY", obj.tilesY_))
1443 void CLAHE_Impl::apply(cv::InputArray src_raw, cv::OutputArray dst_raw)
1445 oclMat& src = getOclMatRef(src_raw);
1446 oclMat& dst = getOclMatRef(dst_raw);
1447 CV_Assert( src.type() == CV_8UC1 );
1449 dst.create( src.size(), src.type() );
1451 const int histSize = 256;
1453 ensureSizeIsEnough(tilesX_ * tilesY_, histSize, CV_8UC1, lut_);
1458 if (src.cols % tilesX_ == 0 && src.rows % tilesY_ == 0)
1460 tileSize = cv::Size(src.cols / tilesX_, src.rows / tilesY_);
1465 ocl::copyMakeBorder(src, srcExt_, 0, tilesY_ - (src.rows % tilesY_), 0,
1466 tilesX_ - (src.cols % tilesX_), BORDER_REFLECT_101, Scalar::all(0));
1468 tileSize = Size(srcExt_.cols / tilesX_, srcExt_.rows / tilesY_);
1469 srcForLut = srcExt_;
1472 const int tileSizeTotal = tileSize.area();
1473 const float lutScale = static_cast<float>(histSize - 1) / tileSizeTotal;
1476 if (clipLimit_ > 0.0)
1478 clipLimit = static_cast<int>(clipLimit_ * tileSizeTotal / histSize);
1479 clipLimit = std::max(clipLimit, 1);
1482 clahe::calcLut(srcForLut, lut_, tilesX_, tilesY_, tileSize, clipLimit, lutScale);
1483 clahe::transform(src, dst, lut_, tilesX_, tilesY_, tileSize);
1486 void CLAHE_Impl::setClipLimit(double clipLimit)
1488 clipLimit_ = clipLimit;
1491 double CLAHE_Impl::getClipLimit() const
1496 void CLAHE_Impl::setTilesGridSize(cv::Size tileGridSize)
1498 tilesX_ = tileGridSize.width;
1499 tilesY_ = tileGridSize.height;
1502 cv::Size CLAHE_Impl::getTilesGridSize() const
1504 return cv::Size(tilesX_, tilesY_);
1507 void CLAHE_Impl::collectGarbage()
1514 cv::Ptr<cv::CLAHE> createCLAHE(double clipLimit, cv::Size tileGridSize)
1516 return new CLAHE_Impl(clipLimit, tileGridSize.width, tileGridSize.height);
1519 //////////////////////////////////bilateralFilter////////////////////////////////////////////////////
1521 static void oclbilateralFilter_8u( const oclMat &src, oclMat &dst, int d,
1522 double sigma_color, double sigma_space,
1525 int cn = src.channels();
1526 int i, j, maxk, radius;
1528 CV_Assert( (src.channels() == 1 || src.channels() == 3) &&
1529 src.type() == dst.type() && src.size() == dst.size() &&
1530 src.data != dst.data );
1532 if ( sigma_color <= 0 )
1534 if ( sigma_space <= 0 )
1537 double gauss_color_coeff = -0.5 / (sigma_color * sigma_color);
1538 double gauss_space_coeff = -0.5 / (sigma_space * sigma_space);
1541 radius = cvRound(sigma_space * 1.5);
1544 radius = MAX(radius, 1);
1548 copyMakeBorder( src, temp, radius, radius, radius, radius, borderType );
1550 vector<float> _color_weight(cn * 256);
1551 vector<float> _space_weight(d * d);
1552 vector<int> _space_ofs(d * d);
1553 float *color_weight = &_color_weight[0];
1554 float *space_weight = &_space_weight[0];
1555 int *space_ofs = &_space_ofs[0];
1557 int dst_step_in_pixel = dst.step / dst.elemSize();
1558 int dst_offset_in_pixel = dst.offset / dst.elemSize();
1559 int temp_step_in_pixel = temp.step / temp.elemSize();
1561 // initialize color-related bilateral filter coefficients
1562 for( i = 0; i < 256 * cn; i++ )
1563 color_weight[i] = (float)std::exp(i * i * gauss_color_coeff);
1565 // initialize space-related bilateral filter coefficients
1566 for( i = -radius, maxk = 0; i <= radius; i++ )
1567 for( j = -radius; j <= radius; j++ )
1569 double r = std::sqrt((double)i * i + (double)j * j);
1572 space_weight[maxk] = (float)std::exp(r * r * gauss_space_coeff);
1573 space_ofs[maxk++] = (int)(i * temp_step_in_pixel + j);
1576 oclMat oclcolor_weight(1, cn * 256, CV_32FC1, color_weight);
1577 oclMat oclspace_weight(1, d * d, CV_32FC1, space_weight);
1578 oclMat oclspace_ofs(1, d * d, CV_32SC1, space_ofs);
1580 string kernelName = "bilateral";
1581 size_t localThreads[3] = { 16, 16, 1 };
1582 size_t globalThreads[3] = { dst.cols, dst.rows, 1 };
1584 if ((dst.type() == CV_8UC1) && ((dst.offset & 3) == 0) && ((dst.cols & 3) == 0))
1586 kernelName = "bilateral2";
1587 globalThreads[0] = dst.cols >> 2;
1590 vector<pair<size_t , const void *> > args;
1591 args.push_back( make_pair( sizeof(cl_mem), (void *)&dst.data ));
1592 args.push_back( make_pair( sizeof(cl_mem), (void *)&temp.data ));
1593 args.push_back( make_pair( sizeof(cl_int), (void *)&dst.rows ));
1594 args.push_back( make_pair( sizeof(cl_int), (void *)&dst.cols ));
1595 args.push_back( make_pair( sizeof(cl_int), (void *)&maxk ));
1596 args.push_back( make_pair( sizeof(cl_int), (void *)&radius ));
1597 args.push_back( make_pair( sizeof(cl_int), (void *)&dst_step_in_pixel ));
1598 args.push_back( make_pair( sizeof(cl_int), (void *)&dst_offset_in_pixel ));
1599 args.push_back( make_pair( sizeof(cl_int), (void *)&temp_step_in_pixel ));
1600 args.push_back( make_pair( sizeof(cl_int), (void *)&temp.rows ));
1601 args.push_back( make_pair( sizeof(cl_int), (void *)&temp.cols ));
1602 args.push_back( make_pair( sizeof(cl_mem), (void *)&oclcolor_weight.data ));
1603 args.push_back( make_pair( sizeof(cl_mem), (void *)&oclspace_weight.data ));
1604 args.push_back( make_pair( sizeof(cl_mem), (void *)&oclspace_ofs.data ));
1606 openCLExecuteKernel(src.clCxt, &imgproc_bilateral, kernelName, globalThreads, localThreads, args, dst.oclchannels(), dst.depth());
1609 void bilateralFilter(const oclMat &src, oclMat &dst, int radius, double sigmaclr, double sigmaspc, int borderType)
1611 dst.create( src.size(), src.type() );
1612 if ( src.depth() == CV_8U )
1613 oclbilateralFilter_8u( src, dst, radius, sigmaclr, sigmaspc, borderType );
1615 CV_Error( CV_StsUnsupportedFormat, "Bilateral filtering is only implemented for CV_8U images" );
1620 //////////////////////////////////convolve////////////////////////////////////////////////////
1622 static void convolve_run(const oclMat &src, const oclMat &temp1, oclMat &dst, string kernelName, const cv::ocl::ProgramEntry* source)
1624 dst.create(src.size(), src.type());
1626 size_t localThreads[3] = { 16, 16, 1 };
1627 size_t globalThreads[3] = { dst.cols, dst.rows, 1 };
1629 int src_step = src.step / src.elemSize(), src_offset = src.offset / src.elemSize();
1630 int dst_step = dst.step / dst.elemSize(), dst_offset = dst.offset / dst.elemSize();
1631 int temp1_step = temp1.step / temp1.elemSize(), temp1_offset = temp1.offset / temp1.elemSize();
1633 vector<pair<size_t , const void *> > args;
1634 args.push_back( make_pair( sizeof(cl_mem), (void *)&src.data ));
1635 args.push_back( make_pair( sizeof(cl_mem), (void *)&temp1.data ));
1636 args.push_back( make_pair( sizeof(cl_mem), (void *)&dst.data ));
1637 args.push_back( make_pair( sizeof(cl_int), (void *)&src.rows ));
1638 args.push_back( make_pair( sizeof(cl_int), (void *)&src.cols ));
1639 args.push_back( make_pair( sizeof(cl_int), (void *)&src_step ));
1640 args.push_back( make_pair( sizeof(cl_int), (void *)&dst_step ));
1641 args.push_back( make_pair( sizeof(cl_int), (void *)&temp1_step ));
1642 args.push_back( make_pair( sizeof(cl_int), (void *)&temp1.rows ));
1643 args.push_back( make_pair( sizeof(cl_int), (void *)&temp1.cols ));
1644 args.push_back( make_pair( sizeof(cl_int), (void *)&src_offset ));
1645 args.push_back( make_pair( sizeof(cl_int), (void *)&dst_offset ));
1646 args.push_back( make_pair( sizeof(cl_int), (void *)&temp1_offset ));
1648 openCLExecuteKernel(src.clCxt, source, kernelName, globalThreads, localThreads, args, -1, dst.depth());
1651 void cv::ocl::convolve(const oclMat &x, const oclMat &t, oclMat &y)
1653 CV_Assert(x.depth() == CV_32F && t.depth() == CV_32F);
1654 CV_Assert(t.cols <= 17 && t.rows <= 17);
1656 y.create(x.size(), x.type());
1658 convolve_run(x, t, y, "convolve", &imgproc_convolve);