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 #if defined (HAVE_IPP) && (IPP_VERSION_MAJOR >= 7)
47 static IppStatus sts = ippInit();
50 /****************************************************************************************\
51 Sobel & Scharr Derivative Filters
52 \****************************************************************************************/
57 static void getScharrKernels( OutputArray _kx, OutputArray _ky,
58 int dx, int dy, bool normalize, int ktype )
62 CV_Assert( ktype == CV_32F || ktype == CV_64F );
63 _kx.create(ksize, 1, ktype, -1, true);
64 _ky.create(ksize, 1, ktype, -1, true);
65 Mat kx = _kx.getMat();
66 Mat ky = _ky.getMat();
68 CV_Assert( dx >= 0 && dy >= 0 && dx+dy == 1 );
70 for( int k = 0; k < 2; k++ )
72 Mat* kernel = k == 0 ? &kx : &ky;
73 int order = k == 0 ? dx : dy;
77 kerI[0] = 3, kerI[1] = 10, kerI[2] = 3;
79 kerI[0] = -1, kerI[1] = 0, kerI[2] = 1;
81 Mat temp(kernel->rows, kernel->cols, CV_32S, &kerI[0]);
82 double scale = !normalize || order == 1 ? 1. : 1./32;
83 temp.convertTo(*kernel, ktype, scale);
88 static void getSobelKernels( OutputArray _kx, OutputArray _ky,
89 int dx, int dy, int _ksize, bool normalize, int ktype )
91 int i, j, ksizeX = _ksize, ksizeY = _ksize;
92 if( ksizeX == 1 && dx > 0 )
94 if( ksizeY == 1 && dy > 0 )
97 CV_Assert( ktype == CV_32F || ktype == CV_64F );
99 _kx.create(ksizeX, 1, ktype, -1, true);
100 _ky.create(ksizeY, 1, ktype, -1, true);
101 Mat kx = _kx.getMat();
102 Mat ky = _ky.getMat();
104 if( _ksize % 2 == 0 || _ksize > 31 )
105 CV_Error( CV_StsOutOfRange, "The kernel size must be odd and not larger than 31" );
106 std::vector<int> kerI(std::max(ksizeX, ksizeY) + 1);
108 CV_Assert( dx >= 0 && dy >= 0 && dx+dy > 0 );
110 for( int k = 0; k < 2; k++ )
112 Mat* kernel = k == 0 ? &kx : &ky;
113 int order = k == 0 ? dx : dy;
114 int ksize = k == 0 ? ksizeX : ksizeY;
116 CV_Assert( ksize > order );
120 else if( ksize == 3 )
123 kerI[0] = 1, kerI[1] = 2, kerI[2] = 1;
124 else if( order == 1 )
125 kerI[0] = -1, kerI[1] = 0, kerI[2] = 1;
127 kerI[0] = 1, kerI[1] = -2, kerI[2] = 1;
133 for( i = 0; i < ksize; i++ )
136 for( i = 0; i < ksize - order - 1; i++ )
139 for( j = 1; j <= ksize; j++ )
141 newval = kerI[j]+kerI[j-1];
147 for( i = 0; i < order; i++ )
150 for( j = 1; j <= ksize; j++ )
152 newval = kerI[j-1] - kerI[j];
159 Mat temp(kernel->rows, kernel->cols, CV_32S, &kerI[0]);
160 double scale = !normalize ? 1. : 1./(1 << (ksize-order-1));
161 temp.convertTo(*kernel, ktype, scale);
167 void cv::getDerivKernels( OutputArray kx, OutputArray ky, int dx, int dy,
168 int ksize, bool normalize, int ktype )
171 getScharrKernels( kx, ky, dx, dy, normalize, ktype );
173 getSobelKernels( kx, ky, dx, dy, ksize, normalize, ktype );
177 cv::Ptr<cv::FilterEngine> cv::createDerivFilter(int srcType, int dstType,
178 int dx, int dy, int ksize, int borderType )
181 getDerivKernels( kx, ky, dx, dy, ksize, false, CV_32F );
182 return createSeparableLinearFilter(srcType, dstType,
183 kx, ky, Point(-1,-1), 0, borderType );
186 #if defined (HAVE_IPP) && (IPP_VERSION_MAJOR >= 7)
188 #define IPP_RETURN_ERROR {setIppErrorStatus(); return false;}
192 #if IPP_VERSION_X100 >= 801
193 static bool IPPDerivScharr(InputArray _src, OutputArray _dst, int ddepth, int dx, int dy, double scale, double delta, int borderType)
195 if ((0 > dx) || (0 > dy) || (1 != dx + dy))
197 if (fabs(delta) > FLT_EPSILON)
200 IppiBorderType ippiBorderType = ippiGetBorderType(borderType & (~BORDER_ISOLATED));
201 if ((int)ippiBorderType < 0)
204 int stype = _src.type(), sdepth = CV_MAT_DEPTH(stype), cn = CV_MAT_CN(stype);
207 int dtype = CV_MAKETYPE(ddepth, cn);
209 Mat src = _src.getMat();
210 if (0 == (BORDER_ISOLATED & borderType))
212 Size size; Point offset;
213 src.locateROI(size, offset);
215 ippiBorderType = (IppiBorderType)(ippiBorderType | ippBorderInMemLeft);
217 ippiBorderType = (IppiBorderType)(ippiBorderType | ippBorderInMemTop);
218 if (offset.x + src.cols < size.width)
219 ippiBorderType = (IppiBorderType)(ippiBorderType | ippBorderInMemRight);
220 if (offset.y + src.rows < size.height)
221 ippiBorderType = (IppiBorderType)(ippiBorderType | ippBorderInMemBottom);
224 bool horz = (0 == dx) && (1 == dy);
225 IppiSize roiSize = {src.cols, src.rows};
227 _dst.create( _src.size(), dtype);
228 Mat dst = _dst.getMat();
229 IppStatus sts = ippStsErr;
230 if ((CV_8U == stype) && (CV_16S == dtype))
232 int bufferSize = 0; Ipp8u *pBuffer;
235 if (0 > ippiFilterScharrHorizMaskBorderGetBufferSize(roiSize, ippMskSize3x3, ipp8u, ipp16s, 1, &bufferSize))
237 pBuffer = ippsMalloc_8u(bufferSize);
240 sts = ippiFilterScharrHorizMaskBorder_8u16s_C1R(src.ptr(), (int)src.step, dst.ptr<Ipp16s>(), (int)dst.step, roiSize, ippMskSize3x3, ippiBorderType, 0, pBuffer);
244 if (0 > ippiFilterScharrVertMaskBorderGetBufferSize(roiSize, ippMskSize3x3, ipp8u, ipp16s, 1, &bufferSize))
246 pBuffer = ippsMalloc_8u(bufferSize);
249 sts = ippiFilterScharrVertMaskBorder_8u16s_C1R(src.ptr(), (int)src.step, dst.ptr<Ipp16s>(), (int)dst.step, roiSize, ippMskSize3x3, ippiBorderType, 0, pBuffer);
253 else if ((CV_16S == stype) && (CV_16S == dtype))
255 int bufferSize = 0; Ipp8u *pBuffer;
258 if (0 > ippiFilterScharrHorizMaskBorderGetBufferSize(roiSize, ippMskSize3x3, ipp16s, ipp16s, 1, &bufferSize))
260 pBuffer = ippsMalloc_8u(bufferSize);
263 sts = ippiFilterScharrHorizMaskBorder_16s_C1R(src.ptr<Ipp16s>(), (int)src.step, dst.ptr<Ipp16s>(), (int)dst.step, roiSize, ippMskSize3x3, ippiBorderType, 0, pBuffer);
267 if (0 > ippiFilterScharrVertMaskBorderGetBufferSize(roiSize, ippMskSize3x3, ipp16s, ipp16s, 1, &bufferSize))
269 pBuffer = ippsMalloc_8u(bufferSize);
272 sts = ippiFilterScharrVertMaskBorder_16s_C1R(src.ptr<Ipp16s>(), (int)src.step, dst.ptr<Ipp16s>(), (int)dst.step, roiSize, ippMskSize3x3, ippiBorderType, 0, pBuffer);
276 else if ((CV_32F == stype) && (CV_32F == dtype))
278 int bufferSize = 0; Ipp8u *pBuffer;
281 if (0 > ippiFilterScharrHorizMaskBorderGetBufferSize(roiSize, ippMskSize3x3, ipp32f, ipp32f, 1, &bufferSize))
283 pBuffer = ippsMalloc_8u(bufferSize);
286 sts = ippiFilterScharrHorizMaskBorder_32f_C1R(src.ptr<Ipp32f>(), (int)src.step, dst.ptr<Ipp32f>(), (int)dst.step, roiSize, ippMskSize3x3, ippiBorderType, 0, pBuffer);
290 if (0 > ippiFilterScharrVertMaskBorderGetBufferSize(roiSize, ippMskSize3x3, ipp32f, ipp32f, 1, &bufferSize))
292 pBuffer = ippsMalloc_8u(bufferSize);
295 sts = ippiFilterScharrVertMaskBorder_32f_C1R(src.ptr<Ipp32f>(), (int)src.step, dst.ptr<Ipp32f>(), (int)dst.step, roiSize, ippMskSize3x3, ippiBorderType, 0, pBuffer);
301 if (FLT_EPSILON < fabs(scale - 1.0))
302 sts = ippiMulC_32f_C1R(dst.ptr<Ipp32f>(), (int)dst.step, (Ipp32f)scale, dst.ptr<Ipp32f>(), (int)dst.step, roiSize);
306 #elif IPP_VERSION_X100 >= 700
307 static bool IPPDerivScharr(InputArray _src, OutputArray _dst, int ddepth, int dx, int dy, double scale, double delta, int borderType)
309 if (BORDER_REPLICATE != borderType)
311 if ((0 > dx) || (0 > dy) || (1 != dx + dy))
313 if (fabs(delta) > FLT_EPSILON)
316 Mat src = _src.getMat(), dst = _dst.getMat();
319 cv::AutoBuffer<char> buffer;
320 IppiSize roi = ippiSize(src.cols, src.rows);
323 ddepth = src.depth();
325 dst.create( src.size(), CV_MAKETYPE(ddepth, src.channels()) );
338 if ((dx == 1) && (dy == 0))
340 if (0 > ippiFilterScharrVertGetBufferSize_8u16s_C1R(roi,&bufSize))
342 buffer.allocate(bufSize);
343 return (0 <= ippiFilterScharrVertBorder_8u16s_C1R(src.ptr<Ipp8u>(), (int)src.step,
344 dst.ptr<Ipp16s>(), (int)dst.step, roi, ippBorderRepl, 0, (Ipp8u*)(char*)buffer));
346 if ((dx == 0) && (dy == 1))
348 if (0 > ippiFilterScharrHorizGetBufferSize_8u16s_C1R(roi,&bufSize))
350 buffer.allocate(bufSize);
351 return (0 <= ippiFilterScharrHorizBorder_8u16s_C1R(src.ptr<Ipp8u>(), (int)src.step,
352 dst.ptr<Ipp16s>(), (int)dst.step, roi, ippBorderRepl, 0, (Ipp8u*)(char*)buffer));
366 if ((dx == 1) && (dy == 0))
368 if (0 > ippiFilterScharrVertGetBufferSize_32f_C1R(ippiSize(src.cols, src.rows),&bufSize))
370 buffer.allocate(bufSize);
372 if (0 > ippiFilterScharrVertBorder_32f_C1R(src.ptr<Ipp32f>(), (int)src.step,
373 dst.ptr<Ipp32f>(), (int)dst.step, ippiSize(src.cols, src.rows),
374 ippBorderRepl, 0, (Ipp8u*)(char*)buffer))
380 /* IPP is fast, so MulC produce very little perf degradation.*/
381 //ippiMulC_32f_C1IR((Ipp32f)scale, dst.ptr<Ipp32f>(), (int)dst.step, ippiSize(dst.cols*dst.channels(), dst.rows));
382 ippiMulC_32f_C1R(dst.ptr<Ipp32f>(), (int)dst.step, (Ipp32f)scale, dst.ptr<Ipp32f>(), (int)dst.step, ippiSize(dst.cols*dst.channels(), dst.rows));
385 if ((dx == 0) && (dy == 1))
387 if (0 > ippiFilterScharrHorizGetBufferSize_32f_C1R(ippiSize(src.cols, src.rows),&bufSize))
389 buffer.allocate(bufSize);
391 if (0 > ippiFilterScharrHorizBorder_32f_C1R(src.ptr<Ipp32f>(), (int)src.step,
392 dst.ptr<Ipp32f>(), (int)dst.step, ippiSize(src.cols, src.rows),
393 ippBorderRepl, 0, (Ipp8u*)(char*)buffer))
397 ippiMulC_32f_C1R(dst.ptr<Ipp32f>(), (int)dst.step, (Ipp32f)scale, dst.ptr<Ipp32f>(), (int)dst.step, ippiSize(dst.cols*dst.channels(), dst.rows));
411 static bool IPPDerivSobel(InputArray _src, OutputArray _dst, int ddepth, int dx, int dy, int ksize, double scale, double delta, int borderType)
413 if ((borderType != BORDER_REPLICATE) || ((3 != ksize) && (5 != ksize)))
415 if (fabs(delta) > FLT_EPSILON)
417 if (1 != _src.channels())
421 cv::AutoBuffer<char> buffer;
422 Mat src = _src.getMat(), dst = _dst.getMat();
424 ddepth = src.depth();
426 if (src.type() == CV_8U && dst.type() == CV_16S && scale == 1)
428 if ((dx == 1) && (dy == 0))
430 if (0 > ippiFilterSobelNegVertGetBufferSize_8u16s_C1R(ippiSize(src.cols, src.rows), (IppiMaskSize)(ksize*10+ksize),&bufSize))
432 buffer.allocate(bufSize);
434 if (0 > ippiFilterSobelNegVertBorder_8u16s_C1R(src.ptr<Ipp8u>(), (int)src.step,
435 dst.ptr<Ipp16s>(), (int)dst.step, ippiSize(src.cols, src.rows), (IppiMaskSize)(ksize*10+ksize),
436 ippBorderRepl, 0, (Ipp8u*)(char*)buffer))
441 if ((dx == 0) && (dy == 1))
443 if (0 > ippiFilterSobelHorizGetBufferSize_8u16s_C1R(ippiSize(src.cols, src.rows), (IppiMaskSize)(ksize*10+ksize),&bufSize))
445 buffer.allocate(bufSize);
447 if (0 > ippiFilterSobelHorizBorder_8u16s_C1R(src.ptr<Ipp8u>(), (int)src.step,
448 dst.ptr<Ipp16s>(), (int)dst.step, ippiSize(src.cols, src.rows), (IppiMaskSize)(ksize*10+ksize),
449 ippBorderRepl, 0, (Ipp8u*)(char*)buffer))
454 #if !defined(HAVE_IPP_ICV_ONLY)
455 if ((dx == 2) && (dy == 0))
457 if (0 > ippiFilterSobelVertSecondGetBufferSize_8u16s_C1R(ippiSize(src.cols, src.rows), (IppiMaskSize)(ksize*10+ksize),&bufSize))
459 buffer.allocate(bufSize);
461 if (0 > ippiFilterSobelVertSecondBorder_8u16s_C1R(src.ptr<Ipp8u>(), (int)src.step,
462 dst.ptr<Ipp16s>(), (int)dst.step, ippiSize(src.cols, src.rows), (IppiMaskSize)(ksize*10+ksize),
463 ippBorderRepl, 0, (Ipp8u*)(char*)buffer))
468 if ((dx == 0) && (dy == 2))
470 if (0 > ippiFilterSobelHorizSecondGetBufferSize_8u16s_C1R(ippiSize(src.cols, src.rows), (IppiMaskSize)(ksize*10+ksize),&bufSize))
472 buffer.allocate(bufSize);
474 if (0 > ippiFilterSobelHorizSecondBorder_8u16s_C1R(src.ptr<Ipp8u>(), (int)src.step,
475 dst.ptr<Ipp16s>(), (int)dst.step, ippiSize(src.cols, src.rows), (IppiMaskSize)(ksize*10+ksize),
476 ippBorderRepl, 0, (Ipp8u*)(char*)buffer))
483 if (src.type() == CV_32F && dst.type() == CV_32F)
486 if ((dx == 1) && (dy == 0))
488 if (0 > ippiFilterSobelNegVertGetBufferSize_32f_C1R(ippiSize(src.cols, src.rows), (IppiMaskSize)(ksize*10+ksize), &bufSize))
490 buffer.allocate(bufSize);
492 if (0 > ippiFilterSobelNegVertBorder_32f_C1R(src.ptr<Ipp32f>(), (int)src.step,
493 dst.ptr<Ipp32f>(), (int)dst.step, ippiSize(src.cols, src.rows), (IppiMaskSize)(ksize*10+ksize),
494 ippBorderRepl, 0, (Ipp8u*)(char*)buffer))
497 ippiMulC_32f_C1R(dst.ptr<Ipp32f>(), (int)dst.step, (Ipp32f)scale, dst.ptr<Ipp32f>(), (int)dst.step, ippiSize(dst.cols*dst.channels(), dst.rows));
501 if ((dx == 0) && (dy == 1))
503 if (0 > ippiFilterSobelHorizGetBufferSize_32f_C1R(ippiSize(src.cols, src.rows), (IppiMaskSize)(ksize*10+ksize),&bufSize))
505 buffer.allocate(bufSize);
506 if (0 > ippiFilterSobelHorizBorder_32f_C1R(src.ptr<Ipp32f>(), (int)src.step,
507 dst.ptr<Ipp32f>(), (int)dst.step, ippiSize(src.cols, src.rows), (IppiMaskSize)(ksize*10+ksize),
508 ippBorderRepl, 0, (Ipp8u*)(char*)buffer))
511 ippiMulC_32f_C1R(dst.ptr<Ipp32f>(), (int)dst.step, (Ipp32f)scale, dst.ptr<Ipp32f>(), (int)dst.step, ippiSize(dst.cols*dst.channels(), dst.rows));
515 #if !defined(HAVE_IPP_ICV_ONLY)
516 if((dx == 2) && (dy == 0))
518 if (0 > ippiFilterSobelVertSecondGetBufferSize_32f_C1R(ippiSize(src.cols, src.rows), (IppiMaskSize)(ksize*10+ksize),&bufSize))
520 buffer.allocate(bufSize);
522 if (0 > ippiFilterSobelVertSecondBorder_32f_C1R(src.ptr<Ipp32f>(), (int)src.step,
523 dst.ptr<Ipp32f>(), (int)dst.step, ippiSize(src.cols, src.rows), (IppiMaskSize)(ksize*10+ksize),
524 ippBorderRepl, 0, (Ipp8u*)(char*)buffer))
527 ippiMulC_32f_C1R(dst.ptr<Ipp32f>(), (int)dst.step, (Ipp32f)scale, dst.ptr<Ipp32f>(), (int)dst.step, ippiSize(dst.cols*dst.channels(), dst.rows));
531 if((dx == 0) && (dy == 2))
533 if (0 > ippiFilterSobelHorizSecondGetBufferSize_32f_C1R(ippiSize(src.cols, src.rows), (IppiMaskSize)(ksize*10+ksize),&bufSize))
535 buffer.allocate(bufSize);
537 if (0 > ippiFilterSobelHorizSecondBorder_32f_C1R(src.ptr<Ipp32f>(), (int)src.step,
538 dst.ptr<Ipp32f>(), (int)dst.step, ippiSize(src.cols, src.rows), (IppiMaskSize)(ksize*10+ksize),
539 ippBorderRepl, 0, (Ipp8u*)(char*)buffer))
543 ippiMulC_32f_C1R(dst.ptr<Ipp32f>(), (int)dst.step, (Ipp32f)scale, dst.ptr<Ipp32f>(), (int)dst.step, ippiSize(dst.cols*dst.channels(), dst.rows));
555 void cv::Sobel( InputArray _src, OutputArray _dst, int ddepth, int dx, int dy,
556 int ksize, double scale, double delta, int borderType )
558 int stype = _src.type(), sdepth = CV_MAT_DEPTH(stype), cn = CV_MAT_CN(stype);
561 int dtype = CV_MAKE_TYPE(ddepth, cn);
562 _dst.create( _src.size(), dtype );
564 #ifdef HAVE_TEGRA_OPTIMIZATION
565 if (scale == 1.0 && delta == 0)
567 Mat src = _src.getMat(), dst = _dst.getMat();
568 if (ksize == 3 && tegra::sobel3x3(src, dst, dx, dy, borderType))
570 if (ksize == -1 && tegra::scharr(src, dst, dx, dy, borderType))
578 if (IPPDerivScharr(_src, _dst, ddepth, dx, dy, scale, delta, borderType))
583 if (IPPDerivSobel(_src, _dst, ddepth, dx, dy, ksize, scale, delta, borderType))
587 int ktype = std::max(CV_32F, std::max(ddepth, sdepth));
590 getDerivKernels( kx, ky, dx, dy, ksize, false, ktype );
593 // usually the smoothing part is the slowest to compute,
594 // so try to scale it instead of the faster differenciating part
600 sepFilter2D( _src, _dst, ddepth, kx, ky, Point(-1, -1), delta, borderType );
604 void cv::Scharr( InputArray _src, OutputArray _dst, int ddepth, int dx, int dy,
605 double scale, double delta, int borderType )
607 int stype = _src.type(), sdepth = CV_MAT_DEPTH(stype), cn = CV_MAT_CN(stype);
610 int dtype = CV_MAKETYPE(ddepth, cn);
611 _dst.create( _src.size(), dtype );
613 #ifdef HAVE_TEGRA_OPTIMIZATION
614 if (scale == 1.0 && delta == 0)
616 Mat src = _src.getMat(), dst = _dst.getMat();
617 if (tegra::scharr(src, dst, dx, dy, borderType))
622 #if defined (HAVE_IPP) && (IPP_VERSION_MAJOR >= 7)
623 if (IPPDerivScharr(_src, _dst, ddepth, dx, dy, scale, delta, borderType))
626 int ktype = std::max(CV_32F, std::max(ddepth, sdepth));
629 getScharrKernels( kx, ky, dx, dy, false, ktype );
632 // usually the smoothing part is the slowest to compute,
633 // so try to scale it instead of the faster differenciating part
639 sepFilter2D( _src, _dst, ddepth, kx, ky, Point(-1, -1), delta, borderType );
646 static bool ocl_Laplacian5(InputArray _src, OutputArray _dst,
647 const Mat & kd, const Mat & ks, double scale, double delta,
648 int borderType, int depth, int ddepth)
650 int iscale = cvRound(scale), idelta = cvRound(delta);
651 bool doubleSupport = ocl::Device::getDefault().doubleFPConfig() > 0,
652 floatCoeff = std::fabs(delta - idelta) > DBL_EPSILON || std::fabs(scale - iscale) > DBL_EPSILON;
653 int cn = _src.channels(), wdepth = std::max(depth, floatCoeff ? CV_32F : CV_32S), kercn = 1;
655 if (!doubleSupport && wdepth == CV_64F)
659 ocl::Kernel k("sumConvert", ocl::imgproc::laplacian5_oclsrc,
660 format("-D srcT=%s -D WT=%s -D dstT=%s -D coeffT=%s -D wdepth=%d "
661 "-D convertToWT=%s -D convertToDT=%s%s",
662 ocl::typeToStr(CV_MAKE_TYPE(depth, kercn)),
663 ocl::typeToStr(CV_MAKE_TYPE(wdepth, kercn)),
664 ocl::typeToStr(CV_MAKE_TYPE(ddepth, kercn)),
665 ocl::typeToStr(wdepth), wdepth,
666 ocl::convertTypeStr(depth, wdepth, kercn, cvt[0]),
667 ocl::convertTypeStr(wdepth, ddepth, kercn, cvt[1]),
668 doubleSupport ? " -D DOUBLE_SUPPORT" : ""));
673 sepFilter2D(_src, d2x, depth, kd, ks, Point(-1, -1), 0, borderType);
674 sepFilter2D(_src, d2y, depth, ks, kd, Point(-1, -1), 0, borderType);
676 UMat dst = _dst.getUMat();
678 ocl::KernelArg d2xarg = ocl::KernelArg::ReadOnlyNoSize(d2x),
679 d2yarg = ocl::KernelArg::ReadOnlyNoSize(d2y),
680 dstarg = ocl::KernelArg::WriteOnly(dst, cn, kercn);
682 if (wdepth >= CV_32F)
683 k.args(d2xarg, d2yarg, dstarg, (float)scale, (float)delta);
685 k.args(d2xarg, d2yarg, dstarg, iscale, idelta);
687 size_t globalsize[] = { dst.cols * cn / kercn, dst.rows };
688 return k.run(2, globalsize, NULL, false);
695 void cv::Laplacian( InputArray _src, OutputArray _dst, int ddepth, int ksize,
696 double scale, double delta, int borderType )
698 int stype = _src.type(), sdepth = CV_MAT_DEPTH(stype), cn = CV_MAT_CN(stype);
701 _dst.create( _src.size(), CV_MAKETYPE(ddepth, cn) );
704 if ((ksize == 3 || ksize == 5) && ((borderType & BORDER_ISOLATED) != 0 || !_src.isSubmatrix()) &&
705 ((stype == CV_8UC1 && ddepth == CV_16S) || (ddepth == CV_32F && stype == CV_32FC1)) && !ocl::useOpenCL())
707 int iscale = saturate_cast<int>(scale), idelta = saturate_cast<int>(delta);
708 bool floatScale = std::fabs(scale - iscale) > DBL_EPSILON, needScale = iscale != 1;
709 bool floatDelta = std::fabs(delta - idelta) > DBL_EPSILON, needDelta = delta != 0;
710 int borderTypeNI = borderType & ~BORDER_ISOLATED;
711 Mat src = _src.getMat(), dst = _dst.getMat();
713 if (src.data != dst.data)
716 IppStatus status = (IppStatus)-1;
717 IppiSize roisize = { src.cols, src.rows };
718 IppiMaskSize masksize = ksize == 3 ? ippMskSize3x3 : ippMskSize5x5;
719 IppiBorderType borderTypeIpp = ippiGetBorderType(borderTypeNI);
721 #define IPP_FILTER_LAPLACIAN(ippsrctype, ippdsttype, ippfavor) \
724 if (borderTypeIpp >= 0 && ippiFilterLaplacianGetBufferSize_##ippfavor##_C1R(roisize, masksize, &bufsize) >= 0) \
726 Ipp8u * buffer = ippsMalloc_8u(bufsize); \
727 status = ippiFilterLaplacianBorder_##ippfavor##_C1R(src.ptr<ippsrctype>(), (int)src.step, dst.ptr<ippdsttype>(), \
728 (int)dst.step, roisize, masksize, borderTypeIpp, 0, buffer); \
733 CV_SUPPRESS_DEPRECATED_START
734 if (sdepth == CV_8U && ddepth == CV_16S && !floatScale && !floatDelta)
736 IPP_FILTER_LAPLACIAN(Ipp8u, Ipp16s, 8u16s);
738 if (needScale && status >= 0)
739 status = ippiMulC_16s_C1IRSfs((Ipp16s)iscale, dst.ptr<Ipp16s>(), (int)dst.step, roisize, 0);
740 if (needDelta && status >= 0)
741 status = ippiAddC_16s_C1IRSfs((Ipp16s)idelta, dst.ptr<Ipp16s>(), (int)dst.step, roisize, 0);
743 else if (sdepth == CV_32F && ddepth == CV_32F)
745 IPP_FILTER_LAPLACIAN(Ipp32f, Ipp32f, 32f);
747 if (needScale && status >= 0)
748 status = ippiMulC_32f_C1IR((Ipp32f)scale, dst.ptr<Ipp32f>(), (int)dst.step, roisize);
749 if (needDelta && status >= 0)
750 status = ippiAddC_32f_C1IR((Ipp32f)delta, dst.ptr<Ipp32f>(), (int)dst.step, roisize);
752 CV_SUPPRESS_DEPRECATED_END
759 #undef IPP_FILTER_LAPLACIAN
762 #ifdef HAVE_TEGRA_OPTIMIZATION
763 if (scale == 1.0 && delta == 0)
765 Mat src = _src.getMat(), dst = _dst.getMat();
766 if (ksize == 1 && tegra::laplace1(src, dst, borderType))
768 if (ksize == 3 && tegra::laplace3(src, dst, borderType))
770 if (ksize == 5 && tegra::laplace5(src, dst, borderType))
775 if( ksize == 1 || ksize == 3 )
779 { 0, 1, 0, 1, -4, 1, 0, 1, 0 },
780 { 2, 0, 2, 0, -8, 0, 2, 0, 2 }
782 Mat kernel(3, 3, CV_32F, K[ksize == 3]);
785 filter2D( _src, _dst, ddepth, kernel, Point(-1, -1), delta, borderType );
789 int ktype = std::max(CV_32F, std::max(ddepth, sdepth));
790 int wdepth = sdepth == CV_8U && ksize <= 5 ? CV_16S : sdepth <= CV_32F ? CV_32F : CV_64F;
791 int wtype = CV_MAKETYPE(wdepth, cn);
793 getSobelKernels( kd, ks, 2, 0, ksize, false, ktype );
795 CV_OCL_RUN(_dst.isUMat(),
796 ocl_Laplacian5(_src, _dst, kd, ks, scale,
797 delta, borderType, wdepth, ddepth))
799 const size_t STRIPE_SIZE = 1 << 14;
800 Ptr<FilterEngine> fx = createSeparableLinearFilter(stype,
801 wtype, kd, ks, Point(-1,-1), 0, borderType, borderType, Scalar() );
802 Ptr<FilterEngine> fy = createSeparableLinearFilter(stype,
803 wtype, ks, kd, Point(-1,-1), 0, borderType, borderType, Scalar() );
805 Mat src = _src.getMat(), dst = _dst.getMat();
806 int y = fx->start(src), dsty = 0, dy = 0;
808 const uchar* sptr = src.ptr(y);
810 int dy0 = std::min(std::max((int)(STRIPE_SIZE/(CV_ELEM_SIZE(stype)*src.cols)), 1), src.rows);
811 Mat d2x( dy0 + kd.rows - 1, src.cols, wtype );
812 Mat d2y( dy0 + kd.rows - 1, src.cols, wtype );
814 for( ; dsty < src.rows; sptr += dy0*src.step, dsty += dy )
816 fx->proceed( sptr, (int)src.step, dy0, d2x.ptr(), (int)d2x.step );
817 dy = fy->proceed( sptr, (int)src.step, dy0, d2y.ptr(), (int)d2y.step );
820 Mat dstripe = dst.rowRange(dsty, dsty + dy);
821 d2x.rows = d2y.rows = dy; // modify the headers, which should work
823 d2x.convertTo( dstripe, ddepth, scale, delta );
829 /////////////////////////////////////////////////////////////////////////////////////////
832 cvSobel( const void* srcarr, void* dstarr, int dx, int dy, int aperture_size )
834 cv::Mat src = cv::cvarrToMat(srcarr), dst = cv::cvarrToMat(dstarr);
836 CV_Assert( src.size() == dst.size() && src.channels() == dst.channels() );
838 cv::Sobel( src, dst, dst.depth(), dx, dy, aperture_size, 1, 0, cv::BORDER_REPLICATE );
839 if( CV_IS_IMAGE(srcarr) && ((IplImage*)srcarr)->origin && dy % 2 != 0 )
845 cvLaplace( const void* srcarr, void* dstarr, int aperture_size )
847 cv::Mat src = cv::cvarrToMat(srcarr), dst = cv::cvarrToMat(dstarr);
849 CV_Assert( src.size() == dst.size() && src.channels() == dst.channels() );
851 cv::Laplacian( src, dst, dst.depth(), aperture_size, 1, 0, cv::BORDER_REPLICATE );