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");
201 CV_Assert(interpolation == INTER_LINEAR || interpolation == INTER_NEAREST);
202 CV_Assert((map1.type() == CV_16SC2 && (map2.empty() || (map2.type() == CV_16UC1 || map2.type() == CV_16SC1)) ) ||
203 (map1.type() == CV_32FC2 && !map2.data) ||
204 (map1.type() == CV_32FC1 && map2.type() == CV_32FC1));
205 CV_Assert(!map2.data || map2.size() == map1.size());
206 CV_Assert(borderType == BORDER_CONSTANT || borderType == BORDER_REPLICATE || borderType == BORDER_WRAP
207 || borderType == BORDER_REFLECT_101 || borderType == BORDER_REFLECT);
209 dst.create(map1.size(), src.type());
211 const char * const typeMap[] = { "uchar", "char", "ushort", "short", "int", "float", "double" };
212 const char * const channelMap[] = { "", "", "2", "4", "4" };
213 const char * const interMap[] = { "INTER_NEAREST", "INTER_LINEAR", "INTER_CUBIC", "INTER_LINEAR", "INTER_LANCZOS" };
214 const char * const borderMap[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", "BORDER_WRAP",
215 "BORDER_REFLECT_101", "BORDER_TRANSPARENT" };
217 string kernelName = "remap";
218 if (map1.type() == CV_32FC2 && map2.empty())
219 kernelName += "_32FC2";
220 else if (map1.type() == CV_16SC2)
222 kernelName += "_16SC2";
224 kernelName += "_16UC1";
226 else if (map1.type() == CV_32FC1 && map2.type() == CV_32FC1)
227 kernelName += "_2_32FC1";
229 CV_Error(CV_StsBadArg, "Unsupported map types");
231 int ocn = dst.oclchannels();
232 size_t localThreads[3] = { 256, 1, 1 };
233 size_t globalThreads[3] = { dst.cols, dst.rows, 1 };
235 Mat scalar(1, 1, CV_MAKE_TYPE(dst.depth(), ocn), borderValue);
236 std::string buildOptions = format("-D %s -D %s -D T=%s%s", interMap[interpolation],
237 borderMap[borderType], typeMap[src.depth()], channelMap[ocn]);
239 if (interpolation != INTER_NEAREST)
241 int wdepth = std::max(CV_32F, dst.depth());
242 buildOptions += format(" -D WT=%s%s -D convertToT=convert_%s%s%s -D convertToWT=convert_%s%s"
243 " -D convertToWT2=convert_%s2 -D WT2=%s2",
244 typeMap[wdepth], channelMap[ocn],
245 typeMap[src.depth()], channelMap[ocn], src.depth() < CV_32F ? "_sat_rte" : "",
246 typeMap[wdepth], channelMap[ocn],
247 typeMap[wdepth], typeMap[wdepth]);
250 int src_step = src.step / src.elemSize(), src_offset = src.offset / src.elemSize();
251 int map1_step = map1.step / map1.elemSize(), map1_offset = map1.offset / map1.elemSize();
252 int map2_step = map2.step / map2.elemSize(), map2_offset = map2.offset / map2.elemSize();
253 int dst_step = dst.step / dst.elemSize(), dst_offset = dst.offset / dst.elemSize();
255 vector< pair<size_t, const void *> > args;
256 args.push_back( make_pair(sizeof(cl_mem), (void *)&src.data));
257 args.push_back( make_pair(sizeof(cl_mem), (void *)&dst.data));
258 args.push_back( make_pair(sizeof(cl_mem), (void *)&map1.data));
260 args.push_back( make_pair(sizeof(cl_mem), (void *)&map2.data));
261 args.push_back( make_pair(sizeof(cl_int), (void *)&src_offset));
262 args.push_back( make_pair(sizeof(cl_int), (void *)&dst_offset));
263 args.push_back( make_pair(sizeof(cl_int), (void *)&map1_offset));
265 args.push_back( make_pair(sizeof(cl_int), (void *)&map2_offset));
266 args.push_back( make_pair(sizeof(cl_int), (void *)&src_step));
267 args.push_back( make_pair(sizeof(cl_int), (void *)&dst_step));
268 args.push_back( make_pair(sizeof(cl_int), (void *)&map1_step));
270 args.push_back( make_pair(sizeof(cl_int), (void *)&map2_step));
271 args.push_back( make_pair(sizeof(cl_int), (void *)&src.cols));
272 args.push_back( make_pair(sizeof(cl_int), (void *)&src.rows));
273 args.push_back( make_pair(sizeof(cl_int), (void *)&dst.cols));
274 args.push_back( make_pair(sizeof(cl_int), (void *)&dst.rows));
275 args.push_back( make_pair(scalar.elemSize(), (void *)scalar.data));
277 openCLExecuteKernel(clCxt, &imgproc_remap, kernelName, globalThreads, localThreads, args, -1, -1, buildOptions.c_str());
280 ////////////////////////////////////////////////////////////////////////////////////////////
283 static void resize_gpu( const oclMat &src, oclMat &dst, double fx, double fy, int interpolation)
285 CV_Assert( (src.channels() == dst.channels()) );
286 Context *clCxt = src.clCxt;
289 double ifx_d = 1. / fx;
290 double ify_d = 1. / fy;
291 int srcStep_in_pixel = src.step1() / src.oclchannels();
292 int srcoffset_in_pixel = src.offset / src.elemSize();
293 int dstStep_in_pixel = dst.step1() / dst.oclchannels();
294 int dstoffset_in_pixel = dst.offset / dst.elemSize();
297 if (interpolation == INTER_LINEAR)
298 kernelName = "resizeLN";
299 else if (interpolation == INTER_NEAREST)
300 kernelName = "resizeNN";
302 //TODO: improve this kernel
303 size_t blkSizeX = 16, blkSizeY = 16;
305 if (src.type() == CV_8UC1)
307 size_t cols = (dst.cols + dst.offset % 4 + 3) / 4;
308 glbSizeX = cols % blkSizeX == 0 && cols != 0 ? cols : (cols / blkSizeX + 1) * blkSizeX;
311 glbSizeX = dst.cols % blkSizeX == 0 && dst.cols != 0 ? dst.cols : (dst.cols / blkSizeX + 1) * blkSizeX;
313 size_t glbSizeY = dst.rows % blkSizeY == 0 && dst.rows != 0 ? dst.rows : (dst.rows / blkSizeY + 1) * blkSizeY;
314 size_t globalThreads[3] = {glbSizeX, glbSizeY, 1};
315 size_t localThreads[3] = {blkSizeX, blkSizeY, 1};
317 vector< pair<size_t, const void *> > args;
318 if (interpolation == INTER_NEAREST)
320 args.push_back( make_pair(sizeof(cl_mem), (void *)&dst.data));
321 args.push_back( make_pair(sizeof(cl_mem), (void *)&src.data));
322 args.push_back( make_pair(sizeof(cl_int), (void *)&dstoffset_in_pixel));
323 args.push_back( make_pair(sizeof(cl_int), (void *)&srcoffset_in_pixel));
324 args.push_back( make_pair(sizeof(cl_int), (void *)&dstStep_in_pixel));
325 args.push_back( make_pair(sizeof(cl_int), (void *)&srcStep_in_pixel));
326 args.push_back( make_pair(sizeof(cl_int), (void *)&src.cols));
327 args.push_back( make_pair(sizeof(cl_int), (void *)&src.rows));
328 args.push_back( make_pair(sizeof(cl_int), (void *)&dst.cols));
329 args.push_back( make_pair(sizeof(cl_int), (void *)&dst.rows));
330 if (src.clCxt->supportsFeature(FEATURE_CL_DOUBLE))
332 args.push_back( make_pair(sizeof(cl_double), (void *)&ifx_d));
333 args.push_back( make_pair(sizeof(cl_double), (void *)&ify_d));
337 args.push_back( make_pair(sizeof(cl_float), (void *)&ifx));
338 args.push_back( make_pair(sizeof(cl_float), (void *)&ify));
343 args.push_back( make_pair(sizeof(cl_mem), (void *)&dst.data));
344 args.push_back( make_pair(sizeof(cl_mem), (void *)&src.data));
345 args.push_back( make_pair(sizeof(cl_int), (void *)&dstoffset_in_pixel));
346 args.push_back( make_pair(sizeof(cl_int), (void *)&srcoffset_in_pixel));
347 args.push_back( make_pair(sizeof(cl_int), (void *)&dstStep_in_pixel));
348 args.push_back( make_pair(sizeof(cl_int), (void *)&srcStep_in_pixel));
349 args.push_back( make_pair(sizeof(cl_int), (void *)&src.cols));
350 args.push_back( make_pair(sizeof(cl_int), (void *)&src.rows));
351 args.push_back( make_pair(sizeof(cl_int), (void *)&dst.cols));
352 args.push_back( make_pair(sizeof(cl_int), (void *)&dst.rows));
353 args.push_back( make_pair(sizeof(cl_float), (void *)&ifx));
354 args.push_back( make_pair(sizeof(cl_float), (void *)&ify));
357 openCLExecuteKernel(clCxt, &imgproc_resize, kernelName, globalThreads, localThreads, args, src.oclchannels(), src.depth());
360 void resize(const oclMat &src, oclMat &dst, Size dsize,
361 double fx, double fy, int interpolation)
363 CV_Assert(src.type() == CV_8UC1 || src.type() == CV_8UC3 || src.type() == CV_8UC4
364 || src.type() == CV_32FC1 || src.type() == CV_32FC3 || src.type() == CV_32FC4);
365 CV_Assert(interpolation == INTER_LINEAR || interpolation == INTER_NEAREST);
366 CV_Assert( src.size().area() > 0 );
367 CV_Assert( !(dsize == Size()) || (fx > 0 && fy > 0) );
369 if (!(dsize == Size()) && (fx > 0 && fy > 0))
370 if (dsize.width != (int)(src.cols * fx) || dsize.height != (int)(src.rows * fy))
371 CV_Error(CV_StsUnmatchedSizes, "invalid dsize and fx, fy!");
373 if ( dsize == Size() )
374 dsize = Size(saturate_cast<int>(src.cols * fx), saturate_cast<int>(src.rows * fy));
377 fx = (double)dsize.width / src.cols;
378 fy = (double)dsize.height / src.rows;
381 dst.create(dsize, src.type());
383 if ( interpolation == INTER_NEAREST || interpolation == INTER_LINEAR )
385 resize_gpu( src, dst, fx, fy, interpolation);
389 CV_Error(CV_StsUnsupportedFormat, "Non-supported interpolation method");
392 ////////////////////////////////////////////////////////////////////////
395 void medianFilter(const oclMat &src, oclMat &dst, int m)
397 CV_Assert( m % 2 == 1 && m > 1 );
398 CV_Assert( (src.depth() == CV_8U || src.depth() == CV_32F) && (src.channels() == 1 || src.channels() == 4));
399 dst.create(src.size(), src.type());
401 int srcStep = src.step / src.elemSize(), dstStep = dst.step / dst.elemSize();
402 int srcOffset = src.offset / src.elemSize(), dstOffset = dst.offset / dst.elemSize();
404 Context *clCxt = src.clCxt;
406 vector< pair<size_t, const void *> > args;
407 args.push_back( make_pair( sizeof(cl_mem), (void *)&src.data));
408 args.push_back( make_pair( sizeof(cl_mem), (void *)&dst.data));
409 args.push_back( make_pair( sizeof(cl_int), (void *)&srcOffset));
410 args.push_back( make_pair( sizeof(cl_int), (void *)&dstOffset));
411 args.push_back( make_pair( sizeof(cl_int), (void *)&src.cols));
412 args.push_back( make_pair( sizeof(cl_int), (void *)&src.rows));
413 args.push_back( make_pair( sizeof(cl_int), (void *)&srcStep));
414 args.push_back( make_pair( sizeof(cl_int), (void *)&dstStep));
416 size_t globalThreads[3] = {(src.cols + 18) / 16 * 16, (src.rows + 15) / 16 * 16, 1};
417 size_t localThreads[3] = {16, 16, 1};
421 string kernelName = "medianFilter3";
422 openCLExecuteKernel(clCxt, &imgproc_median, kernelName, globalThreads, localThreads, args, src.oclchannels(), src.depth());
426 string kernelName = "medianFilter5";
427 openCLExecuteKernel(clCxt, &imgproc_median, kernelName, globalThreads, localThreads, args, src.oclchannels(), src.depth());
430 CV_Error(CV_StsBadArg, "Non-supported filter length");
433 ////////////////////////////////////////////////////////////////////////
436 void copyMakeBorder(const oclMat &src, oclMat &dst, int top, int bottom, int left, int right, int bordertype, const Scalar &scalar)
438 if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
440 CV_Error(CV_OpenCLDoubleNotSupported, "Selected device does not support double");
446 CV_Assert(top >= 0 && bottom >= 0 && left >= 0 && right >= 0);
448 if( (_src.wholecols != _src.cols || _src.wholerows != _src.rows) && (bordertype & BORDER_ISOLATED) == 0 )
452 _src.locateROI(wholeSize, ofs);
453 int dtop = std::min(ofs.y, top);
454 int dbottom = std::min(wholeSize.height - _src.rows - ofs.y, bottom);
455 int dleft = std::min(ofs.x, left);
456 int dright = std::min(wholeSize.width - _src.cols - ofs.x, right);
457 _src.adjustROI(dtop, dbottom, dleft, dright);
463 bordertype &= ~cv::BORDER_ISOLATED;
465 dst.create(_src.rows + top + bottom, _src.cols + left + right, _src.type());
466 int srcStep = _src.step / _src.elemSize(), dstStep = dst.step / dst.elemSize();
467 int srcOffset = _src.offset / _src.elemSize(), dstOffset = dst.offset / dst.elemSize();
468 int depth = _src.depth(), ochannels = _src.oclchannels();
470 int __bordertype[] = { BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT, BORDER_WRAP, BORDER_REFLECT_101 };
471 const char *borderstr[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", "BORDER_WRAP", "BORDER_REFLECT_101" };
473 int bordertype_index = -1;
474 for (int i = 0, end = sizeof(__bordertype) / sizeof(int); i < end; i++)
475 if (__bordertype[i] == bordertype)
477 bordertype_index = i;
480 if (bordertype_index < 0)
481 CV_Error(CV_StsBadArg, "Unsupported border type");
483 size_t localThreads[3] = { 16, 16, 1 };
484 size_t globalThreads[3] = { dst.cols, dst.rows, 1 };
486 vector< pair<size_t, const void *> > args;
487 args.push_back( make_pair( sizeof(cl_mem), (void *)&_src.data));
488 args.push_back( make_pair( sizeof(cl_mem), (void *)&dst.data));
489 args.push_back( make_pair( sizeof(cl_int), (void *)&dst.cols));
490 args.push_back( make_pair( sizeof(cl_int), (void *)&dst.rows));
491 args.push_back( make_pair( sizeof(cl_int), (void *)&_src.cols));
492 args.push_back( make_pair( sizeof(cl_int), (void *)&_src.rows));
493 args.push_back( make_pair( sizeof(cl_int), (void *)&srcStep));
494 args.push_back( make_pair( sizeof(cl_int), (void *)&srcOffset));
495 args.push_back( make_pair( sizeof(cl_int), (void *)&dstStep));
496 args.push_back( make_pair( sizeof(cl_int), (void *)&dstOffset));
497 args.push_back( make_pair( sizeof(cl_int), (void *)&top));
498 args.push_back( make_pair( sizeof(cl_int), (void *)&left));
500 const char * const typeMap[] = { "uchar", "char", "ushort", "short", "int", "float", "double" };
501 const char * const channelMap[] = { "", "", "2", "4", "4" };
502 std::string buildOptions = format("-D GENTYPE=%s%s -D %s",
503 typeMap[depth], channelMap[ochannels],
504 borderstr[bordertype_index]);
506 int cn = src.channels(), ocn = src.oclchannels();
507 int bufSize = src.elemSize1() * ocn;
508 AutoBuffer<uchar> _buf(bufSize);
509 uchar * buf = (uchar *)_buf;
510 scalarToRawData(scalar, buf, dst.type());
511 memset(buf + src.elemSize1() * cn, 0, (ocn - cn) * src.elemSize1());
513 args.push_back( make_pair( bufSize , (void *)buf ));
515 openCLExecuteKernel(src.clCxt, &imgproc_copymakeboder, "copymakeborder", globalThreads,
516 localThreads, args, -1, -1, buildOptions.c_str());
519 ////////////////////////////////////////////////////////////////////////
526 void convert_coeffs(F *M)
528 double D = M[0] * M[4] - M[1] * M[3];
529 D = D != 0 ? 1. / D : 0;
530 double A11 = M[4] * D, A22 = M[0] * D;
535 double b1 = -M[0] * M[2] - M[1] * M[5];
536 double b2 = -M[3] * M[2] - M[4] * M[5];
541 double invert(double *M)
543 #define Sd(y,x) (Sd[y*3+x])
544 #define Dd(y,x) (Dd[y*3+x])
545 #define det3(m) (m(0,0)*(m(1,1)*m(2,2) - m(1,2)*m(2,1)) - \
546 m(0,1)*(m(1,0)*m(2,2) - m(1,2)*m(2,0)) + \
547 m(0,2)*(m(1,0)*m(2,1) - m(1,1)*m(2,0)))
558 t[0] = (Sd(1, 1) * Sd(2, 2) - Sd(1, 2) * Sd(2, 1)) * d;
559 t[1] = (Sd(0, 2) * Sd(2, 1) - Sd(0, 1) * Sd(2, 2)) * d;
560 t[2] = (Sd(0, 1) * Sd(1, 2) - Sd(0, 2) * Sd(1, 1)) * d;
562 t[3] = (Sd(1, 2) * Sd(2, 0) - Sd(1, 0) * Sd(2, 2)) * d;
563 t[4] = (Sd(0, 0) * Sd(2, 2) - Sd(0, 2) * Sd(2, 0)) * d;
564 t[5] = (Sd(0, 2) * Sd(1, 0) - Sd(0, 0) * Sd(1, 2)) * d;
566 t[6] = (Sd(1, 0) * Sd(2, 1) - Sd(1, 1) * Sd(2, 0)) * d;
567 t[7] = (Sd(0, 1) * Sd(2, 0) - Sd(0, 0) * Sd(2, 1)) * d;
568 t[8] = (Sd(0, 0) * Sd(1, 1) - Sd(0, 1) * Sd(1, 0)) * d;
583 void warpAffine_gpu(const oclMat &src, oclMat &dst, F coeffs[2][3], int interpolation)
585 CV_Assert( (src.oclchannels() == dst.oclchannels()) );
586 int srcStep = src.step1();
587 int dstStep = dst.step1();
588 float float_coeffs[2][3];
591 Context *clCxt = src.clCxt;
592 string s[3] = {"NN", "Linear", "Cubic"};
593 string kernelName = "warpAffine" + s[interpolation];
595 if (src.clCxt->supportsFeature(FEATURE_CL_DOUBLE))
598 coeffs_cm = clCreateBuffer(*(cl_context*)clCxt->getOpenCLContextPtr(), CL_MEM_READ_WRITE, sizeof(F) * 2 * 3, NULL, &st );
599 openCLVerifyCall(st);
600 openCLSafeCall(clEnqueueWriteBuffer(*(cl_command_queue*)clCxt->getOpenCLCommandQueuePtr(), (cl_mem)coeffs_cm, 1, 0,
601 sizeof(F) * 2 * 3, coeffs, 0, 0, 0));
606 for(int m = 0; m < 2; m++)
607 for(int n = 0; n < 3; n++)
608 float_coeffs[m][n] = coeffs[m][n];
610 coeffs_cm = clCreateBuffer(*(cl_context*)clCxt->getOpenCLContextPtr(), CL_MEM_READ_WRITE, sizeof(float) * 2 * 3, NULL, &st );
611 openCLSafeCall(clEnqueueWriteBuffer(*(cl_command_queue*)clCxt->getOpenCLCommandQueuePtr(), (cl_mem)coeffs_cm,
612 1, 0, sizeof(float) * 2 * 3, float_coeffs, 0, 0, 0));
615 //TODO: improve this kernel
616 size_t blkSizeX = 16, blkSizeY = 16;
620 if (src.type() == CV_8UC1 && interpolation != 2)
622 cols = (dst.cols + dst.offset % 4 + 3) / 4;
623 glbSizeX = cols % blkSizeX == 0 ? cols : (cols / blkSizeX + 1) * blkSizeX;
628 glbSizeX = dst.cols % blkSizeX == 0 ? dst.cols : (dst.cols / blkSizeX + 1) * blkSizeX;
631 size_t glbSizeY = dst.rows % blkSizeY == 0 ? dst.rows : (dst.rows / blkSizeY + 1) * blkSizeY;
632 size_t globalThreads[3] = {glbSizeX, glbSizeY, 1};
633 size_t localThreads[3] = {blkSizeX, blkSizeY, 1};
635 vector< pair<size_t, const void *> > args;
637 args.push_back(make_pair(sizeof(cl_mem), (void *)&src.data));
638 args.push_back(make_pair(sizeof(cl_mem), (void *)&dst.data));
639 args.push_back(make_pair(sizeof(cl_int), (void *)&src.cols));
640 args.push_back(make_pair(sizeof(cl_int), (void *)&src.rows));
641 args.push_back(make_pair(sizeof(cl_int), (void *)&dst.cols));
642 args.push_back(make_pair(sizeof(cl_int), (void *)&dst.rows));
643 args.push_back(make_pair(sizeof(cl_int), (void *)&srcStep));
644 args.push_back(make_pair(sizeof(cl_int), (void *)&dstStep));
645 args.push_back(make_pair(sizeof(cl_int), (void *)&src.offset));
646 args.push_back(make_pair(sizeof(cl_int), (void *)&dst.offset));
647 args.push_back(make_pair(sizeof(cl_mem), (void *)&coeffs_cm));
648 args.push_back(make_pair(sizeof(cl_int), (void *)&cols));
650 openCLExecuteKernel(clCxt, &imgproc_warpAffine, kernelName, globalThreads, localThreads, args, src.oclchannels(), src.depth());
651 openCLSafeCall(clReleaseMemObject(coeffs_cm));
654 void warpPerspective_gpu(const oclMat &src, oclMat &dst, double coeffs[3][3], int interpolation)
656 CV_Assert( (src.oclchannels() == dst.oclchannels()) );
657 int srcStep = src.step1();
658 int dstStep = dst.step1();
659 float float_coeffs[3][3];
662 Context *clCxt = src.clCxt;
663 string s[3] = {"NN", "Linear", "Cubic"};
664 string kernelName = "warpPerspective" + s[interpolation];
666 if (src.clCxt->supportsFeature(FEATURE_CL_DOUBLE))
669 coeffs_cm = clCreateBuffer(*(cl_context*)clCxt->getOpenCLContextPtr(), CL_MEM_READ_WRITE, sizeof(double) * 3 * 3, NULL, &st );
670 openCLVerifyCall(st);
671 openCLSafeCall(clEnqueueWriteBuffer(*(cl_command_queue*)clCxt->getOpenCLCommandQueuePtr(), (cl_mem)coeffs_cm, 1, 0,
672 sizeof(double) * 3 * 3, coeffs, 0, 0, 0));
677 for(int m = 0; m < 3; m++)
678 for(int n = 0; n < 3; n++)
679 float_coeffs[m][n] = coeffs[m][n];
681 coeffs_cm = clCreateBuffer(*(cl_context*)clCxt->getOpenCLContextPtr(), CL_MEM_READ_WRITE, sizeof(float) * 3 * 3, NULL, &st );
682 openCLVerifyCall(st);
683 openCLSafeCall(clEnqueueWriteBuffer(*(cl_command_queue*)clCxt->getOpenCLCommandQueuePtr(), (cl_mem)coeffs_cm, 1, 0,
684 sizeof(float) * 3 * 3, float_coeffs, 0, 0, 0));
687 //TODO: improve this kernel
688 size_t blkSizeX = 16, blkSizeY = 16;
691 if (src.type() == CV_8UC1 && interpolation == 0)
693 cols = (dst.cols + dst.offset % 4 + 3) / 4;
694 glbSizeX = cols % blkSizeX == 0 ? cols : (cols / blkSizeX + 1) * blkSizeX;
699 glbSizeX = dst.cols % blkSizeX == 0 ? dst.cols : (dst.cols / blkSizeX + 1) * blkSizeX;
702 size_t glbSizeY = dst.rows % blkSizeY == 0 ? dst.rows : (dst.rows / blkSizeY + 1) * blkSizeY;
703 size_t globalThreads[3] = {glbSizeX, glbSizeY, 1};
704 size_t localThreads[3] = {blkSizeX, blkSizeY, 1};
706 vector< pair<size_t, const void *> > args;
708 args.push_back(make_pair(sizeof(cl_mem), (void *)&src.data));
709 args.push_back(make_pair(sizeof(cl_mem), (void *)&dst.data));
710 args.push_back(make_pair(sizeof(cl_int), (void *)&src.cols));
711 args.push_back(make_pair(sizeof(cl_int), (void *)&src.rows));
712 args.push_back(make_pair(sizeof(cl_int), (void *)&dst.cols));
713 args.push_back(make_pair(sizeof(cl_int), (void *)&dst.rows));
714 args.push_back(make_pair(sizeof(cl_int), (void *)&srcStep));
715 args.push_back(make_pair(sizeof(cl_int), (void *)&dstStep));
716 args.push_back(make_pair(sizeof(cl_int), (void *)&src.offset));
717 args.push_back(make_pair(sizeof(cl_int), (void *)&dst.offset));
718 args.push_back(make_pair(sizeof(cl_mem), (void *)&coeffs_cm));
719 args.push_back(make_pair(sizeof(cl_int), (void *)&cols));
721 openCLExecuteKernel(clCxt, &imgproc_warpPerspective, kernelName, globalThreads, localThreads, args, src.oclchannels(), src.depth());
722 openCLSafeCall(clReleaseMemObject(coeffs_cm));
726 void warpAffine(const oclMat &src, oclMat &dst, const Mat &M, Size dsize, int flags)
728 int interpolation = flags & INTER_MAX;
730 CV_Assert((src.depth() == CV_8U || src.depth() == CV_32F) && src.oclchannels() != 2 && src.oclchannels() != 3);
731 CV_Assert(interpolation == INTER_NEAREST || interpolation == INTER_LINEAR || interpolation == INTER_CUBIC);
733 dst.create(dsize, src.type());
735 CV_Assert(M.rows == 2 && M.cols == 3);
737 int warpInd = (flags & WARP_INVERSE_MAP) >> 4;
741 Mat coeffsMat(2, 3, CV_64F, (void *)coeffsM);
742 M.convertTo(coeffsMat, coeffsMat.type());
744 convert_coeffs(coeffsM);
746 for(int i = 0; i < 2; ++i)
747 for(int j = 0; j < 3; ++j)
748 coeffs[i][j] = coeffsM[i*3+j];
750 warpAffine_gpu(src, dst, coeffs, interpolation);
753 void warpPerspective(const oclMat &src, oclMat &dst, const Mat &M, Size dsize, int flags)
755 int interpolation = flags & INTER_MAX;
757 CV_Assert((src.depth() == CV_8U || src.depth() == CV_32F) && src.oclchannels() != 2 && src.oclchannels() != 3);
758 CV_Assert(interpolation == INTER_NEAREST || interpolation == INTER_LINEAR || interpolation == INTER_CUBIC);
760 dst.create(dsize, src.type());
763 CV_Assert(M.rows == 3 && M.cols == 3);
765 int warpInd = (flags & WARP_INVERSE_MAP) >> 4;
769 Mat coeffsMat(3, 3, CV_64F, (void *)coeffsM);
770 M.convertTo(coeffsMat, coeffsMat.type());
774 for(int i = 0; i < 3; ++i)
775 for(int j = 0; j < 3; ++j)
776 coeffs[i][j] = coeffsM[i*3+j];
778 warpPerspective_gpu(src, dst, coeffs, interpolation);
781 ////////////////////////////////////////////////////////////////////////
784 void integral(const oclMat &src, oclMat &sum, oclMat &sqsum, int sdepth)
786 CV_Assert(src.type() == CV_8UC1);
787 if (!src.clCxt->supportsFeature(ocl::FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
789 CV_Error(CV_OpenCLDoubleNotSupported, "Select device doesn't support double");
795 sdepth = CV_MAT_DEPTH(sdepth);
796 int type = CV_MAKE_TYPE(sdepth, 1);
799 int offset = src.offset / vlen;
800 int pre_invalid = src.offset % vlen;
801 int vcols = (pre_invalid + src.cols + vlen - 1) / vlen;
803 oclMat t_sum , t_sqsum;
804 int w = src.cols + 1, h = src.rows + 1;
806 char build_option[250];
807 if(Context::getContext()->supportsFeature(ocl::FEATURE_CL_DOUBLE))
809 t_sqsum.create(src.cols, src.rows, CV_64FC1);
810 sqsum.create(h, w, CV_64FC1);
811 sprintf(build_option, "-D TYPE=double -D TYPE4=double4 -D convert_TYPE4=convert_double4");
815 t_sqsum.create(src.cols, src.rows, CV_32FC1);
816 sqsum.create(h, w, CV_32FC1);
817 sprintf(build_option, "-D TYPE=float -D TYPE4=float4 -D convert_TYPE4=convert_float4");
820 t_sum.create(src.cols, src.rows, type);
821 sum.create(h, w, type);
823 int sum_offset = sum.offset / sum.elemSize();
824 int sqsum_offset = sqsum.offset / sqsum.elemSize();
826 vector<pair<size_t , const void *> > args;
827 args.push_back( make_pair( sizeof(cl_mem) , (void *)&src.data ));
828 args.push_back( make_pair( sizeof(cl_mem) , (void *)&t_sum.data ));
829 args.push_back( make_pair( sizeof(cl_mem) , (void *)&t_sqsum.data ));
830 args.push_back( make_pair( sizeof(cl_int) , (void *)&offset ));
831 args.push_back( make_pair( sizeof(cl_int) , (void *)&pre_invalid ));
832 args.push_back( make_pair( sizeof(cl_int) , (void *)&src.rows ));
833 args.push_back( make_pair( sizeof(cl_int) , (void *)&src.cols ));
834 args.push_back( make_pair( sizeof(cl_int) , (void *)&src.step ));
835 args.push_back( make_pair( sizeof(cl_int) , (void *)&t_sum.step));
836 args.push_back( make_pair( sizeof(cl_int) , (void *)&t_sqsum.step));
837 size_t gt[3] = {((vcols + 1) / 2) * 256, 1, 1}, lt[3] = {256, 1, 1};
838 openCLExecuteKernel(src.clCxt, &imgproc_integral, "integral_cols", gt, lt, args, -1, sdepth, build_option);
841 args.push_back( make_pair( sizeof(cl_mem) , (void *)&t_sum.data ));
842 args.push_back( make_pair( sizeof(cl_mem) , (void *)&t_sqsum.data ));
843 args.push_back( make_pair( sizeof(cl_mem) , (void *)&sum.data ));
844 args.push_back( make_pair( sizeof(cl_mem) , (void *)&sqsum.data ));
845 args.push_back( make_pair( sizeof(cl_int) , (void *)&t_sum.rows ));
846 args.push_back( make_pair( sizeof(cl_int) , (void *)&t_sum.cols ));
847 args.push_back( make_pair( sizeof(cl_int) , (void *)&t_sum.step ));
848 args.push_back( make_pair( sizeof(cl_int) , (void *)&t_sqsum.step));
849 args.push_back( make_pair( sizeof(cl_int) , (void *)&sum.step));
850 args.push_back( make_pair( sizeof(cl_int) , (void *)&sqsum.step));
851 args.push_back( make_pair( sizeof(cl_int) , (void *)&sum_offset));
852 args.push_back( make_pair( sizeof(cl_int) , (void *)&sqsum_offset));
853 size_t gt2[3] = {t_sum.cols * 32, 1, 1}, lt2[3] = {256, 1, 1};
854 openCLExecuteKernel(src.clCxt, &imgproc_integral, "integral_rows", gt2, lt2, args, -1, sdepth, build_option);
857 void integral(const oclMat &src, oclMat &sum, int sdepth)
859 CV_Assert(src.type() == CV_8UC1);
861 int offset = src.offset / vlen;
862 int pre_invalid = src.offset % vlen;
863 int vcols = (pre_invalid + src.cols + vlen - 1) / vlen;
867 sdepth = CV_MAT_DEPTH(sdepth);
868 int type = CV_MAKE_TYPE(sdepth, 1);
871 int w = src.cols + 1, h = src.rows + 1;
873 t_sum.create(src.cols, src.rows, type);
874 sum.create(h, w, type);
876 int sum_offset = sum.offset / vlen;
877 vector<pair<size_t , const void *> > args;
878 args.push_back( make_pair( sizeof(cl_mem) , (void *)&src.data ));
879 args.push_back( make_pair( sizeof(cl_mem) , (void *)&t_sum.data ));
880 args.push_back( make_pair( sizeof(cl_int) , (void *)&offset ));
881 args.push_back( make_pair( sizeof(cl_int) , (void *)&pre_invalid ));
882 args.push_back( make_pair( sizeof(cl_int) , (void *)&src.rows ));
883 args.push_back( make_pair( sizeof(cl_int) , (void *)&src.cols ));
884 args.push_back( make_pair( sizeof(cl_int) , (void *)&src.step ));
885 args.push_back( make_pair( sizeof(cl_int) , (void *)&t_sum.step));
886 size_t gt[3] = {((vcols + 1) / 2) * 256, 1, 1}, lt[3] = {256, 1, 1};
887 openCLExecuteKernel(src.clCxt, &imgproc_integral_sum, "integral_sum_cols", gt, lt, args, -1, sdepth);
890 args.push_back( make_pair( sizeof(cl_mem) , (void *)&t_sum.data ));
891 args.push_back( make_pair( sizeof(cl_mem) , (void *)&sum.data ));
892 args.push_back( make_pair( sizeof(cl_int) , (void *)&t_sum.rows ));
893 args.push_back( make_pair( sizeof(cl_int) , (void *)&t_sum.cols ));
894 args.push_back( make_pair( sizeof(cl_int) , (void *)&t_sum.step ));
895 args.push_back( make_pair( sizeof(cl_int) , (void *)&sum.step));
896 args.push_back( make_pair( sizeof(cl_int) , (void *)&sum_offset));
897 size_t gt2[3] = {t_sum.cols * 32, 1, 1}, lt2[3] = {256, 1, 1};
898 openCLExecuteKernel(src.clCxt, &imgproc_integral_sum, "integral_sum_rows", gt2, lt2, args, -1, sdepth);
901 /////////////////////// corner //////////////////////////////
903 static void extractCovData(const oclMat &src, oclMat &Dx, oclMat &Dy,
904 int blockSize, int ksize, int borderType)
906 CV_Assert(src.type() == CV_8UC1 || src.type() == CV_32FC1);
907 double scale = static_cast<double>(1 << ((ksize > 0 ? ksize : 3) - 1)) * blockSize;
911 if (src.depth() == CV_8U)
921 Context* clCxt = Context::getContext();
922 if(clCxt->supportsFeature(FEATURE_CL_INTEL_DEVICE) && src.type() == CV_8UC1 &&
923 src.cols % 8 == 0 && src.rows % 8 == 0 &&
925 (borderType ==cv::BORDER_REFLECT ||
926 borderType == cv::BORDER_REPLICATE ||
927 borderType ==cv::BORDER_REFLECT101 ||
928 borderType ==cv::BORDER_WRAP))
930 Dx.create(src.size(), CV_32FC1);
931 Dy.create(src.size(), CV_32FC1);
933 const unsigned int block_x = 8;
934 const unsigned int block_y = 8;
936 unsigned int src_pitch = src.step;
937 unsigned int dst_pitch = Dx.cols;
939 float _scale = scale;
941 std::vector<std::pair<size_t , const void *> > args;
942 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&src.data ));
943 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&Dx.data ));
944 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&Dy.data ));
945 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.cols ));
946 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.rows ));
947 args.push_back( std::make_pair( sizeof(cl_uint) , (void *)&src_pitch ));
948 args.push_back( std::make_pair( sizeof(cl_uint) , (void *)&dst_pitch ));
949 args.push_back( std::make_pair( sizeof(cl_float) , (void *)&_scale ));
950 size_t gt2[3] = {src.cols, src.rows, 1}, lt2[3] = {block_x, block_y, 1};
952 string option = "-D BLK_X=8 -D BLK_Y=8";
955 case cv::BORDER_REPLICATE:
956 option += " -D BORDER_REPLICATE";
958 case cv::BORDER_REFLECT:
959 option += " -D BORDER_REFLECT";
961 case cv::BORDER_REFLECT101:
962 option += " -D BORDER_REFLECT101";
964 case cv::BORDER_WRAP:
965 option += " -D BORDER_WRAP";
968 openCLExecuteKernel(src.clCxt, &imgproc_sobel3, "sobel3", gt2, lt2, args, -1, -1, option.c_str() );
972 Sobel(src, Dx, CV_32F, 1, 0, ksize, scale, 0, borderType);
973 Sobel(src, Dy, CV_32F, 0, 1, ksize, scale, 0, borderType);
978 Scharr(src, Dx, CV_32F, 1, 0, scale, 0, borderType);
979 Scharr(src, Dy, CV_32F, 0, 1, scale, 0, borderType);
981 CV_Assert(Dx.offset == 0 && Dy.offset == 0);
984 static void corner_ocl(const cv::ocl::ProgramEntry* source, string kernelName, int block_size, float k, oclMat &Dx, oclMat &Dy,
985 oclMat &dst, int border_type)
990 case cv::BORDER_CONSTANT:
991 sprintf(borderType, "BORDER_CONSTANT");
993 case cv::BORDER_REFLECT101:
994 sprintf(borderType, "BORDER_REFLECT101");
996 case cv::BORDER_REFLECT:
997 sprintf(borderType, "BORDER_REFLECT");
999 case cv::BORDER_REPLICATE:
1000 sprintf(borderType, "BORDER_REPLICATE");
1003 CV_Error(CV_StsBadFlag, "BORDER type is not supported!");
1006 std::string buildOptions = format("-D anX=%d -D anY=%d -D ksX=%d -D ksY=%d -D %s",
1007 block_size / 2, block_size / 2, block_size, block_size, borderType);
1009 size_t blockSizeX = 256, blockSizeY = 1;
1010 size_t gSize = blockSizeX - block_size / 2 * 2;
1011 size_t globalSizeX = (Dx.cols) % gSize == 0 ? Dx.cols / gSize * blockSizeX : (Dx.cols / gSize + 1) * blockSizeX;
1012 size_t rows_per_thread = 2;
1013 size_t globalSizeY = ((Dx.rows + rows_per_thread - 1) / rows_per_thread) % blockSizeY == 0 ?
1014 ((Dx.rows + rows_per_thread - 1) / rows_per_thread) :
1015 (((Dx.rows + rows_per_thread - 1) / rows_per_thread) / blockSizeY + 1) * blockSizeY;
1017 size_t gt[3] = { globalSizeX, globalSizeY, 1 };
1018 size_t lt[3] = { blockSizeX, blockSizeY, 1 };
1019 vector<pair<size_t , const void *> > args;
1020 args.push_back( make_pair( sizeof(cl_mem) , (void *)&Dx.data ));
1021 args.push_back( make_pair( sizeof(cl_mem) , (void *)&Dy.data));
1022 args.push_back( make_pair( sizeof(cl_mem) , (void *)&dst.data));
1023 args.push_back( make_pair( sizeof(cl_int) , (void *)&Dx.offset ));
1024 args.push_back( make_pair( sizeof(cl_int) , (void *)&Dx.wholerows ));
1025 args.push_back( make_pair( sizeof(cl_int) , (void *)&Dx.wholecols ));
1026 args.push_back( make_pair(sizeof(cl_int), (void *)&Dx.step));
1027 args.push_back( make_pair( sizeof(cl_int) , (void *)&Dy.offset ));
1028 args.push_back( make_pair( sizeof(cl_int) , (void *)&Dy.wholerows ));
1029 args.push_back( make_pair( sizeof(cl_int) , (void *)&Dy.wholecols ));
1030 args.push_back( make_pair(sizeof(cl_int), (void *)&Dy.step));
1031 args.push_back( make_pair(sizeof(cl_int), (void *)&dst.offset));
1032 args.push_back( make_pair(sizeof(cl_int), (void *)&dst.rows));
1033 args.push_back( make_pair(sizeof(cl_int), (void *)&dst.cols));
1034 args.push_back( make_pair(sizeof(cl_int), (void *)&dst.step));
1035 args.push_back( make_pair( sizeof(cl_float) , (void *)&k));
1037 openCLExecuteKernel(dst.clCxt, source, kernelName, gt, lt, args, -1, -1, buildOptions.c_str());
1040 void cornerHarris(const oclMat &src, oclMat &dst, int blockSize, int ksize,
1041 double k, int borderType)
1044 cornerHarris_dxdy(src, dst, dx, dy, blockSize, ksize, k, borderType);
1047 void cornerHarris_dxdy(const oclMat &src, oclMat &dst, oclMat &dx, oclMat &dy, int blockSize, int ksize,
1048 double k, int borderType)
1050 if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
1052 CV_Error(CV_OpenCLDoubleNotSupported, "Selected device doesn't support double");
1056 CV_Assert(borderType == cv::BORDER_CONSTANT || borderType == cv::BORDER_REFLECT101 || borderType == cv::BORDER_REPLICATE
1057 || borderType == cv::BORDER_REFLECT);
1059 extractCovData(src, dx, dy, blockSize, ksize, borderType);
1060 dst.create(src.size(), CV_32FC1);
1061 corner_ocl(&imgproc_calcHarris, "calcHarris", blockSize, static_cast<float>(k), dx, dy, dst, borderType);
1064 void cornerMinEigenVal(const oclMat &src, oclMat &dst, int blockSize, int ksize, int borderType)
1067 cornerMinEigenVal_dxdy(src, dst, dx, dy, blockSize, ksize, borderType);
1070 void cornerMinEigenVal_dxdy(const oclMat &src, oclMat &dst, oclMat &dx, oclMat &dy, int blockSize, int ksize, int borderType)
1072 if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
1074 CV_Error(CV_OpenCLDoubleNotSupported, "Selected device doesn't support double");
1078 CV_Assert(borderType == cv::BORDER_CONSTANT || borderType == cv::BORDER_REFLECT101 ||
1079 borderType == cv::BORDER_REPLICATE || borderType == cv::BORDER_REFLECT);
1081 extractCovData(src, dx, dy, blockSize, ksize, borderType);
1082 dst.create(src.size(), CV_32F);
1084 corner_ocl(&imgproc_calcMinEigenVal, "calcMinEigenVal", blockSize, 0, dx, dy, dst, borderType);
1087 /////////////////////////////////// MeanShiftfiltering ///////////////////////////////////////////////
1089 static void meanShiftFiltering_gpu(const oclMat &src, oclMat dst, int sp, int sr, int maxIter, float eps)
1091 CV_Assert( (src.cols == dst.cols) && (src.rows == dst.rows) );
1092 CV_Assert( !(dst.step & 0x3) );
1094 //Arrange the NDRange
1095 int col = src.cols, row = src.rows;
1096 int ltx = 16, lty = 8;
1097 if (src.cols % ltx != 0)
1098 col = (col / ltx + 1) * ltx;
1099 if (src.rows % lty != 0)
1100 row = (row / lty + 1) * lty;
1102 size_t globalThreads[3] = {col, row, 1};
1103 size_t localThreads[3] = {ltx, lty, 1};
1106 vector<pair<size_t , const void *> > args;
1107 args.push_back( make_pair( sizeof(cl_mem) , (void *)&dst.data ));
1108 args.push_back( make_pair( sizeof(cl_int) , (void *)&dst.step ));
1109 args.push_back( make_pair( sizeof(cl_mem) , (void *)&src.data ));
1110 args.push_back( make_pair( sizeof(cl_int) , (void *)&src.step ));
1111 args.push_back( make_pair( sizeof(cl_int) , (void *)&dst.offset ));
1112 args.push_back( make_pair( sizeof(cl_int) , (void *)&src.offset ));
1113 args.push_back( make_pair( sizeof(cl_int) , (void *)&dst.cols ));
1114 args.push_back( make_pair( sizeof(cl_int) , (void *)&dst.rows ));
1115 args.push_back( make_pair( sizeof(cl_int) , (void *)&sp ));
1116 args.push_back( make_pair( sizeof(cl_int) , (void *)&sr ));
1117 args.push_back( make_pair( sizeof(cl_int) , (void *)&maxIter ));
1118 args.push_back( make_pair( sizeof(cl_float) , (void *)&eps ));
1120 openCLExecuteKernel(src.clCxt, &meanShift, "meanshift_kernel", globalThreads, localThreads, args, -1, -1);
1123 void meanShiftFiltering(const oclMat &src, oclMat &dst, int sp, int sr, TermCriteria criteria)
1126 CV_Error( CV_StsBadArg, "The input image is empty" );
1128 if ( src.depth() != CV_8U || src.oclchannels() != 4 )
1129 CV_Error( CV_StsUnsupportedFormat, "Only 8-bit, 4-channel images are supported" );
1131 dst.create( src.size(), CV_8UC4 );
1133 if ( !(criteria.type & TermCriteria::MAX_ITER) )
1134 criteria.maxCount = 5;
1136 int maxIter = std::min(std::max(criteria.maxCount, 1), 100);
1139 if ( !(criteria.type & TermCriteria::EPS) )
1141 eps = (float)std::max(criteria.epsilon, 0.0);
1143 meanShiftFiltering_gpu(src, dst, sp, sr, maxIter, eps);
1146 static void meanShiftProc_gpu(const oclMat &src, oclMat dstr, oclMat dstsp, int sp, int sr, int maxIter, float eps)
1149 CV_Assert( (src.cols == dstr.cols) && (src.rows == dstr.rows) &&
1150 (src.rows == dstsp.rows) && (src.cols == dstsp.cols));
1151 CV_Assert( !(dstsp.step & 0x3) );
1153 //Arrange the NDRange
1154 int col = src.cols, row = src.rows;
1155 int ltx = 16, lty = 8;
1156 if (src.cols % ltx != 0)
1157 col = (col / ltx + 1) * ltx;
1158 if (src.rows % lty != 0)
1159 row = (row / lty + 1) * lty;
1161 size_t globalThreads[3] = {col, row, 1};
1162 size_t localThreads[3] = {ltx, lty, 1};
1165 vector<pair<size_t , const void *> > args;
1166 args.push_back( make_pair( sizeof(cl_mem) , (void *)&src.data ));
1167 args.push_back( make_pair( sizeof(cl_mem) , (void *)&dstr.data ));
1168 args.push_back( make_pair( sizeof(cl_mem) , (void *)&dstsp.data ));
1169 args.push_back( make_pair( sizeof(cl_int) , (void *)&src.step ));
1170 args.push_back( make_pair( sizeof(cl_int) , (void *)&dstr.step ));
1171 args.push_back( make_pair( sizeof(cl_int) , (void *)&dstsp.step ));
1172 args.push_back( make_pair( sizeof(cl_int) , (void *)&src.offset ));
1173 args.push_back( make_pair( sizeof(cl_int) , (void *)&dstr.offset ));
1174 args.push_back( make_pair( sizeof(cl_int) , (void *)&dstsp.offset ));
1175 args.push_back( make_pair( sizeof(cl_int) , (void *)&dstr.cols ));
1176 args.push_back( make_pair( sizeof(cl_int) , (void *)&dstr.rows ));
1177 args.push_back( make_pair( sizeof(cl_int) , (void *)&sp ));
1178 args.push_back( make_pair( sizeof(cl_int) , (void *)&sr ));
1179 args.push_back( make_pair( sizeof(cl_int) , (void *)&maxIter ));
1180 args.push_back( make_pair( sizeof(cl_float) , (void *)&eps ));
1182 openCLExecuteKernel(src.clCxt, &meanShift, "meanshiftproc_kernel", globalThreads, localThreads, args, -1, -1);
1185 void meanShiftProc(const oclMat &src, oclMat &dstr, oclMat &dstsp, int sp, int sr, TermCriteria criteria)
1188 CV_Error( CV_StsBadArg, "The input image is empty" );
1190 if ( src.depth() != CV_8U || src.oclchannels() != 4 )
1191 CV_Error( CV_StsUnsupportedFormat, "Only 8-bit, 4-channel images are supported" );
1193 // if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE))
1195 // 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");
1199 dstr.create( src.size(), CV_8UC4 );
1200 dstsp.create( src.size(), CV_16SC2 );
1202 if ( !(criteria.type & TermCriteria::MAX_ITER) )
1203 criteria.maxCount = 5;
1205 int maxIter = std::min(std::max(criteria.maxCount, 1), 100);
1208 if ( !(criteria.type & TermCriteria::EPS) )
1210 eps = (float)std::max(criteria.epsilon, 0.0);
1212 meanShiftProc_gpu(src, dstr, dstsp, sp, sr, maxIter, eps);
1215 ///////////////////////////////////////////////////////////////////////////////////////////////////
1216 ////////////////////////////////////////////////////hist///////////////////////////////////////////////
1217 /////////////////////////////////////////////////////////////////////////////////////////////////////
1219 namespace histograms
1221 const int PARTIAL_HISTOGRAM256_COUNT = 256;
1222 const int HISTOGRAM256_BIN_COUNT = 256;
1224 ///////////////////////////////calcHist/////////////////////////////////////////////////////////////////
1225 static void calc_sub_hist(const oclMat &mat_src, const oclMat &mat_sub_hist)
1227 using namespace histograms;
1229 int depth = mat_src.depth();
1231 size_t localThreads[3] = { HISTOGRAM256_BIN_COUNT, 1, 1 };
1232 size_t globalThreads[3] = { PARTIAL_HISTOGRAM256_COUNT *localThreads[0], 1, 1};
1235 int dataWidth_bits = 4;
1236 int mask = dataWidth - 1;
1238 int cols = mat_src.cols * mat_src.oclchannels();
1239 int src_offset = mat_src.offset;
1240 int hist_step = mat_sub_hist.step >> 2;
1241 int left_col = 0, right_col = 0;
1243 if (cols >= dataWidth * 2 - 1)
1245 left_col = dataWidth - (src_offset & mask);
1247 src_offset += left_col;
1249 right_col = cols & mask;
1257 globalThreads[0] = 0;
1260 vector<pair<size_t , const void *> > args;
1261 if (globalThreads[0] != 0)
1263 int tempcols = cols >> dataWidth_bits;
1264 int inc_x = globalThreads[0] % tempcols;
1265 int inc_y = globalThreads[0] / tempcols;
1266 src_offset >>= dataWidth_bits;
1267 int src_step = mat_src.step >> dataWidth_bits;
1268 int datacount = tempcols * mat_src.rows;
1270 args.push_back( make_pair( sizeof(cl_mem), (void *)&mat_src.data));
1271 args.push_back( make_pair( sizeof(cl_int), (void *)&src_step));
1272 args.push_back( make_pair( sizeof(cl_int), (void *)&src_offset));
1273 args.push_back( make_pair( sizeof(cl_mem), (void *)&mat_sub_hist.data));
1274 args.push_back( make_pair( sizeof(cl_int), (void *)&datacount));
1275 args.push_back( make_pair( sizeof(cl_int), (void *)&tempcols));
1276 args.push_back( make_pair( sizeof(cl_int), (void *)&inc_x));
1277 args.push_back( make_pair( sizeof(cl_int), (void *)&inc_y));
1278 args.push_back( make_pair( sizeof(cl_int), (void *)&hist_step));
1280 openCLExecuteKernel(mat_src.clCxt, &imgproc_histogram, "calc_sub_hist", globalThreads, localThreads, args, -1, depth);
1283 if (left_col != 0 || right_col != 0)
1285 src_offset = mat_src.offset;
1286 localThreads[0] = 1;
1287 localThreads[1] = 256;
1288 globalThreads[0] = left_col + right_col;
1289 globalThreads[1] = mat_src.rows;
1292 args.push_back( make_pair( sizeof(cl_mem), (void *)&mat_src.data));
1293 args.push_back( make_pair( sizeof(cl_int), (void *)&mat_src.step));
1294 args.push_back( make_pair( sizeof(cl_int), (void *)&src_offset));
1295 args.push_back( make_pair( sizeof(cl_mem), (void *)&mat_sub_hist.data));
1296 args.push_back( make_pair( sizeof(cl_int), (void *)&left_col));
1297 args.push_back( make_pair( sizeof(cl_int), (void *)&cols));
1298 args.push_back( make_pair( sizeof(cl_int), (void *)&mat_src.rows));
1299 args.push_back( make_pair( sizeof(cl_int), (void *)&hist_step));
1301 openCLExecuteKernel(mat_src.clCxt, &imgproc_histogram, "calc_sub_hist_border", globalThreads, localThreads, args, -1, depth);
1305 static void merge_sub_hist(const oclMat &sub_hist, oclMat &mat_hist)
1307 using namespace histograms;
1309 size_t localThreads[3] = { 256, 1, 1 };
1310 size_t globalThreads[3] = { HISTOGRAM256_BIN_COUNT *localThreads[0], 1, 1};
1311 int src_step = sub_hist.step >> 2;
1313 vector<pair<size_t , const void *> > args;
1314 args.push_back( make_pair( sizeof(cl_mem), (void *)&sub_hist.data));
1315 args.push_back( make_pair( sizeof(cl_mem), (void *)&mat_hist.data));
1316 args.push_back( make_pair( sizeof(cl_int), (void *)&src_step));
1318 openCLExecuteKernel(sub_hist.clCxt, &imgproc_histogram, "merge_hist", globalThreads, localThreads, args, -1, -1);
1321 void calcHist(const oclMat &mat_src, oclMat &mat_hist)
1323 using namespace histograms;
1324 CV_Assert(mat_src.type() == CV_8UC1);
1325 mat_hist.create(1, 256, CV_32SC1);
1327 oclMat buf(PARTIAL_HISTOGRAM256_COUNT, HISTOGRAM256_BIN_COUNT, CV_32SC1);
1330 calc_sub_hist(mat_src, buf);
1331 merge_sub_hist(buf, mat_hist);
1334 ///////////////////////////////////equalizeHist/////////////////////////////////////////////////////
1335 void equalizeHist(const oclMat &mat_src, oclMat &mat_dst)
1337 mat_dst.create(mat_src.rows, mat_src.cols, CV_8UC1);
1339 oclMat mat_hist(1, 256, CV_32SC1);
1341 calcHist(mat_src, mat_hist);
1343 size_t localThreads[3] = { 256, 1, 1};
1344 size_t globalThreads[3] = { 256, 1, 1};
1345 oclMat lut(1, 256, CV_8UC1);
1346 int total = mat_src.rows * mat_src.cols;
1348 vector<pair<size_t , const void *> > args;
1349 args.push_back( make_pair( sizeof(cl_mem), (void *)&lut.data));
1350 args.push_back( make_pair( sizeof(cl_mem), (void *)&mat_hist.data));
1351 args.push_back( make_pair( sizeof(int), (void *)&total));
1353 openCLExecuteKernel(mat_src.clCxt, &imgproc_histogram, "calLUT", globalThreads, localThreads, args, -1, -1);
1354 LUT(mat_src, lut, mat_dst);
1357 ////////////////////////////////////////////////////////////////////////
1361 static void calcLut(const oclMat &src, oclMat &dst,
1362 const int tilesX, const int tilesY, const cv::Size tileSize,
1363 const int clipLimit, const float lutScale)
1366 tile_size.s[0] = tileSize.width;
1367 tile_size.s[1] = tileSize.height;
1369 std::vector<pair<size_t , const void *> > args;
1370 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&src.data ));
1371 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst.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_int2), (void *)&tile_size ));
1375 args.push_back( std::make_pair( sizeof(cl_int), (void *)&tilesX ));
1376 args.push_back( std::make_pair( sizeof(cl_int), (void *)&clipLimit ));
1377 args.push_back( std::make_pair( sizeof(cl_float), (void *)&lutScale ));
1378 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.offset ));
1379 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.offset ));
1381 String kernelName = "calcLut";
1382 size_t localThreads[3] = { 32, 8, 1 };
1383 size_t globalThreads[3] = { tilesX * localThreads[0], tilesY * localThreads[1], 1 };
1384 bool is_cpu = isCpuDevice();
1386 openCLExecuteKernel(Context::getContext(), &imgproc_clahe, kernelName, globalThreads, localThreads, args, -1, -1, (char*)"-D CPU");
1389 cl_kernel kernel = openCLGetKernelFromSource(Context::getContext(), &imgproc_clahe, kernelName);
1390 int wave_size = (int)queryWaveFrontSize(kernel);
1391 openCLSafeCall(clReleaseKernel(kernel));
1393 std::string opt = format("-D WAVE_SIZE=%d", wave_size);
1394 openCLExecuteKernel(Context::getContext(), &imgproc_clahe, kernelName, globalThreads, localThreads, args, -1, -1, opt.c_str());
1398 static void transform(const oclMat &src, oclMat &dst, const oclMat &lut,
1399 const int tilesX, const int tilesY, const Size & tileSize)
1402 tile_size.s[0] = tileSize.width;
1403 tile_size.s[1] = tileSize.height;
1405 std::vector<pair<size_t , const void *> > args;
1406 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&src.data ));
1407 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst.data ));
1408 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&lut.data ));
1409 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.step ));
1410 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.step ));
1411 args.push_back( std::make_pair( sizeof(cl_int), (void *)&lut.step ));
1412 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.cols ));
1413 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.rows ));
1414 args.push_back( std::make_pair( sizeof(cl_int2), (void *)&tile_size ));
1415 args.push_back( std::make_pair( sizeof(cl_int), (void *)&tilesX ));
1416 args.push_back( std::make_pair( sizeof(cl_int), (void *)&tilesY ));
1417 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.offset ));
1418 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.offset ));
1419 args.push_back( std::make_pair( sizeof(cl_int), (void *)&lut.offset ));
1421 size_t localThreads[3] = { 32, 8, 1 };
1422 size_t globalThreads[3] = { src.cols, src.rows, 1 };
1424 openCLExecuteKernel(Context::getContext(), &imgproc_clahe, "transform", globalThreads, localThreads, args, -1, -1);
1430 class CLAHE_Impl : public cv::CLAHE
1433 CLAHE_Impl(double clipLimit = 40.0, int tilesX = 8, int tilesY = 8);
1435 cv::AlgorithmInfo* info() const;
1437 void apply(cv::InputArray src, cv::OutputArray dst);
1439 void setClipLimit(double clipLimit);
1440 double getClipLimit() const;
1442 void setTilesGridSize(cv::Size tileGridSize);
1443 cv::Size getTilesGridSize() const;
1445 void collectGarbage();
1456 CLAHE_Impl::CLAHE_Impl(double clipLimit, int tilesX, int tilesY) :
1457 clipLimit_(clipLimit), tilesX_(tilesX), tilesY_(tilesY)
1461 CV_INIT_ALGORITHM(CLAHE_Impl, "CLAHE_OCL",
1462 obj.info()->addParam(obj, "clipLimit", obj.clipLimit_);
1463 obj.info()->addParam(obj, "tilesX", obj.tilesX_);
1464 obj.info()->addParam(obj, "tilesY", obj.tilesY_))
1466 void CLAHE_Impl::apply(cv::InputArray src_raw, cv::OutputArray dst_raw)
1468 oclMat& src = getOclMatRef(src_raw);
1469 oclMat& dst = getOclMatRef(dst_raw);
1470 CV_Assert( src.type() == CV_8UC1 );
1472 dst.create( src.size(), src.type() );
1474 const int histSize = 256;
1476 ensureSizeIsEnough(tilesX_ * tilesY_, histSize, CV_8UC1, lut_);
1481 if (src.cols % tilesX_ == 0 && src.rows % tilesY_ == 0)
1483 tileSize = cv::Size(src.cols / tilesX_, src.rows / tilesY_);
1488 ocl::copyMakeBorder(src, srcExt_, 0, tilesY_ - (src.rows % tilesY_), 0,
1489 tilesX_ - (src.cols % tilesX_), BORDER_REFLECT_101, Scalar::all(0));
1491 tileSize = Size(srcExt_.cols / tilesX_, srcExt_.rows / tilesY_);
1492 srcForLut = srcExt_;
1495 const int tileSizeTotal = tileSize.area();
1496 const float lutScale = static_cast<float>(histSize - 1) / tileSizeTotal;
1499 if (clipLimit_ > 0.0)
1501 clipLimit = static_cast<int>(clipLimit_ * tileSizeTotal / histSize);
1502 clipLimit = std::max(clipLimit, 1);
1505 clahe::calcLut(srcForLut, lut_, tilesX_, tilesY_, tileSize, clipLimit, lutScale);
1506 clahe::transform(src, dst, lut_, tilesX_, tilesY_, tileSize);
1509 void CLAHE_Impl::setClipLimit(double clipLimit)
1511 clipLimit_ = clipLimit;
1514 double CLAHE_Impl::getClipLimit() const
1519 void CLAHE_Impl::setTilesGridSize(cv::Size tileGridSize)
1521 tilesX_ = tileGridSize.width;
1522 tilesY_ = tileGridSize.height;
1525 cv::Size CLAHE_Impl::getTilesGridSize() const
1527 return cv::Size(tilesX_, tilesY_);
1530 void CLAHE_Impl::collectGarbage()
1537 cv::Ptr<cv::CLAHE> createCLAHE(double clipLimit, cv::Size tileGridSize)
1539 return new CLAHE_Impl(clipLimit, tileGridSize.width, tileGridSize.height);
1542 //////////////////////////////////bilateralFilter////////////////////////////////////////////////////
1544 static void oclbilateralFilter_8u( const oclMat &src, oclMat &dst, int d,
1545 double sigma_color, double sigma_space,
1548 int cn = src.channels();
1549 int i, j, maxk, radius;
1551 CV_Assert( (src.channels() == 1 || src.channels() == 3) &&
1552 src.type() == dst.type() && src.size() == dst.size() &&
1553 src.data != dst.data );
1555 if ( sigma_color <= 0 )
1557 if ( sigma_space <= 0 )
1560 double gauss_color_coeff = -0.5 / (sigma_color * sigma_color);
1561 double gauss_space_coeff = -0.5 / (sigma_space * sigma_space);
1564 radius = cvRound(sigma_space * 1.5);
1567 radius = MAX(radius, 1);
1571 copyMakeBorder( src, temp, radius, radius, radius, radius, borderType );
1573 vector<float> _color_weight(cn * 256);
1574 vector<float> _space_weight(d * d);
1575 vector<int> _space_ofs(d * d);
1576 float *color_weight = &_color_weight[0];
1577 float *space_weight = &_space_weight[0];
1578 int *space_ofs = &_space_ofs[0];
1580 int dst_step_in_pixel = dst.step / dst.elemSize();
1581 int dst_offset_in_pixel = dst.offset / dst.elemSize();
1582 int temp_step_in_pixel = temp.step / temp.elemSize();
1584 // initialize color-related bilateral filter coefficients
1585 for( i = 0; i < 256 * cn; i++ )
1586 color_weight[i] = (float)std::exp(i * i * gauss_color_coeff);
1588 // initialize space-related bilateral filter coefficients
1589 for( i = -radius, maxk = 0; i <= radius; i++ )
1590 for( j = -radius; j <= radius; j++ )
1592 double r = std::sqrt((double)i * i + (double)j * j);
1595 space_weight[maxk] = (float)std::exp(r * r * gauss_space_coeff);
1596 space_ofs[maxk++] = (int)(i * temp_step_in_pixel + j);
1599 oclMat oclcolor_weight(1, cn * 256, CV_32FC1, color_weight);
1600 oclMat oclspace_weight(1, d * d, CV_32FC1, space_weight);
1601 oclMat oclspace_ofs(1, d * d, CV_32SC1, space_ofs);
1603 string kernelName = "bilateral";
1604 size_t localThreads[3] = { 16, 16, 1 };
1605 size_t globalThreads[3] = { dst.cols, dst.rows, 1 };
1607 if ((dst.type() == CV_8UC1) && ((dst.offset & 3) == 0) && ((dst.cols & 3) == 0))
1609 kernelName = "bilateral2";
1610 globalThreads[0] = dst.cols >> 2;
1613 vector<pair<size_t , const void *> > args;
1614 args.push_back( make_pair( sizeof(cl_mem), (void *)&dst.data ));
1615 args.push_back( make_pair( sizeof(cl_mem), (void *)&temp.data ));
1616 args.push_back( make_pair( sizeof(cl_int), (void *)&dst.rows ));
1617 args.push_back( make_pair( sizeof(cl_int), (void *)&dst.cols ));
1618 args.push_back( make_pair( sizeof(cl_int), (void *)&maxk ));
1619 args.push_back( make_pair( sizeof(cl_int), (void *)&radius ));
1620 args.push_back( make_pair( sizeof(cl_int), (void *)&dst_step_in_pixel ));
1621 args.push_back( make_pair( sizeof(cl_int), (void *)&dst_offset_in_pixel ));
1622 args.push_back( make_pair( sizeof(cl_int), (void *)&temp_step_in_pixel ));
1623 args.push_back( make_pair( sizeof(cl_int), (void *)&temp.rows ));
1624 args.push_back( make_pair( sizeof(cl_int), (void *)&temp.cols ));
1625 args.push_back( make_pair( sizeof(cl_mem), (void *)&oclcolor_weight.data ));
1626 args.push_back( make_pair( sizeof(cl_mem), (void *)&oclspace_weight.data ));
1627 args.push_back( make_pair( sizeof(cl_mem), (void *)&oclspace_ofs.data ));
1629 openCLExecuteKernel(src.clCxt, &imgproc_bilateral, kernelName, globalThreads, localThreads, args, dst.oclchannels(), dst.depth());
1632 void bilateralFilter(const oclMat &src, oclMat &dst, int radius, double sigmaclr, double sigmaspc, int borderType)
1634 dst.create( src.size(), src.type() );
1635 if ( src.depth() == CV_8U )
1636 oclbilateralFilter_8u( src, dst, radius, sigmaclr, sigmaspc, borderType );
1638 CV_Error( CV_StsUnsupportedFormat, "Bilateral filtering is only implemented for CV_8U images" );
1643 //////////////////////////////////convolve////////////////////////////////////////////////////
1645 static void convolve_run(const oclMat &src, const oclMat &temp1, oclMat &dst, string kernelName, const cv::ocl::ProgramEntry* source)
1647 dst.create(src.size(), src.type());
1649 size_t localThreads[3] = { 16, 16, 1 };
1650 size_t globalThreads[3] = { dst.cols, dst.rows, 1 };
1652 int src_step = src.step / src.elemSize(), src_offset = src.offset / src.elemSize();
1653 int dst_step = dst.step / dst.elemSize(), dst_offset = dst.offset / dst.elemSize();
1654 int temp1_step = temp1.step / temp1.elemSize(), temp1_offset = temp1.offset / temp1.elemSize();
1656 vector<pair<size_t , const void *> > args;
1657 args.push_back( make_pair( sizeof(cl_mem), (void *)&src.data ));
1658 args.push_back( make_pair( sizeof(cl_mem), (void *)&temp1.data ));
1659 args.push_back( make_pair( sizeof(cl_mem), (void *)&dst.data ));
1660 args.push_back( make_pair( sizeof(cl_int), (void *)&src.rows ));
1661 args.push_back( make_pair( sizeof(cl_int), (void *)&src.cols ));
1662 args.push_back( make_pair( sizeof(cl_int), (void *)&src_step ));
1663 args.push_back( make_pair( sizeof(cl_int), (void *)&dst_step ));
1664 args.push_back( make_pair( sizeof(cl_int), (void *)&temp1_step ));
1665 args.push_back( make_pair( sizeof(cl_int), (void *)&temp1.rows ));
1666 args.push_back( make_pair( sizeof(cl_int), (void *)&temp1.cols ));
1667 args.push_back( make_pair( sizeof(cl_int), (void *)&src_offset ));
1668 args.push_back( make_pair( sizeof(cl_int), (void *)&dst_offset ));
1669 args.push_back( make_pair( sizeof(cl_int), (void *)&temp1_offset ));
1671 openCLExecuteKernel(src.clCxt, source, kernelName, globalThreads, localThreads, args, -1, dst.depth());
1674 void cv::ocl::convolve(const oclMat &x, const oclMat &t, oclMat &y)
1676 CV_Assert(x.depth() == CV_32F && t.depth() == CV_32F);
1677 CV_Assert(t.cols <= 17 && t.rows <= 17);
1679 y.create(x.size(), x.type());
1681 convolve_run(x, t, y, "convolve", &imgproc_convolve);