1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
16 // Third party copyrights are property of their respective owners.
19 // Niko Li, newlife20080214@gmail.com
20 // Jia Haipeng, jiahaipeng95@gmail.com
21 // Shengen Yan, yanshengen@gmail.com
22 // Rock Li, Rock.Li@amd.com
23 // Zero Lin, Zero.Lin@amd.com
24 // Zhang Ying, zhangying913@gmail.com
25 // Xu Pang, pangxu010@163.com
26 // Wu Zailong, bullet@yeah.net
27 // Wenju He, wenju@multicorewareinc.com
28 // Peng Xiao, pengxiao@outlook.com
29 // Sen Liu, swjtuls1987@126.com
31 // Redistribution and use in source and binary forms, with or without modification,
32 // are permitted provided that the following conditions are met:
34 // * Redistribution's of source code must retain the above copyright notice,
35 // this list of conditions and the following disclaimer.
37 // * Redistribution's in binary form must reproduce the above copyright notice,
38 // this list of conditions and the following disclaimer in the documentation
39 // and/or other materials provided with the distribution.
41 // * The name of the copyright holders may not be used to endorse or promote products
42 // derived from this software without specific prior written permission.
44 // This software is provided by the copyright holders and contributors "as is" and
45 // any express or implied warranties, including, but not limited to, the implied
46 // warranties of merchantability and fitness for a particular purpose are disclaimed.
47 // In no event shall the Intel Corporation or contributors be liable for any direct,
48 // indirect, incidental, special, exemplary, or consequential damages
49 // (including, but not limited to, procurement of substitute goods or services;
50 // loss of use, data, or profits; or business interruption) however caused
51 // and on any theory of liability, whether in contract, strict liability,
52 // or tort (including negligence or otherwise) arising in any way out of
53 // the use of this software, even if advised of the possibility of such damage.
57 #include "precomp.hpp"
58 #include "opencl_kernels.hpp"
61 using namespace cv::ocl;
67 ////////////////////////////////////OpenCL call wrappers////////////////////////////
69 template <typename T> struct index_and_sizeof;
70 template <> struct index_and_sizeof<char>
74 template <> struct index_and_sizeof<unsigned char>
78 template <> struct index_and_sizeof<short>
82 template <> struct index_and_sizeof<unsigned short>
86 template <> struct index_and_sizeof<int>
90 template <> struct index_and_sizeof<float>
94 template <> struct index_and_sizeof<double>
99 /////////////////////////////////////////////////////////////////////////////////////
102 static std::vector<uchar> scalarToVector(const cv::Scalar & sc, int depth, int ocn, int cn)
104 CV_Assert(ocn == cn || (ocn == 4 && cn == 3));
106 static const int sizeMap[] = { sizeof(uchar), sizeof(char), sizeof(ushort),
107 sizeof(short), sizeof(int), sizeof(float), sizeof(double) };
109 int elemSize1 = sizeMap[depth];
110 int bufSize = elemSize1 * ocn;
111 std::vector<uchar> _buf(bufSize);
112 uchar * buf = &_buf[0];
113 scalarToRawData(sc, buf, CV_MAKE_TYPE(depth, cn));
114 memset(buf + elemSize1 * cn, 0, (ocn - cn) * elemSize1);
119 static void threshold_runner(const oclMat &src, oclMat &dst, double thresh, double maxVal, int thresholdType)
121 bool ival = src.depth() < CV_32F;
122 int cn = src.channels(), vecSize = 4, depth = src.depth();
123 std::vector<uchar> thresholdValue = scalarToVector(cv::Scalar::all(ival ? cvFloor(thresh) : thresh), dst.depth(),
124 dst.oclchannels(), dst.channels());
125 std::vector<uchar> maxValue = scalarToVector(cv::Scalar::all(maxVal), dst.depth(), dst.oclchannels(), dst.channels());
127 const char * const thresholdMap[] = { "THRESH_BINARY", "THRESH_BINARY_INV", "THRESH_TRUNC",
128 "THRESH_TOZERO", "THRESH_TOZERO_INV" };
129 const char * const channelMap[] = { "", "", "2", "4", "4" };
130 const char * const typeMap[] = { "uchar", "char", "ushort", "short", "int", "float", "double" };
131 std::string buildOptions = format("-D T=%s%s -D %s", typeMap[depth], channelMap[cn], thresholdMap[thresholdType]);
133 int elemSize = src.elemSize();
134 int src_step = src.step / elemSize, src_offset = src.offset / elemSize;
135 int dst_step = dst.step / elemSize, dst_offset = dst.offset / elemSize;
137 std::vector< std::pair<size_t, const void *> > args;
138 args.push_back( std::make_pair(sizeof(cl_mem), (void *)&src.data));
139 args.push_back( std::make_pair(sizeof(cl_int), (void *)&src_offset));
140 args.push_back( std::make_pair(sizeof(cl_int), (void *)&src_step));
141 args.push_back( std::make_pair(sizeof(cl_mem), (void *)&dst.data));
142 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst_offset));
143 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst_step));
144 args.push_back( std::make_pair(thresholdValue.size(), (void *)&thresholdValue[0]));
145 args.push_back( std::make_pair(maxValue.size(), (void *)&maxValue[0]));
147 int max_index = dst.cols, cols = dst.cols;
148 if (cn == 1 && vecSize > 1)
150 CV_Assert(((vecSize - 1) & vecSize) == 0 && vecSize <= 16);
151 cols = divUp(cols, vecSize);
152 buildOptions += format(" -D VECTORIZED -D VT=%s%d -D VLOADN=vload%d -D VECSIZE=%d -D VSTOREN=vstore%d",
153 typeMap[depth], vecSize, vecSize, vecSize, vecSize);
155 int vecSizeBytes = vecSize * dst.elemSize1();
156 if ((dst.offset % dst.step) % vecSizeBytes == 0 && dst.step % vecSizeBytes == 0)
157 buildOptions += " -D DST_ALIGNED";
158 if ((src.offset % src.step) % vecSizeBytes == 0 && src.step % vecSizeBytes == 0)
159 buildOptions += " -D SRC_ALIGNED";
161 args.push_back( std::make_pair(sizeof(cl_int), (void *)&max_index));
164 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.rows));
165 args.push_back( std::make_pair(sizeof(cl_int), (void *)&cols));
167 size_t localThreads[3] = { 16, 16, 1 };
168 size_t globalThreads[3] = { cols, dst.rows, 1 };
170 openCLExecuteKernel(src.clCxt, &imgproc_threshold, "threshold", globalThreads, localThreads, args,
171 -1, -1, buildOptions.c_str());
174 double threshold(const oclMat &src, oclMat &dst, double thresh, double maxVal, int thresholdType)
176 CV_Assert(thresholdType == THRESH_BINARY || thresholdType == THRESH_BINARY_INV || thresholdType == THRESH_TRUNC
177 || thresholdType == THRESH_TOZERO || thresholdType == THRESH_TOZERO_INV);
179 dst.create(src.size(), src.type());
180 threshold_runner(src, dst, thresh, maxVal, thresholdType);
185 ////////////////////////////////////////////////////////////////////////////////////////////
186 /////////////////////////////// remap //////////////////////////////////////////////////
187 ////////////////////////////////////////////////////////////////////////////////////////////
189 void remap( const oclMat &src, oclMat &dst, oclMat &map1, oclMat &map2, int interpolation, int borderType, const Scalar &borderValue )
191 Context *clCxt = src.clCxt;
192 bool supportsDouble = clCxt->supportsFeature(FEATURE_CL_DOUBLE);
193 if (!supportsDouble && src.depth() == CV_64F)
195 CV_Error(CV_OpenCLDoubleNotSupported, "Selected device does not support double");
202 CV_Assert(interpolation == INTER_LINEAR || interpolation == INTER_NEAREST
203 /*|| interpolation == INTER_CUBIC || interpolation == INTER_LANCZOS4*/);
204 CV_Assert((map1.type() == CV_16SC2 && (map2.empty() || (interpolation == INTER_NEAREST &&
205 (map2.type() == CV_16UC1 || map2.type() == CV_16SC1)) )) ||
206 (map1.type() == CV_32FC2 && !map2.data) ||
207 (map1.type() == CV_32FC1 && map2.type() == CV_32FC1));
208 CV_Assert(!map2.data || map2.size() == map1.size());
209 CV_Assert(borderType == BORDER_CONSTANT || borderType == BORDER_REPLICATE || borderType == BORDER_WRAP
210 || borderType == BORDER_REFLECT_101 || borderType == BORDER_REFLECT);
212 dst.create(map1.size(), src.type());
214 const char * const typeMap[] = { "uchar", "char", "ushort", "short", "int", "float", "double" };
215 const char * const channelMap[] = { "", "", "2", "4", "4" };
216 const char * const interMap[] = { "INTER_NEAREST", "INTER_LINEAR", "INTER_CUBIC", "INTER_LINEAR", "INTER_LANCZOS" };
217 const char * const borderMap[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", "BORDER_WRAP",
218 "BORDER_REFLECT_101", "BORDER_TRANSPARENT" };
220 String kernelName = "remap";
221 if (map1.type() == CV_32FC2 && map2.empty())
222 kernelName = kernelName + "_32FC2";
223 else if (map1.type() == CV_16SC2)
225 kernelName = kernelName + "_16SC2";
227 kernelName = kernelName + "_16UC1";
229 else if (map1.type() == CV_32FC1 && map2.type() == CV_32FC1)
230 kernelName = kernelName + "_2_32FC1";
232 CV_Error(Error::StsBadArg, "Unsupported map types");
234 int ocn = dst.oclchannels();
235 size_t localThreads[3] = { 16, 16, 1};
236 size_t globalThreads[3] = { dst.cols, dst.rows, 1};
238 Mat scalar(1, 1, CV_MAKE_TYPE(dst.depth(), ocn), borderValue);
239 String buildOptions = format("-D %s -D %s -D T=%s%s", interMap[interpolation],
240 borderMap[borderType], typeMap[src.depth()], channelMap[ocn]);
242 if (interpolation != INTER_NEAREST)
244 int wdepth = std::max(CV_32F, dst.depth());
245 buildOptions = buildOptions
246 + format(" -D WT=%s%s -D convertToT=convert_%s%s%s -D convertToWT=convert_%s%s"
247 " -D convertToWT2=convert_%s2 -D WT2=%s2",
248 typeMap[wdepth], channelMap[ocn],
249 typeMap[src.depth()], channelMap[ocn], src.depth() < CV_32F ? "_sat_rte" : "",
250 typeMap[wdepth], channelMap[ocn],
251 typeMap[wdepth], typeMap[wdepth]);
254 int src_step = src.step / src.elemSize(), src_offset = src.offset / src.elemSize();
255 int map1_step = map1.step / map1.elemSize(), map1_offset = map1.offset / map1.elemSize();
256 int map2_step = map2.step / map2.elemSize(), map2_offset = map2.offset / map2.elemSize();
257 int dst_step = dst.step / dst.elemSize(), dst_offset = dst.offset / dst.elemSize();
259 std::vector< std::pair<size_t, const void *> > args;
260 args.push_back( std::make_pair(sizeof(cl_mem), (void *)&src.data));
261 args.push_back( std::make_pair(sizeof(cl_mem), (void *)&dst.data));
262 args.push_back( std::make_pair(sizeof(cl_mem), (void *)&map1.data));
264 args.push_back( std::make_pair(sizeof(cl_mem), (void *)&map2.data));
265 args.push_back( std::make_pair(sizeof(cl_int), (void *)&src_offset));
266 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst_offset));
267 args.push_back( std::make_pair(sizeof(cl_int), (void *)&map1_offset));
269 args.push_back( std::make_pair(sizeof(cl_int), (void *)&map2_offset));
270 args.push_back( std::make_pair(sizeof(cl_int), (void *)&src_step));
271 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst_step));
272 args.push_back( std::make_pair(sizeof(cl_int), (void *)&map1_step));
274 args.push_back( std::make_pair(sizeof(cl_int), (void *)&map2_step));
275 args.push_back( std::make_pair(sizeof(cl_int), (void *)&src.cols));
276 args.push_back( std::make_pair(sizeof(cl_int), (void *)&src.rows));
277 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.cols));
278 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.rows));
279 args.push_back( std::make_pair(scalar.elemSize(), (void *)scalar.data));
281 openCLExecuteKernel(clCxt, &imgproc_remap, kernelName, globalThreads, localThreads, args, -1, -1, buildOptions.c_str());
284 ////////////////////////////////////////////////////////////////////////////////////////////
287 static void resize_gpu( const oclMat &src, oclMat &dst, double fx, double fy, int interpolation)
289 CV_Assert( (src.channels() == dst.channels()) );
290 Context *clCxt = src.clCxt;
293 double ifx_d = 1. / fx;
294 double ify_d = 1. / fy;
295 int srcStep_in_pixel = src.step1() / src.oclchannels();
296 int srcoffset_in_pixel = src.offset / src.elemSize();
297 int dstStep_in_pixel = dst.step1() / dst.oclchannels();
298 int dstoffset_in_pixel = dst.offset / dst.elemSize();
301 if (interpolation == INTER_LINEAR)
302 kernelName = "resizeLN";
303 else if (interpolation == INTER_NEAREST)
304 kernelName = "resizeNN";
306 //TODO: improve this kernel
307 size_t blkSizeX = 16, blkSizeY = 16;
309 if (src.type() == CV_8UC1)
311 size_t cols = (dst.cols + dst.offset % 4 + 3) / 4;
312 glbSizeX = cols % blkSizeX == 0 && cols != 0 ? cols : (cols / blkSizeX + 1) * blkSizeX;
315 glbSizeX = dst.cols % blkSizeX == 0 && dst.cols != 0 ? dst.cols : (dst.cols / blkSizeX + 1) * blkSizeX;
317 size_t glbSizeY = dst.rows % blkSizeY == 0 && dst.rows != 0 ? dst.rows : (dst.rows / blkSizeY + 1) * blkSizeY;
318 size_t globalThreads[3] = {glbSizeX, glbSizeY, 1};
319 size_t localThreads[3] = {blkSizeX, blkSizeY, 1};
321 std::vector< std::pair<size_t, const void *> > args;
322 if (interpolation == INTER_NEAREST)
324 args.push_back( std::make_pair(sizeof(cl_mem), (void *)&dst.data));
325 args.push_back( std::make_pair(sizeof(cl_mem), (void *)&src.data));
326 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dstoffset_in_pixel));
327 args.push_back( std::make_pair(sizeof(cl_int), (void *)&srcoffset_in_pixel));
328 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dstStep_in_pixel));
329 args.push_back( std::make_pair(sizeof(cl_int), (void *)&srcStep_in_pixel));
330 args.push_back( std::make_pair(sizeof(cl_int), (void *)&src.cols));
331 args.push_back( std::make_pair(sizeof(cl_int), (void *)&src.rows));
332 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.cols));
333 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.rows));
334 if (src.clCxt->supportsFeature(FEATURE_CL_DOUBLE))
336 args.push_back( std::make_pair(sizeof(cl_double), (void *)&ifx_d));
337 args.push_back( std::make_pair(sizeof(cl_double), (void *)&ify_d));
341 args.push_back( std::make_pair(sizeof(cl_float), (void *)&ifx));
342 args.push_back( std::make_pair(sizeof(cl_float), (void *)&ify));
347 args.push_back( std::make_pair(sizeof(cl_mem), (void *)&dst.data));
348 args.push_back( std::make_pair(sizeof(cl_mem), (void *)&src.data));
349 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dstoffset_in_pixel));
350 args.push_back( std::make_pair(sizeof(cl_int), (void *)&srcoffset_in_pixel));
351 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dstStep_in_pixel));
352 args.push_back( std::make_pair(sizeof(cl_int), (void *)&srcStep_in_pixel));
353 args.push_back( std::make_pair(sizeof(cl_int), (void *)&src.cols));
354 args.push_back( std::make_pair(sizeof(cl_int), (void *)&src.rows));
355 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.cols));
356 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.rows));
357 args.push_back( std::make_pair(sizeof(cl_float), (void *)&ifx));
358 args.push_back( std::make_pair(sizeof(cl_float), (void *)&ify));
361 openCLExecuteKernel(clCxt, &imgproc_resize, kernelName, globalThreads, localThreads, args, src.oclchannels(), src.depth());
364 void resize(const oclMat &src, oclMat &dst, Size dsize,
365 double fx, double fy, int interpolation)
367 CV_Assert(src.type() == CV_8UC1 || src.type() == CV_8UC3 || src.type() == CV_8UC4
368 || src.type() == CV_32FC1 || src.type() == CV_32FC3 || src.type() == CV_32FC4);
369 CV_Assert(interpolation == INTER_LINEAR || interpolation == INTER_NEAREST);
370 CV_Assert( src.size().area() > 0 );
371 CV_Assert( !(dsize == Size()) || (fx > 0 && fy > 0) );
373 if (!(dsize == Size()) && (fx > 0 && fy > 0))
374 if (dsize.width != (int)(src.cols * fx) || dsize.height != (int)(src.rows * fy))
375 CV_Error(Error::StsUnmatchedSizes, "invalid dsize and fx, fy!");
377 if ( dsize == Size() )
378 dsize = Size(saturate_cast<int>(src.cols * fx), saturate_cast<int>(src.rows * fy));
381 fx = (double)dsize.width / src.cols;
382 fy = (double)dsize.height / src.rows;
385 dst.create(dsize, src.type());
387 if ( interpolation == INTER_NEAREST || interpolation == INTER_LINEAR )
389 resize_gpu( src, dst, fx, fy, interpolation);
393 CV_Error(Error::StsUnsupportedFormat, "Non-supported interpolation method");
396 ////////////////////////////////////////////////////////////////////////
399 void medianFilter(const oclMat &src, oclMat &dst, int m)
401 CV_Assert( m % 2 == 1 && m > 1 );
402 CV_Assert( (src.depth() == CV_8U || src.depth() == CV_32F) && (src.channels() == 1 || src.channels() == 4));
403 dst.create(src.size(), src.type());
405 int srcStep = src.step / src.elemSize(), dstStep = dst.step / dst.elemSize();
406 int srcOffset = src.offset / src.elemSize(), dstOffset = dst.offset / dst.elemSize();
408 Context *clCxt = src.clCxt;
410 std::vector< std::pair<size_t, const void *> > args;
411 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&src.data));
412 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst.data));
413 args.push_back( std::make_pair( sizeof(cl_int), (void *)&srcOffset));
414 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dstOffset));
415 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.cols));
416 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.rows));
417 args.push_back( std::make_pair( sizeof(cl_int), (void *)&srcStep));
418 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dstStep));
420 size_t globalThreads[3] = {(src.cols + 18) / 16 * 16, (src.rows + 15) / 16 * 16, 1};
421 size_t localThreads[3] = {16, 16, 1};
425 String kernelName = "medianFilter3";
426 openCLExecuteKernel(clCxt, &imgproc_median, kernelName, globalThreads, localThreads, args, src.oclchannels(), src.depth());
430 String kernelName = "medianFilter5";
431 openCLExecuteKernel(clCxt, &imgproc_median, kernelName, globalThreads, localThreads, args, src.oclchannels(), src.depth());
434 CV_Error(Error::StsBadArg, "Non-supported filter length");
437 ////////////////////////////////////////////////////////////////////////
440 void copyMakeBorder(const oclMat &src, oclMat &dst, int top, int bottom, int left, int right, int bordertype, const Scalar &scalar)
442 if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
444 CV_Error(Error::OpenCLDoubleNotSupported, "Selected device does not support double");
450 CV_Assert(top >= 0 && bottom >= 0 && left >= 0 && right >= 0);
452 if( (_src.wholecols != _src.cols || _src.wholerows != _src.rows) && (bordertype & BORDER_ISOLATED) == 0 )
456 _src.locateROI(wholeSize, ofs);
457 int dtop = std::min(ofs.y, top);
458 int dbottom = std::min(wholeSize.height - _src.rows - ofs.y, bottom);
459 int dleft = std::min(ofs.x, left);
460 int dright = std::min(wholeSize.width - _src.cols - ofs.x, right);
461 _src.adjustROI(dtop, dbottom, dleft, dright);
467 bordertype &= ~cv::BORDER_ISOLATED;
469 dst.create(_src.rows + top + bottom, _src.cols + left + right, _src.type());
470 int srcStep = _src.step / _src.elemSize(), dstStep = dst.step / dst.elemSize();
471 int srcOffset = _src.offset / _src.elemSize(), dstOffset = dst.offset / dst.elemSize();
472 int depth = _src.depth(), ochannels = _src.oclchannels();
474 int __bordertype[] = { BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT, BORDER_WRAP, BORDER_REFLECT_101 };
475 const char *borderstr[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", "BORDER_WRAP", "BORDER_REFLECT_101" };
477 int bordertype_index = -1;
478 for (int i = 0, end = sizeof(__bordertype) / sizeof(int); i < end; i++)
479 if (__bordertype[i] == bordertype)
481 bordertype_index = i;
484 if (bordertype_index < 0)
485 CV_Error(Error::StsBadArg, "Unsupported border type");
487 size_t localThreads[3] = { 16, 16, 1 };
488 size_t globalThreads[3] = { dst.cols, dst.rows, 1 };
490 std::vector< std::pair<size_t, const void *> > args;
491 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&_src.data));
492 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst.data));
493 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.cols));
494 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.rows));
495 args.push_back( std::make_pair( sizeof(cl_int), (void *)&_src.cols));
496 args.push_back( std::make_pair( sizeof(cl_int), (void *)&_src.rows));
497 args.push_back( std::make_pair( sizeof(cl_int), (void *)&srcStep));
498 args.push_back( std::make_pair( sizeof(cl_int), (void *)&srcOffset));
499 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dstStep));
500 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dstOffset));
501 args.push_back( std::make_pair( sizeof(cl_int), (void *)&top));
502 args.push_back( std::make_pair( sizeof(cl_int), (void *)&left));
504 const char * const typeMap[] = { "uchar", "char", "ushort", "short", "int", "float", "double" };
505 const char * const channelMap[] = { "", "", "2", "4", "4" };
506 std::string buildOptions = format("-D GENTYPE=%s%s -D %s",
507 typeMap[depth], channelMap[ochannels],
508 borderstr[bordertype_index]);
510 int cn = src.channels(), ocn = src.oclchannels();
511 int bufSize = src.elemSize1() * ocn;
512 AutoBuffer<uchar> _buf(bufSize);
513 uchar * buf = (uchar *)_buf;
514 scalarToRawData(scalar, buf, dst.type());
515 memset(buf + src.elemSize1() * cn, 0, (ocn - cn) * src.elemSize1());
517 args.push_back( std::make_pair( bufSize , (void *)buf ));
519 openCLExecuteKernel(src.clCxt, &imgproc_copymakeboder, "copymakeborder", globalThreads,
520 localThreads, args, -1, -1, buildOptions.c_str());
523 ////////////////////////////////////////////////////////////////////////
530 void convert_coeffs(F *M)
532 double D = M[0] * M[4] - M[1] * M[3];
533 D = D != 0 ? 1. / D : 0;
534 double A11 = M[4] * D, A22 = M[0] * D;
539 double b1 = -M[0] * M[2] - M[1] * M[5];
540 double b2 = -M[3] * M[2] - M[4] * M[5];
545 double invert(double *M)
547 #define Sd(y,x) (Sd[y*3+x])
548 #define Dd(y,x) (Dd[y*3+x])
549 #define det3(m) (m(0,0)*(m(1,1)*m(2,2) - m(1,2)*m(2,1)) - \
550 m(0,1)*(m(1,0)*m(2,2) - m(1,2)*m(2,0)) + \
551 m(0,2)*(m(1,0)*m(2,1) - m(1,1)*m(2,0)))
562 t[0] = (Sd(1, 1) * Sd(2, 2) - Sd(1, 2) * Sd(2, 1)) * d;
563 t[1] = (Sd(0, 2) * Sd(2, 1) - Sd(0, 1) * Sd(2, 2)) * d;
564 t[2] = (Sd(0, 1) * Sd(1, 2) - Sd(0, 2) * Sd(1, 1)) * d;
566 t[3] = (Sd(1, 2) * Sd(2, 0) - Sd(1, 0) * Sd(2, 2)) * d;
567 t[4] = (Sd(0, 0) * Sd(2, 2) - Sd(0, 2) * Sd(2, 0)) * d;
568 t[5] = (Sd(0, 2) * Sd(1, 0) - Sd(0, 0) * Sd(1, 2)) * d;
570 t[6] = (Sd(1, 0) * Sd(2, 1) - Sd(1, 1) * Sd(2, 0)) * d;
571 t[7] = (Sd(0, 1) * Sd(2, 0) - Sd(0, 0) * Sd(2, 1)) * d;
572 t[8] = (Sd(0, 0) * Sd(1, 1) - Sd(0, 1) * Sd(1, 0)) * d;
587 void warpAffine_gpu(const oclMat &src, oclMat &dst, F coeffs[2][3], int interpolation)
589 CV_Assert( (src.oclchannels() == dst.oclchannels()) );
590 int srcStep = src.step1();
591 int dstStep = dst.step1();
592 float float_coeffs[2][3];
595 Context *clCxt = src.clCxt;
596 String s[3] = {"NN", "Linear", "Cubic"};
597 String kernelName = "warpAffine" + s[interpolation];
599 if (src.clCxt->supportsFeature(FEATURE_CL_DOUBLE))
602 coeffs_cm = clCreateBuffer(*(cl_context*)clCxt->getOpenCLContextPtr(), CL_MEM_READ_WRITE, sizeof(F) * 2 * 3, NULL, &st );
603 openCLVerifyCall(st);
604 openCLSafeCall(clEnqueueWriteBuffer(*(cl_command_queue*)clCxt->getOpenCLCommandQueuePtr(), (cl_mem)coeffs_cm, 1, 0,
605 sizeof(F) * 2 * 3, coeffs, 0, 0, 0));
610 for(int m = 0; m < 2; m++)
611 for(int n = 0; n < 3; n++)
612 float_coeffs[m][n] = coeffs[m][n];
614 coeffs_cm = clCreateBuffer(*(cl_context*)clCxt->getOpenCLContextPtr(), CL_MEM_READ_WRITE, sizeof(float) * 2 * 3, NULL, &st );
615 openCLSafeCall(clEnqueueWriteBuffer(*(cl_command_queue*)clCxt->getOpenCLCommandQueuePtr(), (cl_mem)coeffs_cm,
616 1, 0, sizeof(float) * 2 * 3, float_coeffs, 0, 0, 0));
619 //TODO: improve this kernel
620 size_t blkSizeX = 16, blkSizeY = 16;
624 if (src.type() == CV_8UC1 && interpolation != 2)
626 cols = (dst.cols + dst.offset % 4 + 3) / 4;
627 glbSizeX = cols % blkSizeX == 0 ? cols : (cols / blkSizeX + 1) * blkSizeX;
632 glbSizeX = dst.cols % blkSizeX == 0 ? dst.cols : (dst.cols / blkSizeX + 1) * blkSizeX;
635 size_t glbSizeY = dst.rows % blkSizeY == 0 ? dst.rows : (dst.rows / blkSizeY + 1) * blkSizeY;
636 size_t globalThreads[3] = {glbSizeX, glbSizeY, 1};
637 size_t localThreads[3] = {blkSizeX, blkSizeY, 1};
639 std::vector< std::pair<size_t, const void *> > args;
641 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&src.data));
642 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&dst.data));
643 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.cols));
644 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.rows));
645 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.cols));
646 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.rows));
647 args.push_back(std::make_pair(sizeof(cl_int), (void *)&srcStep));
648 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dstStep));
649 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.offset));
650 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.offset));
651 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&coeffs_cm));
652 args.push_back(std::make_pair(sizeof(cl_int), (void *)&cols));
654 openCLExecuteKernel(clCxt, &imgproc_warpAffine, kernelName, globalThreads, localThreads, args, src.oclchannels(), src.depth());
655 openCLSafeCall(clReleaseMemObject(coeffs_cm));
658 void warpPerspective_gpu(const oclMat &src, oclMat &dst, double coeffs[3][3], int interpolation)
660 CV_Assert( (src.oclchannels() == dst.oclchannels()) );
661 int srcStep = src.step1();
662 int dstStep = dst.step1();
663 float float_coeffs[3][3];
666 Context *clCxt = src.clCxt;
667 String s[3] = {"NN", "Linear", "Cubic"};
668 String kernelName = "warpPerspective" + s[interpolation];
670 if (src.clCxt->supportsFeature(FEATURE_CL_DOUBLE))
673 coeffs_cm = clCreateBuffer(*(cl_context*)clCxt->getOpenCLContextPtr(), CL_MEM_READ_WRITE, sizeof(double) * 3 * 3, NULL, &st );
674 openCLVerifyCall(st);
675 openCLSafeCall(clEnqueueWriteBuffer(*(cl_command_queue*)clCxt->getOpenCLCommandQueuePtr(), (cl_mem)coeffs_cm, 1, 0,
676 sizeof(double) * 3 * 3, coeffs, 0, 0, 0));
681 for(int m = 0; m < 3; m++)
682 for(int n = 0; n < 3; n++)
683 float_coeffs[m][n] = coeffs[m][n];
685 coeffs_cm = clCreateBuffer(*(cl_context*)clCxt->getOpenCLContextPtr(), CL_MEM_READ_WRITE, sizeof(float) * 3 * 3, NULL, &st );
686 openCLVerifyCall(st);
687 openCLSafeCall(clEnqueueWriteBuffer(*(cl_command_queue*)clCxt->getOpenCLCommandQueuePtr(), (cl_mem)coeffs_cm, 1, 0,
688 sizeof(float) * 3 * 3, float_coeffs, 0, 0, 0));
691 //TODO: improve this kernel
692 size_t blkSizeX = 16, blkSizeY = 16;
695 if (src.type() == CV_8UC1 && interpolation == 0)
697 cols = (dst.cols + dst.offset % 4 + 3) / 4;
698 glbSizeX = cols % blkSizeX == 0 ? cols : (cols / blkSizeX + 1) * blkSizeX;
703 glbSizeX = dst.cols % blkSizeX == 0 ? dst.cols : (dst.cols / blkSizeX + 1) * blkSizeX;
706 size_t glbSizeY = dst.rows % blkSizeY == 0 ? dst.rows : (dst.rows / blkSizeY + 1) * blkSizeY;
707 size_t globalThreads[3] = {glbSizeX, glbSizeY, 1};
708 size_t localThreads[3] = {blkSizeX, blkSizeY, 1};
710 std::vector< std::pair<size_t, const void *> > args;
712 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&src.data));
713 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&dst.data));
714 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.cols));
715 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.rows));
716 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.cols));
717 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.rows));
718 args.push_back(std::make_pair(sizeof(cl_int), (void *)&srcStep));
719 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dstStep));
720 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.offset));
721 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.offset));
722 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&coeffs_cm));
723 args.push_back(std::make_pair(sizeof(cl_int), (void *)&cols));
725 openCLExecuteKernel(clCxt, &imgproc_warpPerspective, kernelName, globalThreads, localThreads, args, src.oclchannels(), src.depth());
726 openCLSafeCall(clReleaseMemObject(coeffs_cm));
730 void warpAffine(const oclMat &src, oclMat &dst, const Mat &M, Size dsize, int flags)
732 int interpolation = flags & INTER_MAX;
734 CV_Assert((src.depth() == CV_8U || src.depth() == CV_32F) && src.oclchannels() != 2 && src.oclchannels() != 3);
735 CV_Assert(interpolation == INTER_NEAREST || interpolation == INTER_LINEAR || interpolation == INTER_CUBIC);
737 dst.create(dsize, src.type());
739 CV_Assert(M.rows == 2 && M.cols == 3);
741 int warpInd = (flags & WARP_INVERSE_MAP) >> 4;
745 Mat coeffsMat(2, 3, CV_64F, (void *)coeffsM);
746 M.convertTo(coeffsMat, coeffsMat.type());
748 convert_coeffs(coeffsM);
750 for(int i = 0; i < 2; ++i)
751 for(int j = 0; j < 3; ++j)
752 coeffs[i][j] = coeffsM[i*3+j];
754 warpAffine_gpu(src, dst, coeffs, interpolation);
757 void warpPerspective(const oclMat &src, oclMat &dst, const Mat &M, Size dsize, int flags)
759 int interpolation = flags & INTER_MAX;
761 CV_Assert((src.depth() == CV_8U || src.depth() == CV_32F) && src.oclchannels() != 2 && src.oclchannels() != 3);
762 CV_Assert(interpolation == INTER_NEAREST || interpolation == INTER_LINEAR || interpolation == INTER_CUBIC);
764 dst.create(dsize, src.type());
767 CV_Assert(M.rows == 3 && M.cols == 3);
769 int warpInd = (flags & WARP_INVERSE_MAP) >> 4;
773 Mat coeffsMat(3, 3, CV_64F, (void *)coeffsM);
774 M.convertTo(coeffsMat, coeffsMat.type());
778 for(int i = 0; i < 3; ++i)
779 for(int j = 0; j < 3; ++j)
780 coeffs[i][j] = coeffsM[i*3+j];
782 warpPerspective_gpu(src, dst, coeffs, interpolation);
785 ////////////////////////////////////////////////////////////////////////
788 void integral(const oclMat &src, oclMat &sum, oclMat &sqsum)
790 CV_Assert(src.type() == CV_8UC1);
791 if (!src.clCxt->supportsFeature(ocl::FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
793 CV_Error(Error::OpenCLDoubleNotSupported, "Select device doesn't support double");
798 int offset = src.offset / vlen;
799 int pre_invalid = src.offset % vlen;
800 int vcols = (pre_invalid + src.cols + vlen - 1) / vlen;
802 oclMat t_sum , t_sqsum;
803 int w = src.cols + 1, h = src.rows + 1;
804 int depth = src.depth() == CV_8U ? CV_32S : CV_64F;
805 int type = CV_MAKE_TYPE(depth, 1);
807 t_sum.create(src.cols, src.rows, type);
808 sum.create(h, w, type);
810 t_sqsum.create(src.cols, src.rows, CV_32FC1);
811 sqsum.create(h, w, CV_32FC1);
813 int sum_offset = sum.offset / vlen;
814 int sqsum_offset = sqsum.offset / vlen;
816 std::vector<std::pair<size_t , const void *> > args;
817 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&src.data ));
818 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&t_sum.data ));
819 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&t_sqsum.data ));
820 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&offset ));
821 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&pre_invalid ));
822 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.rows ));
823 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.cols ));
824 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.step ));
825 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.step));
826 size_t gt[3] = {((vcols + 1) / 2) * 256, 1, 1}, lt[3] = {256, 1, 1};
827 openCLExecuteKernel(src.clCxt, &imgproc_integral, "integral_cols", gt, lt, args, -1, depth);
830 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&t_sum.data ));
831 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&t_sqsum.data ));
832 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&sum.data ));
833 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&sqsum.data ));
834 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.rows ));
835 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.cols ));
836 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.step ));
837 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sum.step));
838 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sqsum.step));
839 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sum_offset));
840 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sqsum_offset));
841 size_t gt2[3] = {t_sum.cols * 32, 1, 1}, lt2[3] = {256, 1, 1};
842 openCLExecuteKernel(src.clCxt, &imgproc_integral, "integral_rows", gt2, lt2, args, -1, depth);
845 void integral(const oclMat &src, oclMat &sum)
847 CV_Assert(src.type() == CV_8UC1);
849 int offset = src.offset / vlen;
850 int pre_invalid = src.offset % vlen;
851 int vcols = (pre_invalid + src.cols + vlen - 1) / vlen;
854 int w = src.cols + 1, h = src.rows + 1;
855 int depth = src.depth() == CV_8U ? CV_32S : CV_32F;
856 int type = CV_MAKE_TYPE(depth, 1);
858 t_sum.create(src.cols, src.rows, type);
859 sum.create(h, w, type);
861 int sum_offset = sum.offset / vlen;
862 std::vector<std::pair<size_t , const void *> > args;
863 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&src.data ));
864 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&t_sum.data ));
865 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&offset ));
866 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&pre_invalid ));
867 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.rows ));
868 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.cols ));
869 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.step ));
870 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.step));
871 size_t gt[3] = {((vcols + 1) / 2) * 256, 1, 1}, lt[3] = {256, 1, 1};
872 openCLExecuteKernel(src.clCxt, &imgproc_integral_sum, "integral_sum_cols", gt, lt, args, -1, depth);
875 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&t_sum.data ));
876 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&sum.data ));
877 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.rows ));
878 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.cols ));
879 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.step ));
880 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sum.step));
881 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sum_offset));
882 size_t gt2[3] = {t_sum.cols * 32, 1, 1}, lt2[3] = {256, 1, 1};
883 openCLExecuteKernel(src.clCxt, &imgproc_integral_sum, "integral_sum_rows", gt2, lt2, args, -1, depth);
886 /////////////////////// corner //////////////////////////////
888 static void extractCovData(const oclMat &src, oclMat &Dx, oclMat &Dy,
889 int blockSize, int ksize, int borderType)
891 CV_Assert(src.type() == CV_8UC1 || src.type() == CV_32FC1);
892 double scale = static_cast<double>(1 << ((ksize > 0 ? ksize : 3) - 1)) * blockSize;
896 if (src.depth() == CV_8U)
906 Context* clCxt = Context::getContext();
907 if(clCxt->supportsFeature(FEATURE_CL_INTEL_DEVICE) && src.type() == CV_8UC1 &&
908 src.cols % 8 == 0 && src.rows % 8 == 0 &&
910 (borderType ==cv::BORDER_REFLECT ||
911 borderType == cv::BORDER_REPLICATE ||
912 borderType ==cv::BORDER_REFLECT101 ||
913 borderType ==cv::BORDER_WRAP))
915 Dx.create(src.size(), CV_32FC1);
916 Dy.create(src.size(), CV_32FC1);
918 const unsigned int block_x = 8;
919 const unsigned int block_y = 8;
921 unsigned int src_pitch = src.step;
922 unsigned int dst_pitch = Dx.cols;
924 float _scale = scale;
926 std::vector<std::pair<size_t , const void *> > args;
927 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&src.data ));
928 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&Dx.data ));
929 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&Dy.data ));
930 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.cols ));
931 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.rows ));
932 args.push_back( std::make_pair( sizeof(cl_uint) , (void *)&src_pitch ));
933 args.push_back( std::make_pair( sizeof(cl_uint) , (void *)&dst_pitch ));
934 args.push_back( std::make_pair( sizeof(cl_float) , (void *)&_scale ));
935 size_t gt2[3] = {src.cols, src.rows, 1}, lt2[3] = {block_x, block_y, 1};
937 String option = "-D BLK_X=8 -D BLK_Y=8";
940 case cv::BORDER_REPLICATE:
941 option = option + " -D BORDER_REPLICATE";
943 case cv::BORDER_REFLECT:
944 option = option + " -D BORDER_REFLECT";
946 case cv::BORDER_REFLECT101:
947 option = option + " -D BORDER_REFLECT101";
949 case cv::BORDER_WRAP:
950 option = option + " -D BORDER_WRAP";
953 openCLExecuteKernel(src.clCxt, &imgproc_sobel3, "sobel3", gt2, lt2, args, -1, -1, option.c_str() );
957 Sobel(src, Dx, CV_32F, 1, 0, ksize, scale, 0, borderType);
958 Sobel(src, Dy, CV_32F, 0, 1, ksize, scale, 0, borderType);
963 Scharr(src, Dx, CV_32F, 1, 0, scale, 0, borderType);
964 Scharr(src, Dy, CV_32F, 0, 1, scale, 0, borderType);
966 CV_Assert(Dx.offset == 0 && Dy.offset == 0);
969 static void corner_ocl(const cv::ocl::ProgramEntry* source, String kernelName, int block_size, float k, oclMat &Dx, oclMat &Dy,
970 oclMat &dst, int border_type)
975 case cv::BORDER_CONSTANT:
976 sprintf(borderType, "BORDER_CONSTANT");
978 case cv::BORDER_REFLECT101:
979 sprintf(borderType, "BORDER_REFLECT101");
981 case cv::BORDER_REFLECT:
982 sprintf(borderType, "BORDER_REFLECT");
984 case cv::BORDER_REPLICATE:
985 sprintf(borderType, "BORDER_REPLICATE");
988 CV_Error(Error::StsBadFlag, "BORDER type is not supported!");
991 std::string buildOptions = format("-D anX=%d -D anY=%d -D ksX=%d -D ksY=%d -D %s",
992 block_size / 2, block_size / 2, block_size, block_size, borderType);
994 size_t blockSizeX = 256, blockSizeY = 1;
995 size_t gSize = blockSizeX - block_size / 2 * 2;
996 size_t globalSizeX = (Dx.cols) % gSize == 0 ? Dx.cols / gSize * blockSizeX : (Dx.cols / gSize + 1) * blockSizeX;
997 size_t rows_per_thread = 2;
998 size_t globalSizeY = ((Dx.rows + rows_per_thread - 1) / rows_per_thread) % blockSizeY == 0 ?
999 ((Dx.rows + rows_per_thread - 1) / rows_per_thread) :
1000 (((Dx.rows + rows_per_thread - 1) / rows_per_thread) / blockSizeY + 1) * blockSizeY;
1002 size_t gt[3] = { globalSizeX, globalSizeY, 1 };
1003 size_t lt[3] = { blockSizeX, blockSizeY, 1 };
1004 std::vector<std::pair<size_t , const void *> > args;
1005 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&Dx.data ));
1006 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&Dy.data));
1007 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&dst.data));
1008 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&Dx.offset ));
1009 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&Dx.wholerows ));
1010 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&Dx.wholecols ));
1011 args.push_back( std::make_pair(sizeof(cl_int), (void *)&Dx.step));
1012 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&Dy.offset ));
1013 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&Dy.wholerows ));
1014 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&Dy.wholecols ));
1015 args.push_back( std::make_pair(sizeof(cl_int), (void *)&Dy.step));
1016 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.offset));
1017 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.rows));
1018 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.cols));
1019 args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.step));
1020 args.push_back( std::make_pair( sizeof(cl_float) , (void *)&k));
1022 openCLExecuteKernel(dst.clCxt, source, kernelName, gt, lt, args, -1, -1, buildOptions.c_str());
1025 void cornerHarris(const oclMat &src, oclMat &dst, int blockSize, int ksize,
1026 double k, int borderType)
1029 cornerHarris_dxdy(src, dst, dx, dy, blockSize, ksize, k, borderType);
1032 void cornerHarris_dxdy(const oclMat &src, oclMat &dst, oclMat &dx, oclMat &dy, int blockSize, int ksize,
1033 double k, int borderType)
1035 if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
1037 CV_Error(Error::OpenCLDoubleNotSupported, "Selected device doesn't support double");
1041 CV_Assert(borderType == cv::BORDER_CONSTANT || borderType == cv::BORDER_REFLECT101 || borderType == cv::BORDER_REPLICATE
1042 || borderType == cv::BORDER_REFLECT);
1044 extractCovData(src, dx, dy, blockSize, ksize, borderType);
1045 dst.create(src.size(), CV_32FC1);
1046 corner_ocl(&imgproc_calcHarris, "calcHarris", blockSize, static_cast<float>(k), dx, dy, dst, borderType);
1049 void cornerMinEigenVal(const oclMat &src, oclMat &dst, int blockSize, int ksize, int borderType)
1052 cornerMinEigenVal_dxdy(src, dst, dx, dy, blockSize, ksize, borderType);
1055 void cornerMinEigenVal_dxdy(const oclMat &src, oclMat &dst, oclMat &dx, oclMat &dy, int blockSize, int ksize, int borderType)
1057 if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
1059 CV_Error(Error::OpenCLDoubleNotSupported, "Selected device doesn't support double");
1063 CV_Assert(borderType == cv::BORDER_CONSTANT || borderType == cv::BORDER_REFLECT101 ||
1064 borderType == cv::BORDER_REPLICATE || borderType == cv::BORDER_REFLECT);
1066 extractCovData(src, dx, dy, blockSize, ksize, borderType);
1067 dst.create(src.size(), CV_32F);
1069 corner_ocl(&imgproc_calcMinEigenVal, "calcMinEigenVal", blockSize, 0, dx, dy, dst, borderType);
1072 /////////////////////////////////// MeanShiftfiltering ///////////////////////////////////////////////
1074 static void meanShiftFiltering_gpu(const oclMat &src, oclMat dst, int sp, int sr, int maxIter, float eps)
1076 CV_Assert( (src.cols == dst.cols) && (src.rows == dst.rows) );
1077 CV_Assert( !(dst.step & 0x3) );
1079 //Arrange the NDRange
1080 int col = src.cols, row = src.rows;
1081 int ltx = 16, lty = 8;
1082 if (src.cols % ltx != 0)
1083 col = (col / ltx + 1) * ltx;
1084 if (src.rows % lty != 0)
1085 row = (row / lty + 1) * lty;
1087 size_t globalThreads[3] = {col, row, 1};
1088 size_t localThreads[3] = {ltx, lty, 1};
1091 std::vector<std::pair<size_t , const void *> > args;
1092 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&dst.data ));
1093 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst.step ));
1094 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&src.data ));
1095 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.step ));
1096 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst.offset ));
1097 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.offset ));
1098 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst.cols ));
1099 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst.rows ));
1100 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sp ));
1101 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sr ));
1102 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&maxIter ));
1103 args.push_back( std::make_pair( sizeof(cl_float) , (void *)&eps ));
1105 openCLExecuteKernel(src.clCxt, &meanShift, "meanshift_kernel", globalThreads, localThreads, args, -1, -1);
1108 void meanShiftFiltering(const oclMat &src, oclMat &dst, int sp, int sr, TermCriteria criteria)
1111 CV_Error(Error::StsBadArg, "The input image is empty");
1113 if ( src.depth() != CV_8U || src.oclchannels() != 4 )
1114 CV_Error(Error::StsUnsupportedFormat, "Only 8-bit, 4-channel images are supported");
1116 dst.create( src.size(), CV_8UC4 );
1118 if ( !(criteria.type & TermCriteria::MAX_ITER) )
1119 criteria.maxCount = 5;
1121 int maxIter = std::min(std::max(criteria.maxCount, 1), 100);
1124 if ( !(criteria.type & TermCriteria::EPS) )
1126 eps = (float)std::max(criteria.epsilon, 0.0);
1128 meanShiftFiltering_gpu(src, dst, sp, sr, maxIter, eps);
1131 static void meanShiftProc_gpu(const oclMat &src, oclMat dstr, oclMat dstsp, int sp, int sr, int maxIter, float eps)
1134 CV_Assert( (src.cols == dstr.cols) && (src.rows == dstr.rows) &&
1135 (src.rows == dstsp.rows) && (src.cols == dstsp.cols));
1136 CV_Assert( !(dstsp.step & 0x3) );
1138 //Arrange the NDRange
1139 int col = src.cols, row = src.rows;
1140 int ltx = 16, lty = 8;
1141 if (src.cols % ltx != 0)
1142 col = (col / ltx + 1) * ltx;
1143 if (src.rows % lty != 0)
1144 row = (row / lty + 1) * lty;
1146 size_t globalThreads[3] = {col, row, 1};
1147 size_t localThreads[3] = {ltx, lty, 1};
1150 std::vector<std::pair<size_t , const void *> > args;
1151 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&src.data ));
1152 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&dstr.data ));
1153 args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&dstsp.data ));
1154 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.step ));
1155 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dstr.step ));
1156 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dstsp.step ));
1157 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.offset ));
1158 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dstr.offset ));
1159 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dstsp.offset ));
1160 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dstr.cols ));
1161 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dstr.rows ));
1162 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sp ));
1163 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sr ));
1164 args.push_back( std::make_pair( sizeof(cl_int) , (void *)&maxIter ));
1165 args.push_back( std::make_pair( sizeof(cl_float) , (void *)&eps ));
1167 openCLExecuteKernel(src.clCxt, &meanShift, "meanshiftproc_kernel", globalThreads, localThreads, args, -1, -1);
1170 void meanShiftProc(const oclMat &src, oclMat &dstr, oclMat &dstsp, int sp, int sr, TermCriteria criteria)
1173 CV_Error(Error::StsBadArg, "The input image is empty");
1175 if ( src.depth() != CV_8U || src.oclchannels() != 4 )
1176 CV_Error(Error::StsUnsupportedFormat, "Only 8-bit, 4-channel images are supported");
1178 // if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE))
1180 // 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");
1184 dstr.create( src.size(), CV_8UC4 );
1185 dstsp.create( src.size(), CV_16SC2 );
1187 if ( !(criteria.type & TermCriteria::MAX_ITER) )
1188 criteria.maxCount = 5;
1190 int maxIter = std::min(std::max(criteria.maxCount, 1), 100);
1193 if ( !(criteria.type & TermCriteria::EPS) )
1195 eps = (float)std::max(criteria.epsilon, 0.0);
1197 meanShiftProc_gpu(src, dstr, dstsp, sp, sr, maxIter, eps);
1200 ///////////////////////////////////////////////////////////////////////////////////////////////////
1201 ////////////////////////////////////////////////////hist///////////////////////////////////////////////
1202 /////////////////////////////////////////////////////////////////////////////////////////////////////
1204 namespace histograms
1206 const int PARTIAL_HISTOGRAM256_COUNT = 256;
1207 const int HISTOGRAM256_BIN_COUNT = 256;
1209 ///////////////////////////////calcHist/////////////////////////////////////////////////////////////////
1210 static void calc_sub_hist(const oclMat &mat_src, const oclMat &mat_sub_hist)
1212 using namespace histograms;
1214 int depth = mat_src.depth();
1216 size_t localThreads[3] = { HISTOGRAM256_BIN_COUNT, 1, 1 };
1217 size_t globalThreads[3] = { PARTIAL_HISTOGRAM256_COUNT *localThreads[0], 1, 1};
1220 int dataWidth_bits = 4;
1221 int mask = dataWidth - 1;
1223 int cols = mat_src.cols * mat_src.oclchannels();
1224 int src_offset = mat_src.offset;
1225 int hist_step = mat_sub_hist.step >> 2;
1226 int left_col = 0, right_col = 0;
1228 if (cols >= dataWidth * 2 - 1)
1230 left_col = dataWidth - (src_offset & mask);
1232 src_offset += left_col;
1234 right_col = cols & mask;
1242 globalThreads[0] = 0;
1245 std::vector<std::pair<size_t , const void *> > args;
1246 if (globalThreads[0] != 0)
1248 int tempcols = cols >> dataWidth_bits;
1249 int inc_x = globalThreads[0] % tempcols;
1250 int inc_y = globalThreads[0] / tempcols;
1251 src_offset >>= dataWidth_bits;
1252 int src_step = mat_src.step >> dataWidth_bits;
1253 int datacount = tempcols * mat_src.rows;
1255 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_src.data));
1256 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src_step));
1257 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src_offset));
1258 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_sub_hist.data));
1259 args.push_back( std::make_pair( sizeof(cl_int), (void *)&datacount));
1260 args.push_back( std::make_pair( sizeof(cl_int), (void *)&tempcols));
1261 args.push_back( std::make_pair( sizeof(cl_int), (void *)&inc_x));
1262 args.push_back( std::make_pair( sizeof(cl_int), (void *)&inc_y));
1263 args.push_back( std::make_pair( sizeof(cl_int), (void *)&hist_step));
1265 openCLExecuteKernel(mat_src.clCxt, &imgproc_histogram, "calc_sub_hist", globalThreads, localThreads, args, -1, depth);
1268 if (left_col != 0 || right_col != 0)
1270 src_offset = mat_src.offset;
1271 localThreads[0] = 1;
1272 localThreads[1] = 256;
1273 globalThreads[0] = left_col + right_col;
1274 globalThreads[1] = mat_src.rows;
1277 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_src.data));
1278 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src.step));
1279 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src_offset));
1280 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_sub_hist.data));
1281 args.push_back( std::make_pair( sizeof(cl_int), (void *)&left_col));
1282 args.push_back( std::make_pair( sizeof(cl_int), (void *)&cols));
1283 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src.rows));
1284 args.push_back( std::make_pair( sizeof(cl_int), (void *)&hist_step));
1286 openCLExecuteKernel(mat_src.clCxt, &imgproc_histogram, "calc_sub_hist_border", globalThreads, localThreads, args, -1, depth);
1290 static void merge_sub_hist(const oclMat &sub_hist, oclMat &mat_hist)
1292 using namespace histograms;
1294 size_t localThreads[3] = { 256, 1, 1 };
1295 size_t globalThreads[3] = { HISTOGRAM256_BIN_COUNT *localThreads[0], 1, 1};
1296 int src_step = sub_hist.step >> 2;
1298 std::vector<std::pair<size_t , const void *> > args;
1299 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&sub_hist.data));
1300 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_hist.data));
1301 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src_step));
1303 openCLExecuteKernel(sub_hist.clCxt, &imgproc_histogram, "merge_hist", globalThreads, localThreads, args, -1, -1);
1306 void calcHist(const oclMat &mat_src, oclMat &mat_hist)
1308 using namespace histograms;
1309 CV_Assert(mat_src.type() == CV_8UC1);
1310 mat_hist.create(1, 256, CV_32SC1);
1312 oclMat buf(PARTIAL_HISTOGRAM256_COUNT, HISTOGRAM256_BIN_COUNT, CV_32SC1);
1315 calc_sub_hist(mat_src, buf);
1316 merge_sub_hist(buf, mat_hist);
1319 ///////////////////////////////////equalizeHist/////////////////////////////////////////////////////
1320 void equalizeHist(const oclMat &mat_src, oclMat &mat_dst)
1322 mat_dst.create(mat_src.rows, mat_src.cols, CV_8UC1);
1324 oclMat mat_hist(1, 256, CV_32SC1);
1326 calcHist(mat_src, mat_hist);
1328 size_t localThreads[3] = { 256, 1, 1};
1329 size_t globalThreads[3] = { 256, 1, 1};
1330 oclMat lut(1, 256, CV_8UC1);
1331 int total = mat_src.rows * mat_src.cols;
1333 std::vector<std::pair<size_t , const void *> > args;
1334 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&lut.data));
1335 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_hist.data));
1336 args.push_back( std::make_pair( sizeof(int), (void *)&total));
1338 openCLExecuteKernel(mat_src.clCxt, &imgproc_histogram, "calLUT", globalThreads, localThreads, args, -1, -1);
1339 LUT(mat_src, lut, mat_dst);
1342 ////////////////////////////////////////////////////////////////////////
1346 static void calcLut(const oclMat &src, oclMat &dst,
1347 const int tilesX, const int tilesY, const cv::Size tileSize,
1348 const int clipLimit, const float lutScale)
1351 tile_size.s[0] = tileSize.width;
1352 tile_size.s[1] = tileSize.height;
1354 std::vector<std::pair<size_t , const void *> > args;
1355 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&src.data ));
1356 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst.data ));
1357 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.step ));
1358 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.step ));
1359 args.push_back( std::make_pair( sizeof(cl_int2), (void *)&tile_size ));
1360 args.push_back( std::make_pair( sizeof(cl_int), (void *)&tilesX ));
1361 args.push_back( std::make_pair( sizeof(cl_int), (void *)&clipLimit ));
1362 args.push_back( std::make_pair( sizeof(cl_float), (void *)&lutScale ));
1363 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.offset ));
1364 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.offset ));
1366 String kernelName = "calcLut";
1367 size_t localThreads[3] = { 32, 8, 1 };
1368 size_t globalThreads[3] = { tilesX * localThreads[0], tilesY * localThreads[1], 1 };
1369 bool is_cpu = isCpuDevice();
1371 openCLExecuteKernel(Context::getContext(), &imgproc_clahe, kernelName, globalThreads, localThreads, args, -1, -1, (char*)"-D CPU");
1374 cl_kernel kernel = openCLGetKernelFromSource(Context::getContext(), &imgproc_clahe, kernelName);
1375 int wave_size = (int)queryWaveFrontSize(kernel);
1376 openCLSafeCall(clReleaseKernel(kernel));
1378 std::string opt = format("-D WAVE_SIZE=%d", wave_size);
1379 openCLExecuteKernel(Context::getContext(), &imgproc_clahe, kernelName, globalThreads, localThreads, args, -1, -1, opt.c_str());
1383 static void transform(const oclMat &src, oclMat &dst, const oclMat &lut,
1384 const int tilesX, const int tilesY, const Size & tileSize)
1387 tile_size.s[0] = tileSize.width;
1388 tile_size.s[1] = tileSize.height;
1390 std::vector<std::pair<size_t , const void *> > args;
1391 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&src.data ));
1392 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst.data ));
1393 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&lut.data ));
1394 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.step ));
1395 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.step ));
1396 args.push_back( std::make_pair( sizeof(cl_int), (void *)&lut.step ));
1397 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.cols ));
1398 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.rows ));
1399 args.push_back( std::make_pair( sizeof(cl_int2), (void *)&tile_size ));
1400 args.push_back( std::make_pair( sizeof(cl_int), (void *)&tilesX ));
1401 args.push_back( std::make_pair( sizeof(cl_int), (void *)&tilesY ));
1402 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.offset ));
1403 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.offset ));
1404 args.push_back( std::make_pair( sizeof(cl_int), (void *)&lut.offset ));
1406 size_t localThreads[3] = { 32, 8, 1 };
1407 size_t globalThreads[3] = { src.cols, src.rows, 1 };
1409 openCLExecuteKernel(Context::getContext(), &imgproc_clahe, "transform", globalThreads, localThreads, args, -1, -1);
1415 class CLAHE_Impl : public cv::CLAHE
1418 CLAHE_Impl(double clipLimit = 40.0, int tilesX = 8, int tilesY = 8);
1420 cv::AlgorithmInfo* info() const;
1422 void apply(cv::InputArray src, cv::OutputArray dst);
1424 void setClipLimit(double clipLimit);
1425 double getClipLimit() const;
1427 void setTilesGridSize(cv::Size tileGridSize);
1428 cv::Size getTilesGridSize() const;
1430 void collectGarbage();
1441 CLAHE_Impl::CLAHE_Impl(double clipLimit, int tilesX, int tilesY) :
1442 clipLimit_(clipLimit), tilesX_(tilesX), tilesY_(tilesY)
1446 CV_INIT_ALGORITHM(CLAHE_Impl, "CLAHE_OCL",
1447 obj.info()->addParam(obj, "clipLimit", obj.clipLimit_);
1448 obj.info()->addParam(obj, "tilesX", obj.tilesX_);
1449 obj.info()->addParam(obj, "tilesY", obj.tilesY_))
1451 void CLAHE_Impl::apply(cv::InputArray src_raw, cv::OutputArray dst_raw)
1453 oclMat& src = getOclMatRef(src_raw);
1454 oclMat& dst = getOclMatRef(dst_raw);
1455 CV_Assert( src.type() == CV_8UC1 );
1457 dst.create( src.size(), src.type() );
1459 const int histSize = 256;
1461 ensureSizeIsEnough(tilesX_ * tilesY_, histSize, CV_8UC1, lut_);
1466 if (src.cols % tilesX_ == 0 && src.rows % tilesY_ == 0)
1468 tileSize = cv::Size(src.cols / tilesX_, src.rows / tilesY_);
1473 ocl::copyMakeBorder(src, srcExt_, 0, tilesY_ - (src.rows % tilesY_), 0,
1474 tilesX_ - (src.cols % tilesX_), BORDER_REFLECT_101, Scalar::all(0));
1476 tileSize = Size(srcExt_.cols / tilesX_, srcExt_.rows / tilesY_);
1477 srcForLut = srcExt_;
1480 const int tileSizeTotal = tileSize.area();
1481 const float lutScale = static_cast<float>(histSize - 1) / tileSizeTotal;
1484 if (clipLimit_ > 0.0)
1486 clipLimit = static_cast<int>(clipLimit_ * tileSizeTotal / histSize);
1487 clipLimit = std::max(clipLimit, 1);
1490 clahe::calcLut(srcForLut, lut_, tilesX_, tilesY_, tileSize, clipLimit, lutScale);
1491 clahe::transform(src, dst, lut_, tilesX_, tilesY_, tileSize);
1494 void CLAHE_Impl::setClipLimit(double clipLimit)
1496 clipLimit_ = clipLimit;
1499 double CLAHE_Impl::getClipLimit() const
1504 void CLAHE_Impl::setTilesGridSize(cv::Size tileGridSize)
1506 tilesX_ = tileGridSize.width;
1507 tilesY_ = tileGridSize.height;
1510 cv::Size CLAHE_Impl::getTilesGridSize() const
1512 return cv::Size(tilesX_, tilesY_);
1515 void CLAHE_Impl::collectGarbage()
1522 cv::Ptr<cv::CLAHE> createCLAHE(double clipLimit, cv::Size tileGridSize)
1524 return makePtr<CLAHE_Impl>(clipLimit, tileGridSize.width, tileGridSize.height);
1527 //////////////////////////////////bilateralFilter////////////////////////////////////////////////////
1529 static void oclbilateralFilter_8u( const oclMat &src, oclMat &dst, int d,
1530 double sigma_color, double sigma_space,
1533 int cn = src.channels();
1534 int i, j, maxk, radius;
1536 CV_Assert( (src.channels() == 1 || src.channels() == 3) &&
1537 src.type() == dst.type() && src.size() == dst.size() &&
1538 src.data != dst.data );
1540 if ( sigma_color <= 0 )
1542 if ( sigma_space <= 0 )
1545 double gauss_color_coeff = -0.5 / (sigma_color * sigma_color);
1546 double gauss_space_coeff = -0.5 / (sigma_space * sigma_space);
1549 radius = cvRound(sigma_space * 1.5);
1552 radius = MAX(radius, 1);
1556 copyMakeBorder( src, temp, radius, radius, radius, radius, borderType );
1558 std::vector<float> _color_weight(cn * 256);
1559 std::vector<float> _space_weight(d * d);
1560 std::vector<int> _space_ofs(d * d);
1561 float *color_weight = &_color_weight[0];
1562 float *space_weight = &_space_weight[0];
1563 int *space_ofs = &_space_ofs[0];
1565 int dst_step_in_pixel = dst.step / dst.elemSize();
1566 int dst_offset_in_pixel = dst.offset / dst.elemSize();
1567 int temp_step_in_pixel = temp.step / temp.elemSize();
1569 // initialize color-related bilateral filter coefficients
1570 for( i = 0; i < 256 * cn; i++ )
1571 color_weight[i] = (float)std::exp(i * i * gauss_color_coeff);
1573 // initialize space-related bilateral filter coefficients
1574 for( i = -radius, maxk = 0; i <= radius; i++ )
1575 for( j = -radius; j <= radius; j++ )
1577 double r = std::sqrt((double)i * i + (double)j * j);
1580 space_weight[maxk] = (float)std::exp(r * r * gauss_space_coeff);
1581 space_ofs[maxk++] = (int)(i * temp_step_in_pixel + j);
1584 oclMat oclcolor_weight(1, cn * 256, CV_32FC1, color_weight);
1585 oclMat oclspace_weight(1, d * d, CV_32FC1, space_weight);
1586 oclMat oclspace_ofs(1, d * d, CV_32SC1, space_ofs);
1588 String kernelName = "bilateral";
1589 size_t localThreads[3] = { 16, 16, 1 };
1590 size_t globalThreads[3] = { dst.cols, dst.rows, 1 };
1592 if ((dst.type() == CV_8UC1) && ((dst.offset & 3) == 0) && ((dst.cols & 3) == 0))
1594 kernelName = "bilateral2";
1595 globalThreads[0] = dst.cols >> 2;
1598 std::vector<std::pair<size_t , const void *> > args;
1599 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst.data ));
1600 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&temp.data ));
1601 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.rows ));
1602 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.cols ));
1603 args.push_back( std::make_pair( sizeof(cl_int), (void *)&maxk ));
1604 args.push_back( std::make_pair( sizeof(cl_int), (void *)&radius ));
1605 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst_step_in_pixel ));
1606 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst_offset_in_pixel ));
1607 args.push_back( std::make_pair( sizeof(cl_int), (void *)&temp_step_in_pixel ));
1608 args.push_back( std::make_pair( sizeof(cl_int), (void *)&temp.rows ));
1609 args.push_back( std::make_pair( sizeof(cl_int), (void *)&temp.cols ));
1610 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&oclcolor_weight.data ));
1611 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&oclspace_weight.data ));
1612 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&oclspace_ofs.data ));
1614 openCLExecuteKernel(src.clCxt, &imgproc_bilateral, kernelName, globalThreads, localThreads, args, dst.oclchannels(), dst.depth());
1617 void bilateralFilter(const oclMat &src, oclMat &dst, int radius, double sigmaclr, double sigmaspc, int borderType)
1619 dst.create( src.size(), src.type() );
1620 if ( src.depth() == CV_8U )
1621 oclbilateralFilter_8u( src, dst, radius, sigmaclr, sigmaspc, borderType );
1623 CV_Error(Error::StsUnsupportedFormat, "Bilateral filtering is only implemented for CV_8U images");
1628 //////////////////////////////////mulSpectrums////////////////////////////////////////////////////
1629 void cv::ocl::mulSpectrums(const oclMat &a, const oclMat &b, oclMat &c, int /*flags*/, float scale, bool conjB)
1631 CV_Assert(a.type() == CV_32FC2);
1632 CV_Assert(b.type() == CV_32FC2);
1634 c.create(a.size(), CV_32FC2);
1636 size_t lt[3] = { 16, 16, 1 };
1637 size_t gt[3] = { a.cols, a.rows, 1 };
1639 String kernelName = conjB ? "mulAndScaleSpectrumsKernel_CONJ":"mulAndScaleSpectrumsKernel";
1641 std::vector<std::pair<size_t , const void *> > args;
1642 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&a.data ));
1643 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&b.data ));
1644 args.push_back( std::make_pair( sizeof(cl_float), (void *)&scale));
1645 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&c.data ));
1646 args.push_back( std::make_pair( sizeof(cl_int), (void *)&a.cols ));
1647 args.push_back( std::make_pair( sizeof(cl_int), (void *)&a.rows));
1648 args.push_back( std::make_pair( sizeof(cl_int), (void *)&a.step ));
1650 Context *clCxt = Context::getContext();
1651 openCLExecuteKernel(clCxt, &imgproc_mulAndScaleSpectrums, kernelName, gt, lt, args, -1, -1);
1653 //////////////////////////////////convolve////////////////////////////////////////////////////
1654 // ported from CUDA module
1655 void cv::ocl::ConvolveBuf::create(Size image_size, Size templ_size)
1657 result_size = Size(image_size.width - templ_size.width + 1,
1658 image_size.height - templ_size.height + 1);
1660 block_size = user_block_size;
1661 if (user_block_size.width == 0 || user_block_size.height == 0)
1662 block_size = estimateBlockSize(result_size, templ_size);
1664 dft_size.width = 1 << int(ceil(std::log(block_size.width + templ_size.width - 1.) / std::log(2.)));
1665 dft_size.height = 1 << int(ceil(std::log(block_size.height + templ_size.height - 1.) / std::log(2.)));
1667 // CUFFT has hard-coded kernels for power-of-2 sizes (up to 8192),
1668 // see CUDA Toolkit 4.1 CUFFT Library Programming Guide
1669 //if (dft_size.width > 8192)
1670 dft_size.width = getOptimalDFTSize(block_size.width + templ_size.width - 1.);
1671 //if (dft_size.height > 8192)
1672 dft_size.height = getOptimalDFTSize(block_size.height + templ_size.height - 1.);
1674 // To avoid wasting time doing small DFTs
1675 dft_size.width = std::max(dft_size.width, 512);
1676 dft_size.height = std::max(dft_size.height, 512);
1678 image_block.create(dft_size, CV_32F);
1679 templ_block.create(dft_size, CV_32F);
1680 result_data.create(dft_size, CV_32F);
1682 //spect_len = dft_size.height * (dft_size.width / 2 + 1);
1683 image_spect.create(dft_size.height, dft_size.width / 2 + 1, CV_32FC2);
1684 templ_spect.create(dft_size.height, dft_size.width / 2 + 1, CV_32FC2);
1685 result_spect.create(dft_size.height, dft_size.width / 2 + 1, CV_32FC2);
1687 // Use maximum result matrix block size for the estimated DFT block size
1688 block_size.width = std::min(dft_size.width - templ_size.width + 1, result_size.width);
1689 block_size.height = std::min(dft_size.height - templ_size.height + 1, result_size.height);
1692 Size cv::ocl::ConvolveBuf::estimateBlockSize(Size result_size, Size /*templ_size*/)
1694 int width = (result_size.width + 2) / 3;
1695 int height = (result_size.height + 2) / 3;
1696 width = std::min(width, result_size.width);
1697 height = std::min(height, result_size.height);
1698 return Size(width, height);
1701 static void convolve_run_fft(const oclMat &image, const oclMat &templ, oclMat &result, bool ccorr, ConvolveBuf& buf)
1703 #if defined HAVE_CLAMDFFT
1704 CV_Assert(image.type() == CV_32F);
1705 CV_Assert(templ.type() == CV_32F);
1707 buf.create(image.size(), templ.size());
1708 result.create(buf.result_size, CV_32F);
1710 Size& block_size = buf.block_size;
1711 Size& dft_size = buf.dft_size;
1713 oclMat& image_block = buf.image_block;
1714 oclMat& templ_block = buf.templ_block;
1715 oclMat& result_data = buf.result_data;
1717 oclMat& image_spect = buf.image_spect;
1718 oclMat& templ_spect = buf.templ_spect;
1719 oclMat& result_spect = buf.result_spect;
1721 oclMat templ_roi = templ;
1722 copyMakeBorder(templ_roi, templ_block, 0, templ_block.rows - templ_roi.rows, 0,
1723 templ_block.cols - templ_roi.cols, 0, Scalar());
1725 cv::ocl::dft(templ_block, templ_spect, dft_size);
1727 // Process all blocks of the result matrix
1728 for (int y = 0; y < result.rows; y += block_size.height)
1730 for (int x = 0; x < result.cols; x += block_size.width)
1732 Size image_roi_size(std::min(x + dft_size.width, image.cols) - x,
1733 std::min(y + dft_size.height, image.rows) - y);
1734 Rect roi0(x, y, image_roi_size.width, image_roi_size.height);
1736 oclMat image_roi(image, roi0);
1738 copyMakeBorder(image_roi, image_block, 0, image_block.rows - image_roi.rows,
1739 0, image_block.cols - image_roi.cols, 0, Scalar());
1741 cv::ocl::dft(image_block, image_spect, dft_size);
1743 mulSpectrums(image_spect, templ_spect, result_spect, 0,
1744 1.f / dft_size.area(), ccorr);
1746 cv::ocl::dft(result_spect, result_data, dft_size, cv::DFT_INVERSE | cv::DFT_REAL_OUTPUT);
1748 Size result_roi_size(std::min(x + block_size.width, result.cols) - x,
1749 std::min(y + block_size.height, result.rows) - y);
1751 Rect roi1(x, y, result_roi_size.width, result_roi_size.height);
1752 Rect roi2(0, 0, result_roi_size.width, result_roi_size.height);
1754 oclMat result_roi(result, roi1);
1755 oclMat result_block(result_data, roi2);
1757 result_block.copyTo(result_roi);
1762 CV_Error(Error::OpenCLNoAMDBlasFft, "OpenCL DFT is not implemented");
1763 #define UNUSED(x) (void)(x);
1764 UNUSED(image) UNUSED(templ) UNUSED(result) UNUSED(ccorr) UNUSED(buf)
1769 static void convolve_run(const oclMat &src, const oclMat &temp1, oclMat &dst, String kernelName, const cv::ocl::ProgramEntry* source)
1771 CV_Assert(src.depth() == CV_32FC1);
1772 CV_Assert(temp1.depth() == CV_32F);
1773 CV_Assert(temp1.cols <= 17 && temp1.rows <= 17);
1775 dst.create(src.size(), src.type());
1777 CV_Assert(src.cols == dst.cols && src.rows == dst.rows);
1778 CV_Assert(src.type() == dst.type());
1780 size_t localThreads[3] = { 16, 16, 1 };
1781 size_t globalThreads[3] = { dst.cols, dst.rows, 1 };
1783 int src_step = src.step / src.elemSize(), src_offset = src.offset / src.elemSize();
1784 int dst_step = dst.step / dst.elemSize(), dst_offset = dst.offset / dst.elemSize();
1785 int temp1_step = temp1.step / temp1.elemSize(), temp1_offset = temp1.offset / temp1.elemSize();
1787 std::vector<std::pair<size_t , const void *> > args;
1788 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&src.data ));
1789 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&temp1.data ));
1790 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst.data ));
1791 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.rows ));
1792 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.cols ));
1793 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src_step ));
1794 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst_step ));
1795 args.push_back( std::make_pair( sizeof(cl_int), (void *)&temp1_step ));
1796 args.push_back( std::make_pair( sizeof(cl_int), (void *)&temp1.rows ));
1797 args.push_back( std::make_pair( sizeof(cl_int), (void *)&temp1.cols ));
1798 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src_offset ));
1799 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst_offset ));
1800 args.push_back( std::make_pair( sizeof(cl_int), (void *)&temp1_offset ));
1802 openCLExecuteKernel(src.clCxt, source, kernelName, globalThreads, localThreads, args, -1, dst.depth());
1805 void cv::ocl::convolve(const oclMat &x, const oclMat &t, oclMat &y, bool ccorr)
1807 CV_Assert(x.depth() == CV_32F);
1808 CV_Assert(t.depth() == CV_32F);
1809 y.create(x.size(), x.type());
1810 String kernelName = "convolve";
1811 if(t.cols > 17 || t.rows > 17)
1814 convolve_run_fft(x, t, y, ccorr, buf);
1818 CV_Assert(ccorr == false);
1819 convolve_run(x, t, y, kernelName, &imgproc_convolve);
1822 void cv::ocl::convolve(const oclMat &image, const oclMat &templ, oclMat &result, bool ccorr, ConvolveBuf& buf)
1824 result.create(image.size(), image.type());
1825 convolve_run_fft(image, templ, result, ccorr, buf);