1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
16 // Third party copyrights are property of their respective owners.
19 // Niko Li, newlife20080214@gmail.com
20 // Jia Haipeng, jiahaipeng95@gmail.com
21 // Shengen Yan, yanshengen@gmail.com
22 // Rock Li, Rock.Li@amd.com
23 // Zero Lin, Zero.Lin@amd.com
24 // Zhang Ying, zhangying913@gmail.com
25 // Xu Pang, pangxu010@163.com
26 // Wu Zailong, bullet@yeah.net
27 // Wenju He, wenju@multicorewareinc.com
28 // Peng Xiao, pengxiao@outlook.com
29 // Sen Liu, swjtuls1987@126.com
31 // Redistribution and use in source and binary forms, with or without modification,
32 // are permitted provided that the following conditions are met:
34 // * Redistribution's of source code must retain the above copyright notice,
35 // this list of conditions and the following disclaimer.
37 // * Redistribution's in binary form must reproduce the above copyright notice,
38 // this list of conditions and the following disclaimer in the documentation
39 // and/or other materials provided with the distribution.
41 // * The name of the copyright holders may not be used to endorse or promote products
42 // derived from this software without specific prior written permission.
44 // This software is provided by the copyright holders and contributors "as is" and
45 // any express or implied warranties, including, but not limited to, the implied
46 // warranties of merchantability and fitness for a particular purpose are disclaimed.
47 // In no event shall the Intel Corporation or contributors be liable for any direct,
48 // indirect, incidental, special, exemplary, or consequential damages
49 // (including, but not limited to, procurement of substitute goods or services;
50 // loss of use, data, or profits; or business interruption) however caused
51 // and on any theory of liability, whether in contract, strict liability,
52 // or tort (including negligence or otherwise) arising in any way out of
53 // the use of this software, even if advised of the possibility of such damage.
57 #include "precomp.hpp"
58 #include "opencl_kernels.hpp"
61 using namespace cv::ocl;
67 ////////////////////////////////////OpenCL call wrappers////////////////////////////
69 template <typename T> struct index_and_sizeof;
70 template <> struct index_and_sizeof<char>
74 template <> struct index_and_sizeof<unsigned char>
78 template <> struct index_and_sizeof<short>
82 template <> struct index_and_sizeof<unsigned short>
86 template <> struct index_and_sizeof<int>
90 template <> struct index_and_sizeof<float>
94 template <> struct index_and_sizeof<double>
99 /////////////////////////////////////////////////////////////////////////////////////
102 static std::vector<uchar> scalarToVector(const cv::Scalar & sc, int depth, int ocn, int cn)
104 CV_Assert(ocn == cn || (ocn == 4 && cn == 3));
106 static const int sizeMap[] = { sizeof(uchar), sizeof(char), sizeof(ushort),
107 sizeof(short), sizeof(int), sizeof(float), sizeof(double) };
109 int elemSize1 = sizeMap[depth];
110 int bufSize = elemSize1 * ocn;
111 std::vector<uchar> _buf(bufSize);
112 uchar * buf = &_buf[0];
113 scalarToRawData(sc, buf, CV_MAKE_TYPE(depth, cn));
114 memset(buf + elemSize1 * cn, 0, (ocn - cn) * elemSize1);
119 static void threshold_runner(const oclMat &src, oclMat &dst, double thresh, double maxVal, int thresholdType)
121 bool ival = src.depth() < CV_32F;
122 int cn = src.channels(), vecSize = 4, depth = src.depth();
123 std::vector<uchar> thresholdValue = scalarToVector(cv::Scalar::all(ival ? cvFloor(thresh) : thresh), dst.depth(),
124 dst.oclchannels(), dst.channels());
125 std::vector<uchar> maxValue = scalarToVector(cv::Scalar::all(maxVal), dst.depth(), dst.oclchannels(), dst.channels());
127 const char * const thresholdMap[] = { "THRESH_BINARY", "THRESH_BINARY_INV", "THRESH_TRUNC",
128 "THRESH_TOZERO", "THRESH_TOZERO_INV" };
129 const char * const channelMap[] = { "", "", "2", "4", "4" };
130 const char * const typeMap[] = { "uchar", "char", "ushort", "short", "int", "float", "double" };
131 std::string buildOptions = format("-D T=%s%s -D %s", typeMap[depth], channelMap[cn], thresholdMap[thresholdType]);
133 int elemSize = src.elemSize();
134 int src_step = src.step / elemSize, src_offset = src.offset / elemSize;
135 int dst_step = dst.step / elemSize, dst_offset = dst.offset / elemSize;
137 std::vector< std::pair<size_t, const void *> > args;
138 args.push_back( std::make_pair(sizeof(cl_mem), (void *)&src.data));
139 args.push_back( std::make_pair(sizeof(cl_int), (void *)&src_offset));
140 args.push_back( std::make_pair(sizeof(cl_int), (void *)&src_step));
141 args.push_back( std::make_pair(sizeof(cl_mem), (void *)&dst.data));
142 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst_offset));
143 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst_step));
144 args.push_back( std::make_pair(thresholdValue.size(), (void *)&thresholdValue[0]));
145 args.push_back( std::make_pair(maxValue.size(), (void *)&maxValue[0]));
147 int max_index = dst.cols, cols = dst.cols;
148 if (cn == 1 && vecSize > 1)
150 CV_Assert(((vecSize - 1) & vecSize) == 0 && vecSize <= 16);
151 cols = divUp(cols, vecSize);
152 buildOptions += format(" -D VECTORIZED -D VT=%s%d -D VLOADN=vload%d -D VECSIZE=%d -D VSTOREN=vstore%d",
153 typeMap[depth], vecSize, vecSize, vecSize, vecSize);
155 int vecSizeBytes = vecSize * dst.elemSize1();
156 if ((dst.offset % dst.step) % vecSizeBytes == 0 && dst.step % vecSizeBytes == 0)
157 buildOptions += " -D DST_ALIGNED";
158 if ((src.offset % src.step) % vecSizeBytes == 0 && src.step % vecSizeBytes == 0)
159 buildOptions += " -D SRC_ALIGNED";
161 args.push_back( std::make_pair(sizeof(cl_int), (void *)&max_index));
164 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.rows));
165 args.push_back( std::make_pair(sizeof(cl_int), (void *)&cols));
167 size_t localThreads[3] = { 16, 16, 1 };
168 size_t globalThreads[3] = { cols, dst.rows, 1 };
170 openCLExecuteKernel(src.clCxt, &imgproc_threshold, "threshold", globalThreads, localThreads, args,
171 -1, -1, buildOptions.c_str());
174 double threshold(const oclMat &src, oclMat &dst, double thresh, double maxVal, int thresholdType)
176 CV_Assert(thresholdType == THRESH_BINARY || thresholdType == THRESH_BINARY_INV || thresholdType == THRESH_TRUNC
177 || thresholdType == THRESH_TOZERO || thresholdType == THRESH_TOZERO_INV);
179 dst.create(src.size(), src.type());
180 threshold_runner(src, dst, thresh, maxVal, thresholdType);
185 ////////////////////////////////////////////////////////////////////////////////////////////
186 /////////////////////////////// remap //////////////////////////////////////////////////
187 ////////////////////////////////////////////////////////////////////////////////////////////
189 void remap( const oclMat &src, oclMat &dst, oclMat &map1, oclMat &map2, int interpolation, int borderType, const Scalar &borderValue )
191 Context *clCxt = src.clCxt;
192 bool supportsDouble = clCxt->supportsFeature(FEATURE_CL_DOUBLE);
193 if (!supportsDouble && src.depth() == CV_64F)
195 CV_Error(CV_OpenCLDoubleNotSupported, "Selected device does not support double");
199 CV_Assert(interpolation == INTER_LINEAR || interpolation == INTER_NEAREST
200 || interpolation == INTER_CUBIC || interpolation == INTER_LANCZOS4);
201 CV_Assert((map1.type() == CV_16SC2 && !map2.data) || (map1.type() == CV_32FC2 && !map2.data) ||
202 (map1.type() == CV_32FC1 && map2.type() == CV_32FC1));
203 CV_Assert(!map2.data || map2.size() == map1.size());
204 CV_Assert(borderType == BORDER_CONSTANT || borderType == BORDER_REPLICATE || borderType == BORDER_WRAP
205 || borderType == BORDER_REFLECT_101 || borderType == BORDER_REFLECT);
207 dst.create(map1.size(), src.type());
209 const char * const typeMap[] = { "uchar", "char", "ushort", "short", "int", "float", "double" };
210 const char * const channelMap[] = { "", "", "2", "4", "4" };
211 const char * const interMap[] = { "INTER_NEAREST", "INTER_LINEAR", "INTER_CUBIC", "INTER_LINEAR", "INTER_LANCZOS" };
212 const char * const borderMap[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", "BORDER_WRAP",
213 "BORDER_REFLECT_101", "BORDER_TRANSPARENT" };
215 String kernelName = "remap";
216 if ( map1.type() == CV_32FC2 && !map2.data )
217 kernelName = kernelName + "_32FC2";
218 else if (map1.type() == CV_16SC2 && !map2.data)
219 kernelName = kernelName + "_16SC2";
220 else if (map1.type() == CV_32FC1 && map2.type() == CV_32FC1)
221 kernelName = kernelName + "_2_32FC1";
223 CV_Error(Error::StsBadArg, "Unsupported map types");
225 int ocn = dst.oclchannels();
226 size_t localThreads[3] = { 16, 16, 1};
227 size_t globalThreads[3] = { dst.cols, dst.rows, 1};
229 Mat scalar(1, 1, CV_MAKE_TYPE(dst.depth(), ocn), borderValue);
230 String buildOptions = format("-D %s -D %s -D T=%s%s", interMap[interpolation],
231 borderMap[borderType], typeMap[src.depth()], channelMap[ocn]);
233 if (interpolation != INTER_NEAREST)
235 int wdepth = std::max(CV_32F, dst.depth());
237 wdepth = std::min(CV_32F, wdepth);
239 buildOptions = buildOptions
240 + format(" -D WT=%s%s -D convertToT=convert_%s%s%s -D convertToWT=convert_%s%s"
241 " -D convertToWT2=convert_%s2 -D WT2=%s2",
242 typeMap[wdepth], channelMap[ocn],
243 typeMap[src.depth()], channelMap[ocn], src.depth() < CV_32F ? "_sat_rte" : "",
244 typeMap[wdepth], channelMap[ocn],
245 typeMap[wdepth], typeMap[wdepth]);
248 int src_step = src.step / src.elemSize(), src_offset = src.offset / src.elemSize();
249 int map1_step = map1.step / map1.elemSize(), map1_offset = map1.offset / map1.elemSize();
250 int map2_step = map2.step / map2.elemSize(), map2_offset = map2.offset / map2.elemSize();
251 int dst_step = dst.step / dst.elemSize(), dst_offset = dst.offset / dst.elemSize();
253 std::vector< std::pair<size_t, const void *> > args;
254 args.push_back( std::make_pair(sizeof(cl_mem), (void *)&src.data));
255 args.push_back( std::make_pair(sizeof(cl_mem), (void *)&dst.data));
256 args.push_back( std::make_pair(sizeof(cl_mem), (void *)&map1.data));
258 args.push_back( std::make_pair(sizeof(cl_mem), (void *)&map2.data));
259 args.push_back( std::make_pair(sizeof(cl_int), (void *)&src_offset));
260 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst_offset));
261 args.push_back( std::make_pair(sizeof(cl_int), (void *)&map1_offset));
263 args.push_back( std::make_pair(sizeof(cl_int), (void *)&map2_offset));
264 args.push_back( std::make_pair(sizeof(cl_int), (void *)&src_step));
265 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst_step));
266 args.push_back( std::make_pair(sizeof(cl_int), (void *)&map1_step));
268 args.push_back( std::make_pair(sizeof(cl_int), (void *)&map2_step));
269 args.push_back( std::make_pair(sizeof(cl_int), (void *)&src.cols));
270 args.push_back( std::make_pair(sizeof(cl_int), (void *)&src.rows));
271 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.cols));
272 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.rows));
273 args.push_back( std::make_pair(scalar.elemSize(), (void *)scalar.data));
275 openCLExecuteKernel(clCxt, &imgproc_remap, kernelName, globalThreads, localThreads, args, -1, -1, buildOptions.c_str());
278 ////////////////////////////////////////////////////////////////////////////////////////////
281 static void resize_gpu( const oclMat &src, oclMat &dst, double fx, double fy, int interpolation)
283 CV_Assert( (src.channels() == dst.channels()) );
284 Context *clCxt = src.clCxt;
287 double ifx_d = 1. / fx;
288 double ify_d = 1. / fy;
289 int srcStep_in_pixel = src.step1() / src.oclchannels();
290 int srcoffset_in_pixel = src.offset / src.elemSize();
291 int dstStep_in_pixel = dst.step1() / dst.oclchannels();
292 int dstoffset_in_pixel = dst.offset / dst.elemSize();
295 if (interpolation == INTER_LINEAR)
296 kernelName = "resizeLN";
297 else if (interpolation == INTER_NEAREST)
298 kernelName = "resizeNN";
300 //TODO: improve this kernel
301 size_t blkSizeX = 16, blkSizeY = 16;
303 if (src.type() == CV_8UC1)
305 size_t cols = (dst.cols + dst.offset % 4 + 3) / 4;
306 glbSizeX = cols % blkSizeX == 0 && cols != 0 ? cols : (cols / blkSizeX + 1) * blkSizeX;
309 glbSizeX = dst.cols % blkSizeX == 0 && dst.cols != 0 ? dst.cols : (dst.cols / blkSizeX + 1) * blkSizeX;
311 size_t glbSizeY = dst.rows % blkSizeY == 0 && dst.rows != 0 ? dst.rows : (dst.rows / blkSizeY + 1) * blkSizeY;
312 size_t globalThreads[3] = {glbSizeX, glbSizeY, 1};
313 size_t localThreads[3] = {blkSizeX, blkSizeY, 1};
315 std::vector< std::pair<size_t, const void *> > args;
316 if (interpolation == INTER_NEAREST)
318 args.push_back( std::make_pair(sizeof(cl_mem), (void *)&dst.data));
319 args.push_back( std::make_pair(sizeof(cl_mem), (void *)&src.data));
320 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dstoffset_in_pixel));
321 args.push_back( std::make_pair(sizeof(cl_int), (void *)&srcoffset_in_pixel));
322 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dstStep_in_pixel));
323 args.push_back( std::make_pair(sizeof(cl_int), (void *)&srcStep_in_pixel));
324 args.push_back( std::make_pair(sizeof(cl_int), (void *)&src.cols));
325 args.push_back( std::make_pair(sizeof(cl_int), (void *)&src.rows));
326 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.cols));
327 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.rows));
328 if (src.clCxt->supportsFeature(FEATURE_CL_DOUBLE))
330 args.push_back( std::make_pair(sizeof(cl_double), (void *)&ifx_d));
331 args.push_back( std::make_pair(sizeof(cl_double), (void *)&ify_d));
335 args.push_back( std::make_pair(sizeof(cl_float), (void *)&ifx));
336 args.push_back( std::make_pair(sizeof(cl_float), (void *)&ify));
341 args.push_back( std::make_pair(sizeof(cl_mem), (void *)&dst.data));
342 args.push_back( std::make_pair(sizeof(cl_mem), (void *)&src.data));
343 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dstoffset_in_pixel));
344 args.push_back( std::make_pair(sizeof(cl_int), (void *)&srcoffset_in_pixel));
345 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dstStep_in_pixel));
346 args.push_back( std::make_pair(sizeof(cl_int), (void *)&srcStep_in_pixel));
347 args.push_back( std::make_pair(sizeof(cl_int), (void *)&src.cols));
348 args.push_back( std::make_pair(sizeof(cl_int), (void *)&src.rows));
349 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.cols));
350 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.rows));
351 args.push_back( std::make_pair(sizeof(cl_float), (void *)&ifx));
352 args.push_back( std::make_pair(sizeof(cl_float), (void *)&ify));
355 openCLExecuteKernel(clCxt, &imgproc_resize, kernelName, globalThreads, localThreads, args, src.oclchannels(), src.depth());
358 void resize(const oclMat &src, oclMat &dst, Size dsize,
359 double fx, double fy, int interpolation)
361 CV_Assert(src.type() == CV_8UC1 || src.type() == CV_8UC3 || src.type() == CV_8UC4
362 || src.type() == CV_32FC1 || src.type() == CV_32FC3 || src.type() == CV_32FC4);
363 CV_Assert(interpolation == INTER_LINEAR || interpolation == INTER_NEAREST);
364 CV_Assert( src.size().area() > 0 );
365 CV_Assert( !(dsize == Size()) || (fx > 0 && fy > 0) );
367 if (!(dsize == Size()) && (fx > 0 && fy > 0))
368 if (dsize.width != (int)(src.cols * fx) || dsize.height != (int)(src.rows * fy))
369 CV_Error(Error::StsUnmatchedSizes, "invalid dsize and fx, fy!");
371 if ( dsize == Size() )
372 dsize = Size(saturate_cast<int>(src.cols * fx), saturate_cast<int>(src.rows * fy));
375 fx = (double)dsize.width / src.cols;
376 fy = (double)dsize.height / src.rows;
379 dst.create(dsize, src.type());
381 if ( interpolation == INTER_NEAREST || interpolation == INTER_LINEAR )
383 resize_gpu( src, dst, fx, fy, interpolation);
387 CV_Error(Error::StsUnsupportedFormat, "Non-supported interpolation method");
390 ////////////////////////////////////////////////////////////////////////
393 void medianFilter(const oclMat &src, oclMat &dst, int m)
395 CV_Assert( m % 2 == 1 && m > 1 );
396 CV_Assert( (src.depth() == CV_8U || src.depth() == CV_32F) && (src.channels() == 1 || src.channels() == 4));
397 dst.create(src.size(), src.type());
399 int srcStep = src.step / src.elemSize(), dstStep = dst.step / dst.elemSize();
400 int srcOffset = src.offset / src.elemSize(), dstOffset = dst.offset / dst.elemSize();
402 Context *clCxt = src.clCxt;
404 std::vector< std::pair<size_t, const void *> > args;
405 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&src.data));
406 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst.data));
407 args.push_back( std::make_pair( sizeof(cl_int), (void *)&srcOffset));
408 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dstOffset));
409 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.cols));
410 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.rows));
411 args.push_back( std::make_pair( sizeof(cl_int), (void *)&srcStep));
412 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dstStep));
414 size_t globalThreads[3] = {(src.cols + 18) / 16 * 16, (src.rows + 15) / 16 * 16, 1};
415 size_t localThreads[3] = {16, 16, 1};
419 String kernelName = "medianFilter3";
420 openCLExecuteKernel(clCxt, &imgproc_median, kernelName, globalThreads, localThreads, args, src.oclchannels(), src.depth());
424 String kernelName = "medianFilter5";
425 openCLExecuteKernel(clCxt, &imgproc_median, kernelName, globalThreads, localThreads, args, src.oclchannels(), src.depth());
428 CV_Error(Error::StsBadArg, "Non-supported filter length");
431 ////////////////////////////////////////////////////////////////////////
434 void copyMakeBorder(const oclMat &src, oclMat &dst, int top, int bottom, int left, int right, int bordertype, const Scalar &scalar)
436 if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
438 CV_Error(Error::OpenCLDoubleNotSupported, "Selected device does not support double");
444 CV_Assert(top >= 0 && bottom >= 0 && left >= 0 && right >= 0);
446 if( (_src.wholecols != _src.cols || _src.wholerows != _src.rows) && (bordertype & BORDER_ISOLATED) == 0 )
450 _src.locateROI(wholeSize, ofs);
451 int dtop = std::min(ofs.y, top);
452 int dbottom = std::min(wholeSize.height - _src.rows - ofs.y, bottom);
453 int dleft = std::min(ofs.x, left);
454 int dright = std::min(wholeSize.width - _src.cols - ofs.x, right);
455 _src.adjustROI(dtop, dbottom, dleft, dright);
461 bordertype &= ~cv::BORDER_ISOLATED;
463 dst.create(_src.rows + top + bottom, _src.cols + left + right, _src.type());
464 int srcStep = _src.step / _src.elemSize(), dstStep = dst.step / dst.elemSize();
465 int srcOffset = _src.offset / _src.elemSize(), dstOffset = dst.offset / dst.elemSize();
466 int depth = _src.depth(), ochannels = _src.oclchannels();
468 int __bordertype[] = { BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT, BORDER_WRAP, BORDER_REFLECT_101 };
469 const char *borderstr[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", "BORDER_WRAP", "BORDER_REFLECT_101" };
471 int bordertype_index = -1;
472 for (int i = 0, end = sizeof(__bordertype) / sizeof(int); i < end; i++)
473 if (__bordertype[i] == bordertype)
475 bordertype_index = i;
478 if (bordertype_index < 0)
479 CV_Error(Error::StsBadArg, "Unsupported border type");
481 size_t localThreads[3] = { 16, 16, 1 };
482 size_t globalThreads[3] = { dst.cols, dst.rows, 1 };
484 std::vector< std::pair<size_t, const void *> > args;
485 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&_src.data));
486 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst.data));
487 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.cols));
488 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.rows));
489 args.push_back( std::make_pair( sizeof(cl_int), (void *)&_src.cols));
490 args.push_back( std::make_pair( sizeof(cl_int), (void *)&_src.rows));
491 args.push_back( std::make_pair( sizeof(cl_int), (void *)&srcStep));
492 args.push_back( std::make_pair( sizeof(cl_int), (void *)&srcOffset));
493 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dstStep));
494 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dstOffset));
495 args.push_back( std::make_pair( sizeof(cl_int), (void *)&top));
496 args.push_back( std::make_pair( sizeof(cl_int), (void *)&left));
498 const char * const typeMap[] = { "uchar", "char", "ushort", "short", "int", "float", "double" };
499 const char * const channelMap[] = { "", "", "2", "4", "4" };
500 std::string buildOptions = format("-D GENTYPE=%s%s -D %s",
501 typeMap[depth], channelMap[ochannels],
502 borderstr[bordertype_index]);
504 int cn = src.channels(), ocn = src.oclchannels();
505 int bufSize = src.elemSize1() * ocn;
506 AutoBuffer<uchar> _buf(bufSize);
507 uchar * buf = (uchar *)_buf;
508 scalarToRawData(scalar, buf, dst.type());
509 memset(buf + src.elemSize1() * cn, 0, (ocn - cn) * src.elemSize1());
511 args.push_back( std::make_pair( bufSize , (void *)buf ));
513 openCLExecuteKernel(src.clCxt, &imgproc_copymakeboder, "copymakeborder", globalThreads,
514 localThreads, args, -1, -1, buildOptions.c_str());
517 ////////////////////////////////////////////////////////////////////////
524 void convert_coeffs(F *M)
526 double D = M[0] * M[4] - M[1] * M[3];
527 D = D != 0 ? 1. / D : 0;
528 double A11 = M[4] * D, A22 = M[0] * D;
533 double b1 = -M[0] * M[2] - M[1] * M[5];
534 double b2 = -M[3] * M[2] - M[4] * M[5];
539 double invert(double *M)
541 #define Sd(y,x) (Sd[y*3+x])
542 #define Dd(y,x) (Dd[y*3+x])
543 #define det3(m) (m(0,0)*(m(1,1)*m(2,2) - m(1,2)*m(2,1)) - \
544 m(0,1)*(m(1,0)*m(2,2) - m(1,2)*m(2,0)) + \
545 m(0,2)*(m(1,0)*m(2,1) - m(1,1)*m(2,0)))
556 t[0] = (Sd(1, 1) * Sd(2, 2) - Sd(1, 2) * Sd(2, 1)) * d;
557 t[1] = (Sd(0, 2) * Sd(2, 1) - Sd(0, 1) * Sd(2, 2)) * d;
558 t[2] = (Sd(0, 1) * Sd(1, 2) - Sd(0, 2) * Sd(1, 1)) * d;
560 t[3] = (Sd(1, 2) * Sd(2, 0) - Sd(1, 0) * Sd(2, 2)) * d;
561 t[4] = (Sd(0, 0) * Sd(2, 2) - Sd(0, 2) * Sd(2, 0)) * d;
562 t[5] = (Sd(0, 2) * Sd(1, 0) - Sd(0, 0) * Sd(1, 2)) * d;
564 t[6] = (Sd(1, 0) * Sd(2, 1) - Sd(1, 1) * Sd(2, 0)) * d;
565 t[7] = (Sd(0, 1) * Sd(2, 0) - Sd(0, 0) * Sd(2, 1)) * d;
566 t[8] = (Sd(0, 0) * Sd(1, 1) - Sd(0, 1) * Sd(1, 0)) * d;
581 void warpAffine_gpu(const oclMat &src, oclMat &dst, F coeffs[2][3], int interpolation)
583 CV_Assert( (src.oclchannels() == dst.oclchannels()) );
584 int srcStep = src.step1();
585 int dstStep = dst.step1();
586 float float_coeffs[2][3];
589 Context *clCxt = src.clCxt;
590 String s[3] = {"NN", "Linear", "Cubic"};
591 String kernelName = "warpAffine" + s[interpolation];
593 if (src.clCxt->supportsFeature(FEATURE_CL_DOUBLE))
596 coeffs_cm = clCreateBuffer(*(cl_context*)clCxt->getOpenCLContextPtr(), CL_MEM_READ_WRITE, sizeof(F) * 2 * 3, NULL, &st );
597 openCLVerifyCall(st);
598 openCLSafeCall(clEnqueueWriteBuffer(*(cl_command_queue*)clCxt->getOpenCLCommandQueuePtr(), (cl_mem)coeffs_cm, 1, 0,
599 sizeof(F) * 2 * 3, coeffs, 0, 0, 0));
604 for(int m = 0; m < 2; m++)
605 for(int n = 0; n < 3; n++)
606 float_coeffs[m][n] = coeffs[m][n];
608 coeffs_cm = clCreateBuffer(*(cl_context*)clCxt->getOpenCLContextPtr(), CL_MEM_READ_WRITE, sizeof(float) * 2 * 3, NULL, &st );
609 openCLSafeCall(clEnqueueWriteBuffer(*(cl_command_queue*)clCxt->getOpenCLCommandQueuePtr(), (cl_mem)coeffs_cm,
610 1, 0, sizeof(float) * 2 * 3, float_coeffs, 0, 0, 0));
613 //TODO: improve this kernel
614 size_t blkSizeX = 16, blkSizeY = 16;
618 if (src.type() == CV_8UC1 && interpolation != 2)
620 cols = (dst.cols + dst.offset % 4 + 3) / 4;
621 glbSizeX = cols % blkSizeX == 0 ? cols : (cols / blkSizeX + 1) * blkSizeX;
626 glbSizeX = dst.cols % blkSizeX == 0 ? dst.cols : (dst.cols / blkSizeX + 1) * blkSizeX;
629 size_t glbSizeY = dst.rows % blkSizeY == 0 ? dst.rows : (dst.rows / blkSizeY + 1) * blkSizeY;
630 size_t globalThreads[3] = {glbSizeX, glbSizeY, 1};
631 size_t localThreads[3] = {blkSizeX, blkSizeY, 1};
633 std::vector< std::pair<size_t, const void *> > args;
635 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&src.data));
636 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&dst.data));
637 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.cols));
638 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.rows));
639 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.cols));
640 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.rows));
641 args.push_back(std::make_pair(sizeof(cl_int), (void *)&srcStep));
642 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dstStep));
643 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.offset));
644 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.offset));
645 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&coeffs_cm));
646 args.push_back(std::make_pair(sizeof(cl_int), (void *)&cols));
648 openCLExecuteKernel(clCxt, &imgproc_warpAffine, kernelName, globalThreads, localThreads, args, src.oclchannels(), src.depth());
649 openCLSafeCall(clReleaseMemObject(coeffs_cm));
652 void warpPerspective_gpu(const oclMat &src, oclMat &dst, double coeffs[3][3], int interpolation)
654 CV_Assert( (src.oclchannels() == dst.oclchannels()) );
655 int srcStep = src.step1();
656 int dstStep = dst.step1();
657 float float_coeffs[3][3];
660 Context *clCxt = src.clCxt;
661 String s[3] = {"NN", "Linear", "Cubic"};
662 String kernelName = "warpPerspective" + s[interpolation];
664 if (src.clCxt->supportsFeature(FEATURE_CL_DOUBLE))
667 coeffs_cm = clCreateBuffer(*(cl_context*)clCxt->getOpenCLContextPtr(), CL_MEM_READ_WRITE, sizeof(double) * 3 * 3, NULL, &st );
668 openCLVerifyCall(st);
669 openCLSafeCall(clEnqueueWriteBuffer(*(cl_command_queue*)clCxt->getOpenCLCommandQueuePtr(), (cl_mem)coeffs_cm, 1, 0,
670 sizeof(double) * 3 * 3, coeffs, 0, 0, 0));
675 for(int m = 0; m < 3; m++)
676 for(int n = 0; n < 3; n++)
677 float_coeffs[m][n] = coeffs[m][n];
679 coeffs_cm = clCreateBuffer(*(cl_context*)clCxt->getOpenCLContextPtr(), CL_MEM_READ_WRITE, sizeof(float) * 3 * 3, NULL, &st );
680 openCLVerifyCall(st);
681 openCLSafeCall(clEnqueueWriteBuffer(*(cl_command_queue*)clCxt->getOpenCLCommandQueuePtr(), (cl_mem)coeffs_cm, 1, 0,
682 sizeof(float) * 3 * 3, float_coeffs, 0, 0, 0));
685 //TODO: improve this kernel
686 size_t blkSizeX = 16, blkSizeY = 16;
689 if (src.type() == CV_8UC1 && interpolation == 0)
691 cols = (dst.cols + dst.offset % 4 + 3) / 4;
692 glbSizeX = cols % blkSizeX == 0 ? cols : (cols / blkSizeX + 1) * blkSizeX;
697 glbSizeX = dst.cols % blkSizeX == 0 ? dst.cols : (dst.cols / blkSizeX + 1) * blkSizeX;
700 size_t glbSizeY = dst.rows % blkSizeY == 0 ? dst.rows : (dst.rows / blkSizeY + 1) * blkSizeY;
701 size_t globalThreads[3] = {glbSizeX, glbSizeY, 1};
702 size_t localThreads[3] = {blkSizeX, blkSizeY, 1};
704 std::vector< std::pair<size_t, const void *> > args;
706 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&src.data));
707 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&dst.data));
708 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.cols));
709 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.rows));
710 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.cols));
711 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.rows));
712 args.push_back(std::make_pair(sizeof(cl_int), (void *)&srcStep));
713 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dstStep));
714 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.offset));
715 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.offset));
716 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&coeffs_cm));
717 args.push_back(std::make_pair(sizeof(cl_int), (void *)&cols));
719 openCLExecuteKernel(clCxt, &imgproc_warpPerspective, kernelName, globalThreads, localThreads, args, src.oclchannels(), src.depth());
720 openCLSafeCall(clReleaseMemObject(coeffs_cm));
724 void warpAffine(const oclMat &src, oclMat &dst, const Mat &M, Size dsize, int flags)
726 int interpolation = flags & INTER_MAX;
728 CV_Assert((src.depth() == CV_8U || src.depth() == CV_32F) && src.oclchannels() != 2 && src.oclchannels() != 3);
729 CV_Assert(interpolation == INTER_NEAREST || interpolation == INTER_LINEAR || interpolation == INTER_CUBIC);
731 dst.create(dsize, src.type());
733 CV_Assert(M.rows == 2 && M.cols == 3);
735 int warpInd = (flags & WARP_INVERSE_MAP) >> 4;
739 Mat coeffsMat(2, 3, CV_64F, (void *)coeffsM);
740 M.convertTo(coeffsMat, coeffsMat.type());
742 convert_coeffs(coeffsM);
744 for(int i = 0; i < 2; ++i)
745 for(int j = 0; j < 3; ++j)
746 coeffs[i][j] = coeffsM[i*3+j];
748 warpAffine_gpu(src, dst, coeffs, interpolation);
751 void warpPerspective(const oclMat &src, oclMat &dst, const Mat &M, Size dsize, int flags)
753 int interpolation = flags & INTER_MAX;
755 CV_Assert((src.depth() == CV_8U || src.depth() == CV_32F) && src.oclchannels() != 2 && src.oclchannels() != 3);
756 CV_Assert(interpolation == INTER_NEAREST || interpolation == INTER_LINEAR || interpolation == INTER_CUBIC);
758 dst.create(dsize, src.type());
761 CV_Assert(M.rows == 3 && M.cols == 3);
763 int warpInd = (flags & WARP_INVERSE_MAP) >> 4;
767 Mat coeffsMat(3, 3, CV_64F, (void *)coeffsM);
768 M.convertTo(coeffsMat, coeffsMat.type());
772 for(int i = 0; i < 3; ++i)
773 for(int j = 0; j < 3; ++j)
774 coeffs[i][j] = coeffsM[i*3+j];
776 warpPerspective_gpu(src, dst, coeffs, interpolation);
779 ////////////////////////////////////////////////////////////////////////
782 void integral(const oclMat &src, oclMat &sum, oclMat &sqsum)
784 CV_Assert(src.type() == CV_8UC1);
785 if (!src.clCxt->supportsFeature(ocl::FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
787 CV_Error(Error::OpenCLDoubleNotSupported, "Select device doesn't support double");
792 int offset = src.offset / vlen;
793 int pre_invalid = src.offset % vlen;
794 int vcols = (pre_invalid + src.cols + vlen - 1) / vlen;
796 oclMat t_sum , t_sqsum;
797 int w = src.cols + 1, h = src.rows + 1;
798 int depth = src.depth() == CV_8U ? CV_32S : CV_64F;
799 int type = CV_MAKE_TYPE(depth, 1);
801 t_sum.create(src.cols, src.rows, type);
802 sum.create(h, w, type);
804 t_sqsum.create(src.cols, src.rows, CV_32FC1);
805 sqsum.create(h, w, CV_32FC1);
807 int sum_offset = sum.offset / vlen;
808 int sqsum_offset = sqsum.offset / vlen;
810 std::vector<std::pair<size_t , const void *> > args;
811 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&src.data ));
812 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&t_sum.data ));
813 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&t_sqsum.data ));
814 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&offset ));
815 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&pre_invalid ));
816 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.rows ));
817 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.cols ));
818 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.step ));
819 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.step));
820 size_t gt[3] = {((vcols + 1) / 2) * 256, 1, 1}, lt[3] = {256, 1, 1};
821 openCLExecuteKernel(src.clCxt, &imgproc_integral, "integral_cols", gt, lt, args, -1, depth);
824 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&t_sum.data ));
825 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&t_sqsum.data ));
826 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&sum.data ));
827 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&sqsum.data ));
828 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.rows ));
829 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.cols ));
830 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.step ));
831 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sum.step));
832 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sqsum.step));
833 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sum_offset));
834 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sqsum_offset));
835 size_t gt2[3] = {t_sum.cols * 32, 1, 1}, lt2[3] = {256, 1, 1};
836 openCLExecuteKernel(src.clCxt, &imgproc_integral, "integral_rows", gt2, lt2, args, -1, depth);
839 void integral(const oclMat &src, oclMat &sum)
841 CV_Assert(src.type() == CV_8UC1);
843 int offset = src.offset / vlen;
844 int pre_invalid = src.offset % vlen;
845 int vcols = (pre_invalid + src.cols + vlen - 1) / vlen;
848 int w = src.cols + 1, h = src.rows + 1;
849 int depth = src.depth() == CV_8U ? CV_32S : CV_32F;
850 int type = CV_MAKE_TYPE(depth, 1);
852 t_sum.create(src.cols, src.rows, type);
853 sum.create(h, w, type);
855 int sum_offset = sum.offset / vlen;
856 std::vector<std::pair<size_t , const void *> > args;
857 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&src.data ));
858 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&t_sum.data ));
859 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&offset ));
860 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&pre_invalid ));
861 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.rows ));
862 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.cols ));
863 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.step ));
864 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.step));
865 size_t gt[3] = {((vcols + 1) / 2) * 256, 1, 1}, lt[3] = {256, 1, 1};
866 openCLExecuteKernel(src.clCxt, &imgproc_integral_sum, "integral_sum_cols", gt, lt, args, -1, depth);
869 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&t_sum.data ));
870 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&sum.data ));
871 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.rows ));
872 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.cols ));
873 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.step ));
874 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sum.step));
875 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sum_offset));
876 size_t gt2[3] = {t_sum.cols * 32, 1, 1}, lt2[3] = {256, 1, 1};
877 openCLExecuteKernel(src.clCxt, &imgproc_integral_sum, "integral_sum_rows", gt2, lt2, args, -1, depth);
880 /////////////////////// corner //////////////////////////////
882 static void extractCovData(const oclMat &src, oclMat &Dx, oclMat &Dy,
883 int blockSize, int ksize, int borderType)
885 CV_Assert(src.type() == CV_8UC1 || src.type() == CV_32FC1);
886 double scale = static_cast<double>(1 << ((ksize > 0 ? ksize : 3) - 1)) * blockSize;
890 if (src.depth() == CV_8U)
900 Context* clCxt = Context::getContext();
901 if(clCxt->supportsFeature(FEATURE_CL_INTEL_DEVICE) && src.type() == CV_8UC1 &&
902 src.cols % 8 == 0 && src.rows % 8 == 0 &&
904 (borderType ==cv::BORDER_REFLECT ||
905 borderType == cv::BORDER_REPLICATE ||
906 borderType ==cv::BORDER_REFLECT101 ||
907 borderType ==cv::BORDER_WRAP))
909 Dx.create(src.size(), CV_32FC1);
910 Dy.create(src.size(), CV_32FC1);
912 const unsigned int block_x = 8;
913 const unsigned int block_y = 8;
915 unsigned int src_pitch = src.step;
916 unsigned int dst_pitch = Dx.cols;
918 float _scale = scale;
920 std::vector<std::pair<size_t , const void *> > args;
921 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&src.data ));
922 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&Dx.data ));
923 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&Dy.data ));
924 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.cols ));
925 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.rows ));
926 args.push_back( std::make_pair( sizeof(cl_uint) , (void *)&src_pitch ));
927 args.push_back( std::make_pair( sizeof(cl_uint) , (void *)&dst_pitch ));
928 args.push_back( std::make_pair( sizeof(cl_float) , (void *)&_scale ));
929 size_t gt2[3] = {src.cols, src.rows, 1}, lt2[3] = {block_x, block_y, 1};
931 String option = "-D BLK_X=8 -D BLK_Y=8";
934 case cv::BORDER_REPLICATE:
935 option = option + " -D BORDER_REPLICATE";
937 case cv::BORDER_REFLECT:
938 option = option + " -D BORDER_REFLECT";
940 case cv::BORDER_REFLECT101:
941 option = option + " -D BORDER_REFLECT101";
943 case cv::BORDER_WRAP:
944 option = option + " -D BORDER_WRAP";
947 openCLExecuteKernel(src.clCxt, &imgproc_sobel3, "sobel3", gt2, lt2, args, -1, -1, option.c_str() );
951 Sobel(src, Dx, CV_32F, 1, 0, ksize, scale, 0, borderType);
952 Sobel(src, Dy, CV_32F, 0, 1, ksize, scale, 0, borderType);
957 Scharr(src, Dx, CV_32F, 1, 0, scale, 0, borderType);
958 Scharr(src, Dy, CV_32F, 0, 1, scale, 0, borderType);
960 CV_Assert(Dx.offset == 0 && Dy.offset == 0);
963 static void corner_ocl(const cv::ocl::ProgramEntry* source, String kernelName, int block_size, float k, oclMat &Dx, oclMat &Dy,
964 oclMat &dst, int border_type)
969 case cv::BORDER_CONSTANT:
970 sprintf(borderType, "BORDER_CONSTANT");
972 case cv::BORDER_REFLECT101:
973 sprintf(borderType, "BORDER_REFLECT101");
975 case cv::BORDER_REFLECT:
976 sprintf(borderType, "BORDER_REFLECT");
978 case cv::BORDER_REPLICATE:
979 sprintf(borderType, "BORDER_REPLICATE");
982 CV_Error(Error::StsBadFlag, "BORDER type is not supported!");
985 std::string buildOptions = format("-D anX=%d -D anY=%d -D ksX=%d -D ksY=%d -D %s",
986 block_size / 2, block_size / 2, block_size, block_size, borderType);
988 size_t blockSizeX = 256, blockSizeY = 1;
989 size_t gSize = blockSizeX - block_size / 2 * 2;
990 size_t globalSizeX = (Dx.cols) % gSize == 0 ? Dx.cols / gSize * blockSizeX : (Dx.cols / gSize + 1) * blockSizeX;
991 size_t rows_per_thread = 2;
992 size_t globalSizeY = ((Dx.rows + rows_per_thread - 1) / rows_per_thread) % blockSizeY == 0 ?
993 ((Dx.rows + rows_per_thread - 1) / rows_per_thread) :
994 (((Dx.rows + rows_per_thread - 1) / rows_per_thread) / blockSizeY + 1) * blockSizeY;
996 size_t gt[3] = { globalSizeX, globalSizeY, 1 };
997 size_t lt[3] = { blockSizeX, blockSizeY, 1 };
998 std::vector<std::pair<size_t , const void *> > args;
999 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&Dx.data ));
1000 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&Dy.data));
1001 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&dst.data));
1002 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&Dx.offset ));
1003 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&Dx.wholerows ));
1004 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&Dx.wholecols ));
1005 args.push_back( std::make_pair(sizeof(cl_int), (void *)&Dx.step));
1006 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&Dy.offset ));
1007 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&Dy.wholerows ));
1008 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&Dy.wholecols ));
1009 args.push_back( std::make_pair(sizeof(cl_int), (void *)&Dy.step));
1010 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.offset));
1011 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.rows));
1012 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.cols));
1013 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.step));
1014 args.push_back( std::make_pair( sizeof(cl_float) , (void *)&k));
1016 openCLExecuteKernel(dst.clCxt, source, kernelName, gt, lt, args, -1, -1, buildOptions.c_str());
1019 void cornerHarris(const oclMat &src, oclMat &dst, int blockSize, int ksize,
1020 double k, int borderType)
1023 cornerHarris_dxdy(src, dst, dx, dy, blockSize, ksize, k, borderType);
1026 void cornerHarris_dxdy(const oclMat &src, oclMat &dst, oclMat &dx, oclMat &dy, int blockSize, int ksize,
1027 double k, int borderType)
1029 if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
1031 CV_Error(Error::OpenCLDoubleNotSupported, "Selected device doesn't support double");
1035 CV_Assert(borderType == cv::BORDER_CONSTANT || borderType == cv::BORDER_REFLECT101 || borderType == cv::BORDER_REPLICATE
1036 || borderType == cv::BORDER_REFLECT);
1038 extractCovData(src, dx, dy, blockSize, ksize, borderType);
1039 dst.create(src.size(), CV_32FC1);
1040 corner_ocl(&imgproc_calcHarris, "calcHarris", blockSize, static_cast<float>(k), dx, dy, dst, borderType);
1043 void cornerMinEigenVal(const oclMat &src, oclMat &dst, int blockSize, int ksize, int borderType)
1046 cornerMinEigenVal_dxdy(src, dst, dx, dy, blockSize, ksize, borderType);
1049 void cornerMinEigenVal_dxdy(const oclMat &src, oclMat &dst, oclMat &dx, oclMat &dy, int blockSize, int ksize, int borderType)
1051 if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
1053 CV_Error(Error::OpenCLDoubleNotSupported, "Selected device doesn't support double");
1057 CV_Assert(borderType == cv::BORDER_CONSTANT || borderType == cv::BORDER_REFLECT101 ||
1058 borderType == cv::BORDER_REPLICATE || borderType == cv::BORDER_REFLECT);
1060 extractCovData(src, dx, dy, blockSize, ksize, borderType);
1061 dst.create(src.size(), CV_32F);
1063 corner_ocl(&imgproc_calcMinEigenVal, "calcMinEigenVal", blockSize, 0, dx, dy, dst, borderType);
1066 /////////////////////////////////// MeanShiftfiltering ///////////////////////////////////////////////
1068 static void meanShiftFiltering_gpu(const oclMat &src, oclMat dst, int sp, int sr, int maxIter, float eps)
1070 CV_Assert( (src.cols == dst.cols) && (src.rows == dst.rows) );
1071 CV_Assert( !(dst.step & 0x3) );
1073 //Arrange the NDRange
1074 int col = src.cols, row = src.rows;
1075 int ltx = 16, lty = 8;
1076 if (src.cols % ltx != 0)
1077 col = (col / ltx + 1) * ltx;
1078 if (src.rows % lty != 0)
1079 row = (row / lty + 1) * lty;
1081 size_t globalThreads[3] = {col, row, 1};
1082 size_t localThreads[3] = {ltx, lty, 1};
1085 std::vector<std::pair<size_t , const void *> > args;
1086 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&dst.data ));
1087 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst.step ));
1088 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&src.data ));
1089 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.step ));
1090 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst.offset ));
1091 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.offset ));
1092 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst.cols ));
1093 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst.rows ));
1094 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sp ));
1095 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sr ));
1096 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&maxIter ));
1097 args.push_back( std::make_pair( sizeof(cl_float) , (void *)&eps ));
1099 openCLExecuteKernel(src.clCxt, &meanShift, "meanshift_kernel", globalThreads, localThreads, args, -1, -1);
1102 void meanShiftFiltering(const oclMat &src, oclMat &dst, int sp, int sr, TermCriteria criteria)
1105 CV_Error(Error::StsBadArg, "The input image is empty");
1107 if ( src.depth() != CV_8U || src.oclchannels() != 4 )
1108 CV_Error(Error::StsUnsupportedFormat, "Only 8-bit, 4-channel images are supported");
1110 dst.create( src.size(), CV_8UC4 );
1112 if ( !(criteria.type & TermCriteria::MAX_ITER) )
1113 criteria.maxCount = 5;
1115 int maxIter = std::min(std::max(criteria.maxCount, 1), 100);
1118 if ( !(criteria.type & TermCriteria::EPS) )
1120 eps = (float)std::max(criteria.epsilon, 0.0);
1122 meanShiftFiltering_gpu(src, dst, sp, sr, maxIter, eps);
1125 static void meanShiftProc_gpu(const oclMat &src, oclMat dstr, oclMat dstsp, int sp, int sr, int maxIter, float eps)
1128 CV_Assert( (src.cols == dstr.cols) && (src.rows == dstr.rows) &&
1129 (src.rows == dstsp.rows) && (src.cols == dstsp.cols));
1130 CV_Assert( !(dstsp.step & 0x3) );
1132 //Arrange the NDRange
1133 int col = src.cols, row = src.rows;
1134 int ltx = 16, lty = 8;
1135 if (src.cols % ltx != 0)
1136 col = (col / ltx + 1) * ltx;
1137 if (src.rows % lty != 0)
1138 row = (row / lty + 1) * lty;
1140 size_t globalThreads[3] = {col, row, 1};
1141 size_t localThreads[3] = {ltx, lty, 1};
1144 std::vector<std::pair<size_t , const void *> > args;
1145 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&src.data ));
1146 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&dstr.data ));
1147 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&dstsp.data ));
1148 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.step ));
1149 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dstr.step ));
1150 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dstsp.step ));
1151 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.offset ));
1152 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dstr.offset ));
1153 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dstsp.offset ));
1154 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dstr.cols ));
1155 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dstr.rows ));
1156 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sp ));
1157 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sr ));
1158 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&maxIter ));
1159 args.push_back( std::make_pair( sizeof(cl_float) , (void *)&eps ));
1161 openCLExecuteKernel(src.clCxt, &meanShift, "meanshiftproc_kernel", globalThreads, localThreads, args, -1, -1);
1164 void meanShiftProc(const oclMat &src, oclMat &dstr, oclMat &dstsp, int sp, int sr, TermCriteria criteria)
1167 CV_Error(Error::StsBadArg, "The input image is empty");
1169 if ( src.depth() != CV_8U || src.oclchannels() != 4 )
1170 CV_Error(Error::StsUnsupportedFormat, "Only 8-bit, 4-channel images are supported");
1172 // if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE))
1174 // CV_Error(Error::OpenCLDoubleNotSupportedNotSupported, "Selected device doesn't support double, so a deviation exists.\nIf the accuracy is acceptable, the error can be ignored.\n");
1178 dstr.create( src.size(), CV_8UC4 );
1179 dstsp.create( src.size(), CV_16SC2 );
1181 if ( !(criteria.type & TermCriteria::MAX_ITER) )
1182 criteria.maxCount = 5;
1184 int maxIter = std::min(std::max(criteria.maxCount, 1), 100);
1187 if ( !(criteria.type & TermCriteria::EPS) )
1189 eps = (float)std::max(criteria.epsilon, 0.0);
1191 meanShiftProc_gpu(src, dstr, dstsp, sp, sr, maxIter, eps);
1194 ///////////////////////////////////////////////////////////////////////////////////////////////////
1195 ////////////////////////////////////////////////////hist///////////////////////////////////////////////
1196 /////////////////////////////////////////////////////////////////////////////////////////////////////
1198 namespace histograms
1200 const int PARTIAL_HISTOGRAM256_COUNT = 256;
1201 const int HISTOGRAM256_BIN_COUNT = 256;
1203 ///////////////////////////////calcHist/////////////////////////////////////////////////////////////////
1204 static void calc_sub_hist(const oclMat &mat_src, const oclMat &mat_sub_hist)
1206 using namespace histograms;
1208 int depth = mat_src.depth();
1210 size_t localThreads[3] = { HISTOGRAM256_BIN_COUNT, 1, 1 };
1211 size_t globalThreads[3] = { PARTIAL_HISTOGRAM256_COUNT *localThreads[0], 1, 1};
1214 int dataWidth_bits = 4;
1215 int mask = dataWidth - 1;
1217 int cols = mat_src.cols * mat_src.oclchannels();
1218 int src_offset = mat_src.offset;
1219 int hist_step = mat_sub_hist.step >> 2;
1220 int left_col = 0, right_col = 0;
1222 if (cols >= dataWidth * 2 - 1)
1224 left_col = dataWidth - (src_offset & mask);
1226 src_offset += left_col;
1228 right_col = cols & mask;
1236 globalThreads[0] = 0;
1239 std::vector<std::pair<size_t , const void *> > args;
1240 if (globalThreads[0] != 0)
1242 int tempcols = cols >> dataWidth_bits;
1243 int inc_x = globalThreads[0] % tempcols;
1244 int inc_y = globalThreads[0] / tempcols;
1245 src_offset >>= dataWidth_bits;
1246 int src_step = mat_src.step >> dataWidth_bits;
1247 int datacount = tempcols * mat_src.rows;
1249 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_src.data));
1250 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src_step));
1251 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src_offset));
1252 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_sub_hist.data));
1253 args.push_back( std::make_pair( sizeof(cl_int), (void *)&datacount));
1254 args.push_back( std::make_pair( sizeof(cl_int), (void *)&tempcols));
1255 args.push_back( std::make_pair( sizeof(cl_int), (void *)&inc_x));
1256 args.push_back( std::make_pair( sizeof(cl_int), (void *)&inc_y));
1257 args.push_back( std::make_pair( sizeof(cl_int), (void *)&hist_step));
1259 openCLExecuteKernel(mat_src.clCxt, &imgproc_histogram, "calc_sub_hist", globalThreads, localThreads, args, -1, depth);
1262 if (left_col != 0 || right_col != 0)
1264 src_offset = mat_src.offset;
1265 localThreads[0] = 1;
1266 localThreads[1] = 256;
1267 globalThreads[0] = left_col + right_col;
1268 globalThreads[1] = mat_src.rows;
1271 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_src.data));
1272 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src.step));
1273 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src_offset));
1274 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_sub_hist.data));
1275 args.push_back( std::make_pair( sizeof(cl_int), (void *)&left_col));
1276 args.push_back( std::make_pair( sizeof(cl_int), (void *)&cols));
1277 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src.rows));
1278 args.push_back( std::make_pair( sizeof(cl_int), (void *)&hist_step));
1280 openCLExecuteKernel(mat_src.clCxt, &imgproc_histogram, "calc_sub_hist_border", globalThreads, localThreads, args, -1, depth);
1284 static void merge_sub_hist(const oclMat &sub_hist, oclMat &mat_hist)
1286 using namespace histograms;
1288 size_t localThreads[3] = { 256, 1, 1 };
1289 size_t globalThreads[3] = { HISTOGRAM256_BIN_COUNT *localThreads[0], 1, 1};
1290 int src_step = sub_hist.step >> 2;
1292 std::vector<std::pair<size_t , const void *> > args;
1293 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&sub_hist.data));
1294 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_hist.data));
1295 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src_step));
1297 openCLExecuteKernel(sub_hist.clCxt, &imgproc_histogram, "merge_hist", globalThreads, localThreads, args, -1, -1);
1300 void calcHist(const oclMat &mat_src, oclMat &mat_hist)
1302 using namespace histograms;
1303 CV_Assert(mat_src.type() == CV_8UC1);
1304 mat_hist.create(1, 256, CV_32SC1);
1306 oclMat buf(PARTIAL_HISTOGRAM256_COUNT, HISTOGRAM256_BIN_COUNT, CV_32SC1);
1309 calc_sub_hist(mat_src, buf);
1310 merge_sub_hist(buf, mat_hist);
1313 ///////////////////////////////////equalizeHist/////////////////////////////////////////////////////
1314 void equalizeHist(const oclMat &mat_src, oclMat &mat_dst)
1316 mat_dst.create(mat_src.rows, mat_src.cols, CV_8UC1);
1318 oclMat mat_hist(1, 256, CV_32SC1);
1320 calcHist(mat_src, mat_hist);
1322 size_t localThreads[3] = { 256, 1, 1};
1323 size_t globalThreads[3] = { 256, 1, 1};
1324 oclMat lut(1, 256, CV_8UC1);
1325 int total = mat_src.rows * mat_src.cols;
1327 std::vector<std::pair<size_t , const void *> > args;
1328 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&lut.data));
1329 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_hist.data));
1330 args.push_back( std::make_pair( sizeof(int), (void *)&total));
1332 openCLExecuteKernel(mat_src.clCxt, &imgproc_histogram, "calLUT", globalThreads, localThreads, args, -1, -1);
1333 LUT(mat_src, lut, mat_dst);
1336 ////////////////////////////////////////////////////////////////////////
1340 static void calcLut(const oclMat &src, oclMat &dst,
1341 const int tilesX, const int tilesY, const cv::Size tileSize,
1342 const int clipLimit, const float lutScale)
1345 tile_size.s[0] = tileSize.width;
1346 tile_size.s[1] = tileSize.height;
1348 std::vector<std::pair<size_t , const void *> > args;
1349 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&src.data ));
1350 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst.data ));
1351 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.step ));
1352 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.step ));
1353 args.push_back( std::make_pair( sizeof(cl_int2), (void *)&tile_size ));
1354 args.push_back( std::make_pair( sizeof(cl_int), (void *)&tilesX ));
1355 args.push_back( std::make_pair( sizeof(cl_int), (void *)&clipLimit ));
1356 args.push_back( std::make_pair( sizeof(cl_float), (void *)&lutScale ));
1357 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.offset ));
1358 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.offset ));
1360 String kernelName = "calcLut";
1361 size_t localThreads[3] = { 32, 8, 1 };
1362 size_t globalThreads[3] = { tilesX * localThreads[0], tilesY * localThreads[1], 1 };
1363 bool is_cpu = isCpuDevice();
1365 openCLExecuteKernel(Context::getContext(), &imgproc_clahe, kernelName, globalThreads, localThreads, args, -1, -1, (char*)"-D CPU");
1368 cl_kernel kernel = openCLGetKernelFromSource(Context::getContext(), &imgproc_clahe, kernelName);
1369 int wave_size = (int)queryWaveFrontSize(kernel);
1370 openCLSafeCall(clReleaseKernel(kernel));
1372 std::string opt = format("-D WAVE_SIZE=%d", wave_size);
1373 openCLExecuteKernel(Context::getContext(), &imgproc_clahe, kernelName, globalThreads, localThreads, args, -1, -1, opt.c_str());
1377 static void transform(const oclMat &src, oclMat &dst, const oclMat &lut,
1378 const int tilesX, const int tilesY, const Size & tileSize)
1381 tile_size.s[0] = tileSize.width;
1382 tile_size.s[1] = tileSize.height;
1384 std::vector<std::pair<size_t , const void *> > args;
1385 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&src.data ));
1386 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst.data ));
1387 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&lut.data ));
1388 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.step ));
1389 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.step ));
1390 args.push_back( std::make_pair( sizeof(cl_int), (void *)&lut.step ));
1391 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.cols ));
1392 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.rows ));
1393 args.push_back( std::make_pair( sizeof(cl_int2), (void *)&tile_size ));
1394 args.push_back( std::make_pair( sizeof(cl_int), (void *)&tilesX ));
1395 args.push_back( std::make_pair( sizeof(cl_int), (void *)&tilesY ));
1396 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.offset ));
1397 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.offset ));
1398 args.push_back( std::make_pair( sizeof(cl_int), (void *)&lut.offset ));
1400 size_t localThreads[3] = { 32, 8, 1 };
1401 size_t globalThreads[3] = { src.cols, src.rows, 1 };
1403 openCLExecuteKernel(Context::getContext(), &imgproc_clahe, "transform", globalThreads, localThreads, args, -1, -1);
1409 class CLAHE_Impl : public cv::CLAHE
1412 CLAHE_Impl(double clipLimit = 40.0, int tilesX = 8, int tilesY = 8);
1414 cv::AlgorithmInfo* info() const;
1416 void apply(cv::InputArray src, cv::OutputArray dst);
1418 void setClipLimit(double clipLimit);
1419 double getClipLimit() const;
1421 void setTilesGridSize(cv::Size tileGridSize);
1422 cv::Size getTilesGridSize() const;
1424 void collectGarbage();
1435 CLAHE_Impl::CLAHE_Impl(double clipLimit, int tilesX, int tilesY) :
1436 clipLimit_(clipLimit), tilesX_(tilesX), tilesY_(tilesY)
1440 CV_INIT_ALGORITHM(CLAHE_Impl, "CLAHE_OCL",
1441 obj.info()->addParam(obj, "clipLimit", obj.clipLimit_);
1442 obj.info()->addParam(obj, "tilesX", obj.tilesX_);
1443 obj.info()->addParam(obj, "tilesY", obj.tilesY_))
1445 void CLAHE_Impl::apply(cv::InputArray src_raw, cv::OutputArray dst_raw)
1447 oclMat& src = getOclMatRef(src_raw);
1448 oclMat& dst = getOclMatRef(dst_raw);
1449 CV_Assert( src.type() == CV_8UC1 );
1451 dst.create( src.size(), src.type() );
1453 const int histSize = 256;
1455 ensureSizeIsEnough(tilesX_ * tilesY_, histSize, CV_8UC1, lut_);
1460 if (src.cols % tilesX_ == 0 && src.rows % tilesY_ == 0)
1462 tileSize = cv::Size(src.cols / tilesX_, src.rows / tilesY_);
1467 ocl::copyMakeBorder(src, srcExt_, 0, tilesY_ - (src.rows % tilesY_), 0,
1468 tilesX_ - (src.cols % tilesX_), BORDER_REFLECT_101, Scalar::all(0));
1470 tileSize = Size(srcExt_.cols / tilesX_, srcExt_.rows / tilesY_);
1471 srcForLut = srcExt_;
1474 const int tileSizeTotal = tileSize.area();
1475 const float lutScale = static_cast<float>(histSize - 1) / tileSizeTotal;
1478 if (clipLimit_ > 0.0)
1480 clipLimit = static_cast<int>(clipLimit_ * tileSizeTotal / histSize);
1481 clipLimit = std::max(clipLimit, 1);
1484 clahe::calcLut(srcForLut, lut_, tilesX_, tilesY_, tileSize, clipLimit, lutScale);
1485 clahe::transform(src, dst, lut_, tilesX_, tilesY_, tileSize);
1488 void CLAHE_Impl::setClipLimit(double clipLimit)
1490 clipLimit_ = clipLimit;
1493 double CLAHE_Impl::getClipLimit() const
1498 void CLAHE_Impl::setTilesGridSize(cv::Size tileGridSize)
1500 tilesX_ = tileGridSize.width;
1501 tilesY_ = tileGridSize.height;
1504 cv::Size CLAHE_Impl::getTilesGridSize() const
1506 return cv::Size(tilesX_, tilesY_);
1509 void CLAHE_Impl::collectGarbage()
1516 cv::Ptr<cv::CLAHE> createCLAHE(double clipLimit, cv::Size tileGridSize)
1518 return makePtr<CLAHE_Impl>(clipLimit, tileGridSize.width, tileGridSize.height);
1521 //////////////////////////////////bilateralFilter////////////////////////////////////////////////////
1523 static void oclbilateralFilter_8u( const oclMat &src, oclMat &dst, int d,
1524 double sigma_color, double sigma_space,
1527 int cn = src.channels();
1528 int i, j, maxk, radius;
1530 CV_Assert( (src.channels() == 1 || src.channels() == 3) &&
1531 src.type() == dst.type() && src.size() == dst.size() &&
1532 src.data != dst.data );
1534 if ( sigma_color <= 0 )
1536 if ( sigma_space <= 0 )
1539 double gauss_color_coeff = -0.5 / (sigma_color * sigma_color);
1540 double gauss_space_coeff = -0.5 / (sigma_space * sigma_space);
1543 radius = cvRound(sigma_space * 1.5);
1546 radius = MAX(radius, 1);
1550 copyMakeBorder( src, temp, radius, radius, radius, radius, borderType );
1552 std::vector<float> _color_weight(cn * 256);
1553 std::vector<float> _space_weight(d * d);
1554 std::vector<int> _space_ofs(d * d);
1555 float *color_weight = &_color_weight[0];
1556 float *space_weight = &_space_weight[0];
1557 int *space_ofs = &_space_ofs[0];
1559 int dst_step_in_pixel = dst.step / dst.elemSize();
1560 int dst_offset_in_pixel = dst.offset / dst.elemSize();
1561 int temp_step_in_pixel = temp.step / temp.elemSize();
1563 // initialize color-related bilateral filter coefficients
1564 for( i = 0; i < 256 * cn; i++ )
1565 color_weight[i] = (float)std::exp(i * i * gauss_color_coeff);
1567 // initialize space-related bilateral filter coefficients
1568 for( i = -radius, maxk = 0; i <= radius; i++ )
1569 for( j = -radius; j <= radius; j++ )
1571 double r = std::sqrt((double)i * i + (double)j * j);
1574 space_weight[maxk] = (float)std::exp(r * r * gauss_space_coeff);
1575 space_ofs[maxk++] = (int)(i * temp_step_in_pixel + j);
1578 oclMat oclcolor_weight(1, cn * 256, CV_32FC1, color_weight);
1579 oclMat oclspace_weight(1, d * d, CV_32FC1, space_weight);
1580 oclMat oclspace_ofs(1, d * d, CV_32SC1, space_ofs);
1582 String kernelName = "bilateral";
1583 size_t localThreads[3] = { 16, 16, 1 };
1584 size_t globalThreads[3] = { dst.cols, dst.rows, 1 };
1586 if ((dst.type() == CV_8UC1) && ((dst.offset & 3) == 0) && ((dst.cols & 3) == 0))
1588 kernelName = "bilateral2";
1589 globalThreads[0] = dst.cols >> 2;
1592 std::vector<std::pair<size_t , const void *> > args;
1593 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst.data ));
1594 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&temp.data ));
1595 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.rows ));
1596 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.cols ));
1597 args.push_back( std::make_pair( sizeof(cl_int), (void *)&maxk ));
1598 args.push_back( std::make_pair( sizeof(cl_int), (void *)&radius ));
1599 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst_step_in_pixel ));
1600 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst_offset_in_pixel ));
1601 args.push_back( std::make_pair( sizeof(cl_int), (void *)&temp_step_in_pixel ));
1602 args.push_back( std::make_pair( sizeof(cl_int), (void *)&temp.rows ));
1603 args.push_back( std::make_pair( sizeof(cl_int), (void *)&temp.cols ));
1604 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&oclcolor_weight.data ));
1605 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&oclspace_weight.data ));
1606 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&oclspace_ofs.data ));
1608 openCLExecuteKernel(src.clCxt, &imgproc_bilateral, kernelName, globalThreads, localThreads, args, dst.oclchannels(), dst.depth());
1611 void bilateralFilter(const oclMat &src, oclMat &dst, int radius, double sigmaclr, double sigmaspc, int borderType)
1613 dst.create( src.size(), src.type() );
1614 if ( src.depth() == CV_8U )
1615 oclbilateralFilter_8u( src, dst, radius, sigmaclr, sigmaspc, borderType );
1617 CV_Error(Error::StsUnsupportedFormat, "Bilateral filtering is only implemented for CV_8U images");
1622 //////////////////////////////////mulSpectrums////////////////////////////////////////////////////
1623 void cv::ocl::mulSpectrums(const oclMat &a, const oclMat &b, oclMat &c, int /*flags*/, float scale, bool conjB)
1625 CV_Assert(a.type() == CV_32FC2);
1626 CV_Assert(b.type() == CV_32FC2);
1628 c.create(a.size(), CV_32FC2);
1630 size_t lt[3] = { 16, 16, 1 };
1631 size_t gt[3] = { a.cols, a.rows, 1 };
1633 String kernelName = conjB ? "mulAndScaleSpectrumsKernel_CONJ":"mulAndScaleSpectrumsKernel";
1635 std::vector<std::pair<size_t , const void *> > args;
1636 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&a.data ));
1637 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&b.data ));
1638 args.push_back( std::make_pair( sizeof(cl_float), (void *)&scale));
1639 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&c.data ));
1640 args.push_back( std::make_pair( sizeof(cl_int), (void *)&a.cols ));
1641 args.push_back( std::make_pair( sizeof(cl_int), (void *)&a.rows));
1642 args.push_back( std::make_pair( sizeof(cl_int), (void *)&a.step ));
1644 Context *clCxt = Context::getContext();
1645 openCLExecuteKernel(clCxt, &imgproc_mulAndScaleSpectrums, kernelName, gt, lt, args, -1, -1);
1647 //////////////////////////////////convolve////////////////////////////////////////////////////
1648 // ported from CUDA module
1649 void cv::ocl::ConvolveBuf::create(Size image_size, Size templ_size)
1651 result_size = Size(image_size.width - templ_size.width + 1,
1652 image_size.height - templ_size.height + 1);
1654 block_size = user_block_size;
1655 if (user_block_size.width == 0 || user_block_size.height == 0)
1656 block_size = estimateBlockSize(result_size, templ_size);
1658 dft_size.width = 1 << int(ceil(std::log(block_size.width + templ_size.width - 1.) / std::log(2.)));
1659 dft_size.height = 1 << int(ceil(std::log(block_size.height + templ_size.height - 1.) / std::log(2.)));
1661 // CUFFT has hard-coded kernels for power-of-2 sizes (up to 8192),
1662 // see CUDA Toolkit 4.1 CUFFT Library Programming Guide
1663 //if (dft_size.width > 8192)
1664 dft_size.width = getOptimalDFTSize(block_size.width + templ_size.width - 1.);
1665 //if (dft_size.height > 8192)
1666 dft_size.height = getOptimalDFTSize(block_size.height + templ_size.height - 1.);
1668 // To avoid wasting time doing small DFTs
1669 dft_size.width = std::max(dft_size.width, 512);
1670 dft_size.height = std::max(dft_size.height, 512);
1672 image_block.create(dft_size, CV_32F);
1673 templ_block.create(dft_size, CV_32F);
1674 result_data.create(dft_size, CV_32F);
1676 //spect_len = dft_size.height * (dft_size.width / 2 + 1);
1677 image_spect.create(dft_size.height, dft_size.width / 2 + 1, CV_32FC2);
1678 templ_spect.create(dft_size.height, dft_size.width / 2 + 1, CV_32FC2);
1679 result_spect.create(dft_size.height, dft_size.width / 2 + 1, CV_32FC2);
1681 // Use maximum result matrix block size for the estimated DFT block size
1682 block_size.width = std::min(dft_size.width - templ_size.width + 1, result_size.width);
1683 block_size.height = std::min(dft_size.height - templ_size.height + 1, result_size.height);
1686 Size cv::ocl::ConvolveBuf::estimateBlockSize(Size result_size, Size /*templ_size*/)
1688 int width = (result_size.width + 2) / 3;
1689 int height = (result_size.height + 2) / 3;
1690 width = std::min(width, result_size.width);
1691 height = std::min(height, result_size.height);
1692 return Size(width, height);
1695 static void convolve_run_fft(const oclMat &image, const oclMat &templ, oclMat &result, bool ccorr, ConvolveBuf& buf)
1697 #if defined HAVE_CLAMDFFT
1698 CV_Assert(image.type() == CV_32F);
1699 CV_Assert(templ.type() == CV_32F);
1701 buf.create(image.size(), templ.size());
1702 result.create(buf.result_size, CV_32F);
1704 Size& block_size = buf.block_size;
1705 Size& dft_size = buf.dft_size;
1707 oclMat& image_block = buf.image_block;
1708 oclMat& templ_block = buf.templ_block;
1709 oclMat& result_data = buf.result_data;
1711 oclMat& image_spect = buf.image_spect;
1712 oclMat& templ_spect = buf.templ_spect;
1713 oclMat& result_spect = buf.result_spect;
1715 oclMat templ_roi = templ;
1716 copyMakeBorder(templ_roi, templ_block, 0, templ_block.rows - templ_roi.rows, 0,
1717 templ_block.cols - templ_roi.cols, 0, Scalar());
1719 cv::ocl::dft(templ_block, templ_spect, dft_size);
1721 // Process all blocks of the result matrix
1722 for (int y = 0; y < result.rows; y += block_size.height)
1724 for (int x = 0; x < result.cols; x += block_size.width)
1726 Size image_roi_size(std::min(x + dft_size.width, image.cols) - x,
1727 std::min(y + dft_size.height, image.rows) - y);
1728 Rect roi0(x, y, image_roi_size.width, image_roi_size.height);
1730 oclMat image_roi(image, roi0);
1732 copyMakeBorder(image_roi, image_block, 0, image_block.rows - image_roi.rows,
1733 0, image_block.cols - image_roi.cols, 0, Scalar());
1735 cv::ocl::dft(image_block, image_spect, dft_size);
1737 mulSpectrums(image_spect, templ_spect, result_spect, 0,
1738 1.f / dft_size.area(), ccorr);
1740 cv::ocl::dft(result_spect, result_data, dft_size, cv::DFT_INVERSE | cv::DFT_REAL_OUTPUT);
1742 Size result_roi_size(std::min(x + block_size.width, result.cols) - x,
1743 std::min(y + block_size.height, result.rows) - y);
1745 Rect roi1(x, y, result_roi_size.width, result_roi_size.height);
1746 Rect roi2(0, 0, result_roi_size.width, result_roi_size.height);
1748 oclMat result_roi(result, roi1);
1749 oclMat result_block(result_data, roi2);
1751 result_block.copyTo(result_roi);
1756 CV_Error(Error::OpenCLNoAMDBlasFft, "OpenCL DFT is not implemented");
1757 #define UNUSED(x) (void)(x);
1758 UNUSED(image) UNUSED(templ) UNUSED(result) UNUSED(ccorr) UNUSED(buf)
1763 static void convolve_run(const oclMat &src, const oclMat &temp1, oclMat &dst, String kernelName, const cv::ocl::ProgramEntry* source)
1765 CV_Assert(src.depth() == CV_32FC1);
1766 CV_Assert(temp1.depth() == CV_32F);
1767 CV_Assert(temp1.cols <= 17 && temp1.rows <= 17);
1769 dst.create(src.size(), src.type());
1771 CV_Assert(src.cols == dst.cols && src.rows == dst.rows);
1772 CV_Assert(src.type() == dst.type());
1774 size_t localThreads[3] = { 16, 16, 1 };
1775 size_t globalThreads[3] = { dst.cols, dst.rows, 1 };
1777 int src_step = src.step / src.elemSize(), src_offset = src.offset / src.elemSize();
1778 int dst_step = dst.step / dst.elemSize(), dst_offset = dst.offset / dst.elemSize();
1779 int temp1_step = temp1.step / temp1.elemSize(), temp1_offset = temp1.offset / temp1.elemSize();
1781 std::vector<std::pair<size_t , const void *> > args;
1782 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&src.data ));
1783 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&temp1.data ));
1784 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst.data ));
1785 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.rows ));
1786 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.cols ));
1787 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src_step ));
1788 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst_step ));
1789 args.push_back( std::make_pair( sizeof(cl_int), (void *)&temp1_step ));
1790 args.push_back( std::make_pair( sizeof(cl_int), (void *)&temp1.rows ));
1791 args.push_back( std::make_pair( sizeof(cl_int), (void *)&temp1.cols ));
1792 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src_offset ));
1793 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst_offset ));
1794 args.push_back( std::make_pair( sizeof(cl_int), (void *)&temp1_offset ));
1796 openCLExecuteKernel(src.clCxt, source, kernelName, globalThreads, localThreads, args, -1, dst.depth());
1799 void cv::ocl::convolve(const oclMat &x, const oclMat &t, oclMat &y, bool ccorr)
1801 CV_Assert(x.depth() == CV_32F);
1802 CV_Assert(t.depth() == CV_32F);
1803 y.create(x.size(), x.type());
1804 String kernelName = "convolve";
1805 if(t.cols > 17 || t.rows > 17)
1808 convolve_run_fft(x, t, y, ccorr, buf);
1812 CV_Assert(ccorr == false);
1813 convolve_run(x, t, y, kernelName, &imgproc_convolve);
1816 void cv::ocl::convolve(const oclMat &image, const oclMat &templ, oclMat &result, bool ccorr, ConvolveBuf& buf)
1818 result.create(image.size(), image.type());
1819 convolve_run_fft(image, templ, result, ccorr, buf);