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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Copyright (C) 2014, Itseez, Inc, all rights reserved.
15 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
27 // * The name of Intel Corporation may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
43 #include "precomp.hpp"
44 #include "opencl_kernels_imgproc.hpp"
46 #include "opencv2/core/openvx/ovx_defs.hpp"
49 /****************************************************************************************\
50 Sobel & Scharr Derivative Filters
51 \****************************************************************************************/
56 static void getScharrKernels( OutputArray _kx, OutputArray _ky,
57 int dx, int dy, bool normalize, int ktype )
61 CV_Assert( ktype == CV_32F || ktype == CV_64F );
62 _kx.create(ksize, 1, ktype, -1, true);
63 _ky.create(ksize, 1, ktype, -1, true);
64 Mat kx = _kx.getMat();
65 Mat ky = _ky.getMat();
67 CV_Assert( dx >= 0 && dy >= 0 && dx+dy == 1 );
69 for( int k = 0; k < 2; k++ )
71 Mat* kernel = k == 0 ? &kx : &ky;
72 int order = k == 0 ? dx : dy;
76 kerI[0] = 3, kerI[1] = 10, kerI[2] = 3;
78 kerI[0] = -1, kerI[1] = 0, kerI[2] = 1;
80 Mat temp(kernel->rows, kernel->cols, CV_32S, &kerI[0]);
81 double scale = !normalize || order == 1 ? 1. : 1./32;
82 temp.convertTo(*kernel, ktype, scale);
87 static void getSobelKernels( OutputArray _kx, OutputArray _ky,
88 int dx, int dy, int _ksize, bool normalize, int ktype )
90 int i, j, ksizeX = _ksize, ksizeY = _ksize;
91 if( ksizeX == 1 && dx > 0 )
93 if( ksizeY == 1 && dy > 0 )
96 CV_Assert( ktype == CV_32F || ktype == CV_64F );
98 _kx.create(ksizeX, 1, ktype, -1, true);
99 _ky.create(ksizeY, 1, ktype, -1, true);
100 Mat kx = _kx.getMat();
101 Mat ky = _ky.getMat();
103 if( _ksize % 2 == 0 || _ksize > 31 )
104 CV_Error( CV_StsOutOfRange, "The kernel size must be odd and not larger than 31" );
105 std::vector<int> kerI(std::max(ksizeX, ksizeY) + 1);
107 CV_Assert( dx >= 0 && dy >= 0 && dx+dy > 0 );
109 for( int k = 0; k < 2; k++ )
111 Mat* kernel = k == 0 ? &kx : &ky;
112 int order = k == 0 ? dx : dy;
113 int ksize = k == 0 ? ksizeX : ksizeY;
115 CV_Assert( ksize > order );
119 else if( ksize == 3 )
122 kerI[0] = 1, kerI[1] = 2, kerI[2] = 1;
123 else if( order == 1 )
124 kerI[0] = -1, kerI[1] = 0, kerI[2] = 1;
126 kerI[0] = 1, kerI[1] = -2, kerI[2] = 1;
132 for( i = 0; i < ksize; i++ )
135 for( i = 0; i < ksize - order - 1; i++ )
138 for( j = 1; j <= ksize; j++ )
140 newval = kerI[j]+kerI[j-1];
146 for( i = 0; i < order; i++ )
149 for( j = 1; j <= ksize; j++ )
151 newval = kerI[j-1] - kerI[j];
158 Mat temp(kernel->rows, kernel->cols, CV_32S, &kerI[0]);
159 double scale = !normalize ? 1. : 1./(1 << (ksize-order-1));
160 temp.convertTo(*kernel, ktype, scale);
166 void cv::getDerivKernels( OutputArray kx, OutputArray ky, int dx, int dy,
167 int ksize, bool normalize, int ktype )
170 getScharrKernels( kx, ky, dx, dy, normalize, ktype );
172 getSobelKernels( kx, ky, dx, dy, ksize, normalize, ktype );
176 cv::Ptr<cv::FilterEngine> cv::createDerivFilter(int srcType, int dstType,
177 int dx, int dy, int ksize, int borderType )
180 getDerivKernels( kx, ky, dx, dy, ksize, false, CV_32F );
181 return createSeparableLinearFilter(srcType, dstType,
182 kx, ky, Point(-1,-1), 0, borderType );
189 template <> inline bool skipSmallImages<VX_KERNEL_SOBEL_3x3>(int w, int h) { return w*h < 320 * 240; }
191 static bool openvx_sobel(InputArray _src, OutputArray _dst,
192 int dx, int dy, int ksize,
193 double scale, double delta, int borderType)
195 if (_src.type() != CV_8UC1 || _dst.type() != CV_16SC1 ||
196 ksize != 3 || scale != 1.0 || delta != 0.0 ||
197 (dx | dy) != 1 || (dx + dy) != 1 ||
198 _src.cols() < ksize || _src.rows() < ksize ||
199 ovx::skipSmallImages<VX_KERNEL_SOBEL_3x3>(_src.cols(), _src.rows())
203 Mat src = _src.getMat();
204 Mat dst = _dst.getMat();
206 if ((borderType & BORDER_ISOLATED) == 0 && src.isSubmatrix())
207 return false; //Process isolated borders only
209 switch (borderType & ~BORDER_ISOLATED)
211 case BORDER_CONSTANT:
212 border = VX_BORDER_CONSTANT;
214 case BORDER_REPLICATE:
215 // border = VX_BORDER_REPLICATE;
223 ivx::Context ctx = ovx::getOpenVXContext();
224 //if ((vx_size)ksize > ctx.convolutionMaxDimension())
228 if (dst.data != src.data)
234 ia = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
235 ivx::Image::createAddressing(a.cols, a.rows, 1, (vx_int32)(a.step)), a.data),
236 ib = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_S16,
237 ivx::Image::createAddressing(dst.cols, dst.rows, 2, (vx_int32)(dst.step)), dst.data);
239 //ATTENTION: VX_CONTEXT_IMMEDIATE_BORDER attribute change could lead to strange issues in multi-threaded environments
240 //since OpenVX standard says nothing about thread-safety for now
241 ivx::border_t prevBorder = ctx.immediateBorder();
242 ctx.setImmediateBorder(border, (vx_uint8)(0));
244 ivx::IVX_CHECK_STATUS(vxuSobel3x3(ctx, ia, ib, NULL));
246 ivx::IVX_CHECK_STATUS(vxuSobel3x3(ctx, ia, NULL, ib));
247 ctx.setImmediateBorder(prevBorder);
249 catch (ivx::RuntimeError & e)
251 VX_DbgThrow(e.what());
253 catch (ivx::WrapperError & e)
255 VX_DbgThrow(e.what());
267 static bool ipp_Deriv(InputArray _src, OutputArray _dst, int dx, int dy, int ksize, double scale, double delta, int borderType)
270 CV_INSTRUMENT_REGION_IPP()
272 ::ipp::IwiSize size(_src.size().width, _src.size().height);
273 IppDataType srcType = ippiGetDataType(_src.depth());
274 IppDataType dstType = ippiGetDataType(_dst.depth());
275 int channels = _src.channels();
276 bool useScale = false;
277 bool useScharr = false;
279 if(channels != _dst.channels() || channels > 1)
282 if(fabs(delta) > FLT_EPSILON || fabs(scale-1) > FLT_EPSILON)
291 IppiMaskSize maskSize = ippiGetMaskSize(ksize, ksize);
292 if((int)maskSize < 0)
295 #if IPP_VERSION_X100 <= 201703
296 // Bug with mirror wrap
297 if(borderType == BORDER_REFLECT_101 && (ksize/2+1 > size.width || ksize/2+1 > size.height))
301 IwiDerivativeType derivType = ippiGetDerivType(dx, dy, (useScharr)?false:true);
302 if((int)derivType < 0)
305 // Acquire data and begin processing
308 Mat src = _src.getMat();
309 Mat dst = _dst.getMat();
310 ::ipp::IwiImage iwSrc = ippiGetImage(src);
311 ::ipp::IwiImage iwDst = ippiGetImage(dst);
312 ::ipp::IwiImage iwSrcProc = iwSrc;
313 ::ipp::IwiImage iwDstProc = iwDst;
314 ::ipp::IwiBorderSize borderSize(maskSize);
315 ::ipp::IwiBorderType ippBorder(ippiGetBorder(iwSrc, borderType, borderSize));
319 if(srcType == ipp8u && dstType == ipp8u)
321 iwDstProc.Alloc(iwDst.m_size, ipp16s, channels);
324 else if(srcType == ipp8u && dstType == ipp32f)
327 iwSrcProc.Alloc(iwSrc.m_size, ipp32f, channels);
328 CV_INSTRUMENT_FUN_IPP(::ipp::iwiScale, iwSrc, iwSrcProc, 1, 0, ::ipp::IwiScaleParams(ippAlgHintFast));
329 iwSrcProc += borderSize;
333 CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterScharr, iwSrcProc, iwDstProc, derivType, maskSize, ::ipp::IwDefault(), ippBorder);
335 CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterSobel, iwSrcProc, iwDstProc, derivType, maskSize, ::ipp::IwDefault(), ippBorder);
338 CV_INSTRUMENT_FUN_IPP(::ipp::iwiScale, iwDstProc, iwDst, scale, delta, ::ipp::IwiScaleParams(ippAlgHintFast));
340 catch (::ipp::IwException)
347 CV_UNUSED(_src); CV_UNUSED(_dst); CV_UNUSED(dx); CV_UNUSED(dy); CV_UNUSED(ksize); CV_UNUSED(scale); CV_UNUSED(delta); CV_UNUSED(borderType);
357 static bool ocl_sepFilter3x3_8UC1(InputArray _src, OutputArray _dst, int ddepth,
358 InputArray _kernelX, InputArray _kernelY, double delta, int borderType)
360 const ocl::Device & dev = ocl::Device::getDefault();
361 int type = _src.type(), sdepth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
363 if ( !(dev.isIntel() && (type == CV_8UC1) && (ddepth == CV_8U) &&
364 (_src.offset() == 0) && (_src.step() % 4 == 0) &&
365 (_src.cols() % 16 == 0) && (_src.rows() % 2 == 0)) )
368 Mat kernelX = _kernelX.getMat().reshape(1, 1);
369 if (kernelX.cols % 2 != 1)
371 Mat kernelY = _kernelY.getMat().reshape(1, 1);
372 if (kernelY.cols % 2 != 1)
378 Size size = _src.size();
379 size_t globalsize[2] = { 0, 0 };
380 size_t localsize[2] = { 0, 0 };
382 globalsize[0] = size.width / 16;
383 globalsize[1] = size.height / 2;
385 const char * const borderMap[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", 0, "BORDER_REFLECT_101" };
386 char build_opts[1024];
387 sprintf(build_opts, "-D %s %s%s", borderMap[borderType],
388 ocl::kernelToStr(kernelX, CV_32F, "KERNEL_MATRIX_X").c_str(),
389 ocl::kernelToStr(kernelY, CV_32F, "KERNEL_MATRIX_Y").c_str());
391 ocl::Kernel kernel("sepFilter3x3_8UC1_cols16_rows2", cv::ocl::imgproc::sepFilter3x3_oclsrc, build_opts);
395 UMat src = _src.getUMat();
396 _dst.create(size, CV_MAKETYPE(ddepth, cn));
397 if (!(_dst.offset() == 0 && _dst.step() % 4 == 0))
399 UMat dst = _dst.getUMat();
401 int idxArg = kernel.set(0, ocl::KernelArg::PtrReadOnly(src));
402 idxArg = kernel.set(idxArg, (int)src.step);
403 idxArg = kernel.set(idxArg, ocl::KernelArg::PtrWriteOnly(dst));
404 idxArg = kernel.set(idxArg, (int)dst.step);
405 idxArg = kernel.set(idxArg, (int)dst.rows);
406 idxArg = kernel.set(idxArg, (int)dst.cols);
407 idxArg = kernel.set(idxArg, static_cast<float>(delta));
409 return kernel.run(2, globalsize, (localsize[0] == 0) ? NULL : localsize, false);
414 void cv::Sobel( InputArray _src, OutputArray _dst, int ddepth, int dx, int dy,
415 int ksize, double scale, double delta, int borderType )
417 CV_INSTRUMENT_REGION()
419 int stype = _src.type(), sdepth = CV_MAT_DEPTH(stype), cn = CV_MAT_CN(stype);
422 int dtype = CV_MAKE_TYPE(ddepth, cn);
423 _dst.create( _src.size(), dtype );
425 int ktype = std::max(CV_32F, std::max(ddepth, sdepth));
428 getDerivKernels( kx, ky, dx, dy, ksize, false, ktype );
431 // usually the smoothing part is the slowest to compute,
432 // so try to scale it instead of the faster differentiating part
439 CV_OCL_RUN(ocl::isOpenCLActivated() && _dst.isUMat() && _src.dims() <= 2 && ksize == 3 &&
440 (size_t)_src.rows() > ky.total() && (size_t)_src.cols() > kx.total(),
441 ocl_sepFilter3x3_8UC1(_src, _dst, ddepth, kx, ky, delta, borderType));
443 CV_OCL_RUN(ocl::isOpenCLActivated() && _dst.isUMat() && _src.dims() <= 2 && (size_t)_src.rows() > kx.total() && (size_t)_src.cols() > kx.total(),
444 ocl_sepFilter2D(_src, _dst, ddepth, kx, ky, Point(-1, -1), 0, borderType))
446 Mat src = _src.getMat();
447 Mat dst = _dst.getMat();
450 Size wsz(src.cols, src.rows);
451 if(!(borderType & BORDER_ISOLATED))
452 src.locateROI( wsz, ofs );
454 CALL_HAL(sobel, cv_hal_sobel, src.ptr(), src.step, dst.ptr(), dst.step, src.cols, src.rows, sdepth, ddepth, cn,
455 ofs.x, ofs.y, wsz.width - src.cols - ofs.x, wsz.height - src.rows - ofs.y, dx, dy, ksize, scale, delta, borderType&~BORDER_ISOLATED);
458 openvx_sobel(src, dst, dx, dy, ksize, scale, delta, borderType))
460 CV_IPP_RUN_FAST(ipp_Deriv(src, dst, dx, dy, ksize, scale, delta, borderType));
462 sepFilter2D(src, dst, ddepth, kx, ky, Point(-1, -1), delta, borderType );
466 void cv::Scharr( InputArray _src, OutputArray _dst, int ddepth, int dx, int dy,
467 double scale, double delta, int borderType )
469 CV_INSTRUMENT_REGION()
471 int stype = _src.type(), sdepth = CV_MAT_DEPTH(stype), cn = CV_MAT_CN(stype);
474 int dtype = CV_MAKETYPE(ddepth, cn);
475 _dst.create( _src.size(), dtype );
477 int ktype = std::max(CV_32F, std::max(ddepth, sdepth));
480 getScharrKernels( kx, ky, dx, dy, false, ktype );
483 // usually the smoothing part is the slowest to compute,
484 // so try to scale it instead of the faster differentiating part
491 CV_OCL_RUN(ocl::isOpenCLActivated() && _dst.isUMat() && _src.dims() <= 2 &&
492 (size_t)_src.rows() > ky.total() && (size_t)_src.cols() > kx.total(),
493 ocl_sepFilter3x3_8UC1(_src, _dst, ddepth, kx, ky, delta, borderType));
495 CV_OCL_RUN(ocl::isOpenCLActivated() && _dst.isUMat() && _src.dims() <= 2 &&
496 (size_t)_src.rows() > kx.total() && (size_t)_src.cols() > kx.total(),
497 ocl_sepFilter2D(_src, _dst, ddepth, kx, ky, Point(-1, -1), 0, borderType))
499 Mat src = _src.getMat();
500 Mat dst = _dst.getMat();
503 Size wsz(src.cols, src.rows);
504 if(!(borderType & BORDER_ISOLATED))
505 src.locateROI( wsz, ofs );
507 CALL_HAL(scharr, cv_hal_scharr, src.ptr(), src.step, dst.ptr(), dst.step, src.cols, src.rows, sdepth, ddepth, cn,
508 ofs.x, ofs.y, wsz.width - src.cols - ofs.x, wsz.height - src.rows - ofs.y, dx, dy, scale, delta, borderType&~BORDER_ISOLATED);
510 CV_IPP_RUN_FAST(ipp_Deriv(src, dst, dx, dy, 0, scale, delta, borderType));
512 sepFilter2D( src, dst, ddepth, kx, ky, Point(-1, -1), delta, borderType );
519 #define LAPLACIAN_LOCAL_MEM(tileX, tileY, ksize, elsize) (((tileX) + 2 * (int)((ksize) / 2)) * (3 * (tileY) + 2 * (int)((ksize) / 2)) * elsize)
521 static bool ocl_Laplacian5(InputArray _src, OutputArray _dst,
522 const Mat & kd, const Mat & ks, double scale, double delta,
523 int borderType, int depth, int ddepth)
525 const size_t tileSizeX = 16;
526 const size_t tileSizeYmin = 8;
528 const ocl::Device dev = ocl::Device::getDefault();
530 int stype = _src.type();
531 int sdepth = CV_MAT_DEPTH(stype), cn = CV_MAT_CN(stype), esz = CV_ELEM_SIZE(stype);
533 bool doubleSupport = dev.doubleFPConfig() > 0;
534 if (!doubleSupport && (sdepth == CV_64F || ddepth == CV_64F))
537 Mat kernelX = kd.reshape(1, 1);
538 if (kernelX.cols % 2 != 1)
540 Mat kernelY = ks.reshape(1, 1);
541 if (kernelY.cols % 2 != 1)
543 CV_Assert(kernelX.cols == kernelY.cols);
545 size_t wgs = dev.maxWorkGroupSize();
546 size_t lmsz = dev.localMemSize();
547 size_t src_step = _src.step(), src_offset = _src.offset();
548 const size_t tileSizeYmax = wgs / tileSizeX;
549 CV_Assert(src_step != 0 && esz != 0);
551 // workaround for NVIDIA: 3 channel vector type takes 4*elem_size in local memory
552 int loc_mem_cn = dev.vendorID() == ocl::Device::VENDOR_NVIDIA && cn == 3 ? 4 : cn;
553 if (((src_offset % src_step) % esz == 0) &&
555 (borderType == BORDER_CONSTANT || borderType == BORDER_REPLICATE) ||
556 ((borderType == BORDER_REFLECT || borderType == BORDER_WRAP || borderType == BORDER_REFLECT_101) &&
557 (_src.cols() >= (int) (kernelX.cols + tileSizeX) && _src.rows() >= (int) (kernelY.cols + tileSizeYmax)))
559 (tileSizeX * tileSizeYmin <= wgs) &&
560 (LAPLACIAN_LOCAL_MEM(tileSizeX, tileSizeYmin, kernelX.cols, loc_mem_cn * 4) <= lmsz)
561 && OCL_PERFORMANCE_CHECK(!dev.isAMD()) // TODO FIXIT 2018: Problem with AMDGPU on Linux (2482.3)
564 Size size = _src.size(), wholeSize;
566 int dtype = CV_MAKE_TYPE(ddepth, cn);
569 size_t tileSizeY = tileSizeYmax;
570 while ((tileSizeX * tileSizeY > wgs) || (LAPLACIAN_LOCAL_MEM(tileSizeX, tileSizeY, kernelX.cols, loc_mem_cn * 4) > lmsz))
574 size_t lt2[2] = { tileSizeX, tileSizeY};
575 size_t gt2[2] = { lt2[0] * (1 + (size.width - 1) / lt2[0]), lt2[1] };
578 const char * const borderMap[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", "BORDER_WRAP",
579 "BORDER_REFLECT_101" };
581 String opts = cv::format("-D BLK_X=%d -D BLK_Y=%d -D RADIUS=%d%s%s"
582 " -D convertToWT=%s -D convertToDT=%s"
583 " -D %s -D srcT1=%s -D dstT1=%s -D WT1=%s"
584 " -D srcT=%s -D dstT=%s -D WT=%s"
586 (int)lt2[0], (int)lt2[1], kernelX.cols / 2,
587 ocl::kernelToStr(kernelX, wdepth, "KERNEL_MATRIX_X").c_str(),
588 ocl::kernelToStr(kernelY, wdepth, "KERNEL_MATRIX_Y").c_str(),
589 ocl::convertTypeStr(sdepth, wdepth, cn, cvt[0]),
590 ocl::convertTypeStr(wdepth, ddepth, cn, cvt[1]),
591 borderMap[borderType],
592 ocl::typeToStr(sdepth), ocl::typeToStr(ddepth), ocl::typeToStr(wdepth),
593 ocl::typeToStr(CV_MAKETYPE(sdepth, cn)),
594 ocl::typeToStr(CV_MAKETYPE(ddepth, cn)),
595 ocl::typeToStr(CV_MAKETYPE(wdepth, cn)),
598 ocl::Kernel k("laplacian", ocl::imgproc::laplacian5_oclsrc, opts);
601 UMat src = _src.getUMat();
602 _dst.create(size, dtype);
603 UMat dst = _dst.getUMat();
605 int src_offset_x = static_cast<int>((src_offset % src_step) / esz);
606 int src_offset_y = static_cast<int>(src_offset / src_step);
608 src.locateROI(wholeSize, origin);
610 k.args(ocl::KernelArg::PtrReadOnly(src), (int)src_step, src_offset_x, src_offset_y,
611 wholeSize.height, wholeSize.width, ocl::KernelArg::WriteOnly(dst),
612 static_cast<float>(scale), static_cast<float>(delta));
614 return k.run(2, gt2, lt2, false);
616 int iscale = cvRound(scale), idelta = cvRound(delta);
617 bool floatCoeff = std::fabs(delta - idelta) > DBL_EPSILON || std::fabs(scale - iscale) > DBL_EPSILON;
618 int wdepth = std::max(depth, floatCoeff ? CV_32F : CV_32S), kercn = 1;
620 if (!doubleSupport && wdepth == CV_64F)
624 ocl::Kernel k("sumConvert", ocl::imgproc::laplacian5_oclsrc,
625 format("-D ONLY_SUM_CONVERT "
626 "-D srcT=%s -D WT=%s -D dstT=%s -D coeffT=%s -D wdepth=%d "
627 "-D convertToWT=%s -D convertToDT=%s%s",
628 ocl::typeToStr(CV_MAKE_TYPE(depth, kercn)),
629 ocl::typeToStr(CV_MAKE_TYPE(wdepth, kercn)),
630 ocl::typeToStr(CV_MAKE_TYPE(ddepth, kercn)),
631 ocl::typeToStr(wdepth), wdepth,
632 ocl::convertTypeStr(depth, wdepth, kercn, cvt[0]),
633 ocl::convertTypeStr(wdepth, ddepth, kercn, cvt[1]),
634 doubleSupport ? " -D DOUBLE_SUPPORT" : ""));
639 sepFilter2D(_src, d2x, depth, kd, ks, Point(-1, -1), 0, borderType);
640 sepFilter2D(_src, d2y, depth, ks, kd, Point(-1, -1), 0, borderType);
642 UMat dst = _dst.getUMat();
644 ocl::KernelArg d2xarg = ocl::KernelArg::ReadOnlyNoSize(d2x),
645 d2yarg = ocl::KernelArg::ReadOnlyNoSize(d2y),
646 dstarg = ocl::KernelArg::WriteOnly(dst, cn, kercn);
648 if (wdepth >= CV_32F)
649 k.args(d2xarg, d2yarg, dstarg, (float)scale, (float)delta);
651 k.args(d2xarg, d2yarg, dstarg, iscale, idelta);
653 size_t globalsize[] = { (size_t)dst.cols * cn / kercn, (size_t)dst.rows };
654 return k.run(2, globalsize, NULL, false);
657 static bool ocl_Laplacian3_8UC1(InputArray _src, OutputArray _dst, int ddepth,
658 InputArray _kernel, double delta, int borderType)
660 const ocl::Device & dev = ocl::Device::getDefault();
661 int type = _src.type(), sdepth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
663 if ( !(dev.isIntel() && (type == CV_8UC1) && (ddepth == CV_8U) &&
664 (borderType != BORDER_WRAP) &&
665 (_src.offset() == 0) && (_src.step() % 4 == 0) &&
666 (_src.cols() % 16 == 0) && (_src.rows() % 2 == 0)) )
669 Mat kernel = _kernel.getMat().reshape(1, 1);
674 Size size = _src.size();
675 size_t globalsize[2] = { 0, 0 };
676 size_t localsize[2] = { 0, 0 };
678 globalsize[0] = size.width / 16;
679 globalsize[1] = size.height / 2;
681 const char * const borderMap[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", 0, "BORDER_REFLECT_101" };
682 char build_opts[1024];
683 sprintf(build_opts, "-D %s %s", borderMap[borderType],
684 ocl::kernelToStr(kernel, CV_32F, "KERNEL_MATRIX").c_str());
686 ocl::Kernel k("laplacian3_8UC1_cols16_rows2", cv::ocl::imgproc::laplacian3_oclsrc, build_opts);
690 UMat src = _src.getUMat();
691 _dst.create(size, CV_MAKETYPE(ddepth, cn));
692 if (!(_dst.offset() == 0 && _dst.step() % 4 == 0))
694 UMat dst = _dst.getUMat();
696 int idxArg = k.set(0, ocl::KernelArg::PtrReadOnly(src));
697 idxArg = k.set(idxArg, (int)src.step);
698 idxArg = k.set(idxArg, ocl::KernelArg::PtrWriteOnly(dst));
699 idxArg = k.set(idxArg, (int)dst.step);
700 idxArg = k.set(idxArg, (int)dst.rows);
701 idxArg = k.set(idxArg, (int)dst.cols);
702 idxArg = k.set(idxArg, static_cast<float>(delta));
704 return k.run(2, globalsize, (localsize[0] == 0) ? NULL : localsize, false);
710 #if defined(HAVE_IPP)
714 static bool ipp_Laplacian(InputArray _src, OutputArray _dst, int ksize, double scale, double delta, int borderType)
717 CV_INSTRUMENT_REGION_IPP()
719 ::ipp::IwiSize size(_src.size().width, _src.size().height);
720 IppDataType srcType = ippiGetDataType(_src.depth());
721 IppDataType dstType = ippiGetDataType(_dst.depth());
722 int channels = _src.channels();
723 bool useScale = false;
725 if(channels != _dst.channels() || channels > 1)
728 if(fabs(delta) > FLT_EPSILON || fabs(scale-1) > FLT_EPSILON)
731 IppiMaskSize maskSize = ippiGetMaskSize(ksize, ksize);
732 if((int)maskSize < 0)
735 // Acquire data and begin processing
738 Mat src = _src.getMat();
739 Mat dst = _dst.getMat();
740 ::ipp::IwiImage iwSrc = ippiGetImage(src);
741 ::ipp::IwiImage iwDst = ippiGetImage(dst);
742 ::ipp::IwiImage iwSrcProc = iwSrc;
743 ::ipp::IwiImage iwDstProc = iwDst;
744 ::ipp::IwiBorderSize borderSize(maskSize);
745 ::ipp::IwiBorderType ippBorder(ippiGetBorder(iwSrc, borderType, borderSize));
749 if(srcType == ipp8u && dstType == ipp8u)
751 iwDstProc.Alloc(iwDst.m_size, ipp16s, channels);
754 else if(srcType == ipp8u && dstType == ipp32f)
757 iwSrcProc.Alloc(iwSrc.m_size, ipp32f, channels);
758 CV_INSTRUMENT_FUN_IPP(::ipp::iwiScale, iwSrc, iwSrcProc, 1, 0);
759 iwSrcProc += borderSize;
762 CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterLaplacian, iwSrcProc, iwDstProc, maskSize, ::ipp::IwDefault(), ippBorder);
765 CV_INSTRUMENT_FUN_IPP(::ipp::iwiScale, iwDstProc, iwDst, scale, delta);
768 catch (::ipp::IwException ex)
775 CV_UNUSED(_src); CV_UNUSED(_dst); CV_UNUSED(ksize); CV_UNUSED(scale); CV_UNUSED(delta); CV_UNUSED(borderType);
783 void cv::Laplacian( InputArray _src, OutputArray _dst, int ddepth, int ksize,
784 double scale, double delta, int borderType )
786 CV_INSTRUMENT_REGION()
788 int stype = _src.type(), sdepth = CV_MAT_DEPTH(stype), cn = CV_MAT_CN(stype);
791 _dst.create( _src.size(), CV_MAKETYPE(ddepth, cn) );
793 if( ksize == 1 || ksize == 3 )
797 { 0, 1, 0, 1, -4, 1, 0, 1, 0 },
798 { 2, 0, 2, 0, -8, 0, 2, 0, 2 }
801 Mat kernel(3, 3, CV_32F, K[ksize == 3]);
805 CV_OCL_RUN(_dst.isUMat() && _src.dims() <= 2,
806 ocl_Laplacian3_8UC1(_src, _dst, ddepth, kernel, delta, borderType));
809 CV_IPP_RUN(!(cv::ocl::isOpenCLActivated() && _dst.isUMat()), ipp_Laplacian(_src, _dst, ksize, scale, delta, borderType));
811 if( ksize == 1 || ksize == 3 )
815 { 0, 1, 0, 1, -4, 1, 0, 1, 0 },
816 { 2, 0, 2, 0, -8, 0, 2, 0, 2 }
818 Mat kernel(3, 3, CV_32F, K[ksize == 3]);
822 filter2D( _src, _dst, ddepth, kernel, Point(-1, -1), delta, borderType );
826 int ktype = std::max(CV_32F, std::max(ddepth, sdepth));
827 int wdepth = sdepth == CV_8U && ksize <= 5 ? CV_16S : sdepth <= CV_32F ? CV_32F : CV_64F;
828 int wtype = CV_MAKETYPE(wdepth, cn);
830 getSobelKernels( kd, ks, 2, 0, ksize, false, ktype );
832 CV_OCL_RUN(_dst.isUMat(),
833 ocl_Laplacian5(_src, _dst, kd, ks, scale,
834 delta, borderType, wdepth, ddepth))
836 Mat src = _src.getMat(), dst = _dst.getMat();
838 Size wsz(src.cols, src.rows);
839 if(!(borderType&BORDER_ISOLATED))
840 src.locateROI( wsz, ofs );
841 borderType = (borderType&~BORDER_ISOLATED);
843 const size_t STRIPE_SIZE = 1 << 14;
844 Ptr<FilterEngine> fx = createSeparableLinearFilter(stype,
845 wtype, kd, ks, Point(-1,-1), 0, borderType, borderType, Scalar() );
846 Ptr<FilterEngine> fy = createSeparableLinearFilter(stype,
847 wtype, ks, kd, Point(-1,-1), 0, borderType, borderType, Scalar() );
849 int y = fx->start(src, wsz, ofs), dsty = 0, dy = 0;
850 fy->start(src, wsz, ofs);
851 const uchar* sptr = src.ptr() + src.step[0] * y;
853 int dy0 = std::min(std::max((int)(STRIPE_SIZE/(CV_ELEM_SIZE(stype)*src.cols)), 1), src.rows);
854 Mat d2x( dy0 + kd.rows - 1, src.cols, wtype );
855 Mat d2y( dy0 + kd.rows - 1, src.cols, wtype );
857 for( ; dsty < src.rows; sptr += dy0*src.step, dsty += dy )
859 fx->proceed( sptr, (int)src.step, dy0, d2x.ptr(), (int)d2x.step );
860 dy = fy->proceed( sptr, (int)src.step, dy0, d2y.ptr(), (int)d2y.step );
863 Mat dstripe = dst.rowRange(dsty, dsty + dy);
864 d2x.rows = d2y.rows = dy; // modify the headers, which should work
866 d2x.convertTo( dstripe, ddepth, scale, delta );
872 /////////////////////////////////////////////////////////////////////////////////////////
875 cvSobel( const void* srcarr, void* dstarr, int dx, int dy, int aperture_size )
877 cv::Mat src = cv::cvarrToMat(srcarr), dst = cv::cvarrToMat(dstarr);
879 CV_Assert( src.size() == dst.size() && src.channels() == dst.channels() );
881 cv::Sobel( src, dst, dst.depth(), dx, dy, aperture_size, 1, 0, cv::BORDER_REPLICATE );
882 if( CV_IS_IMAGE(srcarr) && ((IplImage*)srcarr)->origin && dy % 2 != 0 )
888 cvLaplace( const void* srcarr, void* dstarr, int aperture_size )
890 cv::Mat src = cv::cvarrToMat(srcarr), dst = cv::cvarrToMat(dstarr);
892 CV_Assert( src.size() == dst.size() && src.channels() == dst.channels() );
894 cv::Laplacian( src, dst, dst.depth(), aperture_size, 1, 0, cv::BORDER_REPLICATE );