2010-05-12 01:44:00 +08:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
|
|
|
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
2015-01-12 15:59:30 +08:00
|
|
|
// Copyright (C) 2014-2015, Itseez Inc., all rights reserved.
|
2010-05-12 01:44:00 +08:00
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * The name of the copyright holders may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//M*/
|
|
|
|
|
|
|
|
#include "precomp.hpp"
|
2018-01-22 18:26:32 +08:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
2017-05-24 01:04:35 +08:00
|
|
|
#include "opencv2/core/hal/intrin.hpp"
|
2014-08-01 22:11:20 +08:00
|
|
|
#include "opencl_kernels_imgproc.hpp"
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2016-12-14 22:31:41 +08:00
|
|
|
#include "opencv2/core/openvx/ovx_defs.hpp"
|
|
|
|
|
2017-11-14 17:57:02 +08:00
|
|
|
#include "filter.hpp"
|
|
|
|
|
2019-03-10 03:20:24 +08:00
|
|
|
#include "opencv2/core/softfloat.hpp"
|
|
|
|
|
|
|
|
namespace cv {
|
2018-01-22 18:26:32 +08:00
|
|
|
#include "fixedpoint.inl.hpp"
|
2019-03-10 03:20:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "smooth.simd.hpp"
|
|
|
|
#include "smooth.simd_declarations.hpp" // defines CV_CPU_DISPATCH_MODES_ALL=AVX2,...,BASELINE based on CMakeLists.txt content
|
|
|
|
|
|
|
|
namespace cv {
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
/****************************************************************************************\
|
|
|
|
Gaussian Blur
|
|
|
|
\****************************************************************************************/
|
|
|
|
|
2019-03-10 03:20:24 +08:00
|
|
|
Mat getGaussianKernel(int n, double sigma, int ktype)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-07-17 21:14:54 +08:00
|
|
|
CV_Assert(n > 0);
|
2010-05-12 01:44:00 +08:00
|
|
|
const int SMALL_GAUSSIAN_SIZE = 7;
|
|
|
|
static const float small_gaussian_tab[][SMALL_GAUSSIAN_SIZE] =
|
|
|
|
{
|
|
|
|
{1.f},
|
|
|
|
{0.25f, 0.5f, 0.25f},
|
|
|
|
{0.0625f, 0.25f, 0.375f, 0.25f, 0.0625f},
|
|
|
|
{0.03125f, 0.109375f, 0.21875f, 0.28125f, 0.21875f, 0.109375f, 0.03125f}
|
|
|
|
};
|
|
|
|
|
|
|
|
const float* fixed_kernel = n % 2 == 1 && n <= SMALL_GAUSSIAN_SIZE && sigma <= 0 ?
|
|
|
|
small_gaussian_tab[n>>1] : 0;
|
|
|
|
|
|
|
|
CV_Assert( ktype == CV_32F || ktype == CV_64F );
|
|
|
|
Mat kernel(n, 1, ktype);
|
2014-08-13 19:08:27 +08:00
|
|
|
float* cf = kernel.ptr<float>();
|
|
|
|
double* cd = kernel.ptr<double>();
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
double sigmaX = sigma > 0 ? sigma : ((n-1)*0.5 - 1)*0.3 + 0.8;
|
|
|
|
double scale2X = -0.5/(sigmaX*sigmaX);
|
|
|
|
double sum = 0;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for( i = 0; i < n; i++ )
|
|
|
|
{
|
|
|
|
double x = i - (n-1)*0.5;
|
|
|
|
double t = fixed_kernel ? (double)fixed_kernel[i] : std::exp(scale2X*x*x);
|
|
|
|
if( ktype == CV_32F )
|
|
|
|
{
|
|
|
|
cf[i] = (float)t;
|
|
|
|
sum += cf[i];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cd[i] = t;
|
|
|
|
sum += cd[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-17 21:14:54 +08:00
|
|
|
CV_DbgAssert(fabs(sum) > 0);
|
2010-05-12 01:44:00 +08:00
|
|
|
sum = 1./sum;
|
|
|
|
for( i = 0; i < n; i++ )
|
|
|
|
{
|
|
|
|
if( ktype == CV_32F )
|
|
|
|
cf[i] = (float)(cf[i]*sum);
|
|
|
|
else
|
|
|
|
cd[i] *= sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
return kernel;
|
|
|
|
}
|
|
|
|
|
2018-01-22 18:26:32 +08:00
|
|
|
template <typename T>
|
|
|
|
static std::vector<T> getFixedpointGaussianKernel( int n, double sigma )
|
|
|
|
{
|
|
|
|
if (sigma <= 0)
|
|
|
|
{
|
|
|
|
if(n == 1)
|
|
|
|
return std::vector<T>(1, softdouble(1.0));
|
|
|
|
else if(n == 3)
|
|
|
|
{
|
|
|
|
T v3[] = { softdouble(0.25), softdouble(0.5), softdouble(0.25) };
|
|
|
|
return std::vector<T>(v3, v3 + 3);
|
|
|
|
}
|
|
|
|
else if(n == 5)
|
|
|
|
{
|
|
|
|
T v5[] = { softdouble(0.0625), softdouble(0.25), softdouble(0.375), softdouble(0.25), softdouble(0.0625) };
|
|
|
|
return std::vector<T>(v5, v5 + 5);
|
|
|
|
}
|
|
|
|
else if(n == 7)
|
|
|
|
{
|
|
|
|
T v7[] = { softdouble(0.03125), softdouble(0.109375), softdouble(0.21875), softdouble(0.28125), softdouble(0.21875), softdouble(0.109375), softdouble(0.03125) };
|
|
|
|
return std::vector<T>(v7, v7 + 7);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
softdouble sigmaX = sigma > 0 ? softdouble(sigma) : mulAdd(softdouble(n),softdouble(0.15),softdouble(0.35));// softdouble(((n-1)*0.5 - 1)*0.3 + 0.8)
|
|
|
|
softdouble scale2X = softdouble(-0.5*0.25)/(sigmaX*sigmaX);
|
|
|
|
std::vector<softdouble> values(n);
|
|
|
|
softdouble sum(0.);
|
|
|
|
for(int i = 0, x = 1 - n; i < n; i++, x+=2 )
|
|
|
|
{
|
|
|
|
// x = i - (n - 1)*0.5
|
|
|
|
// t = std::exp(scale2X*x*x)
|
|
|
|
values[i] = exp(softdouble(x*x)*scale2X);
|
|
|
|
sum += values[i];
|
|
|
|
}
|
|
|
|
sum = softdouble::one()/sum;
|
|
|
|
|
|
|
|
std::vector<T> kernel(n);
|
|
|
|
for(int i = 0; i < n; i++ )
|
|
|
|
{
|
|
|
|
kernel[i] = values[i] * sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
return kernel;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void getGaussianKernel(int n, double sigma, int ktype, Mat& res) { res = getGaussianKernel(n, sigma, ktype); }
|
|
|
|
template <typename T> static void getGaussianKernel(int n, double sigma, int, std::vector<T>& res) { res = getFixedpointGaussianKernel<T>(n, sigma); }
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static void createGaussianKernels( T & kx, T & ky, int type, Size &ksize,
|
2013-12-11 00:14:37 +08:00
|
|
|
double sigma1, double sigma2 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
int depth = CV_MAT_DEPTH(type);
|
|
|
|
if( sigma2 <= 0 )
|
|
|
|
sigma2 = sigma1;
|
|
|
|
|
|
|
|
// automatic detection of kernel size from sigma
|
|
|
|
if( ksize.width <= 0 && sigma1 > 0 )
|
|
|
|
ksize.width = cvRound(sigma1*(depth == CV_8U ? 3 : 4)*2 + 1)|1;
|
|
|
|
if( ksize.height <= 0 && sigma2 > 0 )
|
|
|
|
ksize.height = cvRound(sigma2*(depth == CV_8U ? 3 : 4)*2 + 1)|1;
|
|
|
|
|
2018-08-31 22:05:00 +08:00
|
|
|
CV_Assert( ksize.width > 0 && ksize.width % 2 == 1 &&
|
|
|
|
ksize.height > 0 && ksize.height % 2 == 1 );
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
sigma1 = std::max( sigma1, 0. );
|
|
|
|
sigma2 = std::max( sigma2, 0. );
|
|
|
|
|
2018-01-22 18:26:32 +08:00
|
|
|
getGaussianKernel( ksize.width, sigma1, std::max(depth, CV_32F), kx );
|
2010-05-12 01:44:00 +08:00
|
|
|
if( ksize.height == ksize.width && std::abs(sigma1 - sigma2) < DBL_EPSILON )
|
|
|
|
ky = kx;
|
|
|
|
else
|
2018-01-22 18:26:32 +08:00
|
|
|
getGaussianKernel( ksize.height, sigma2, std::max(depth, CV_32F), ky );
|
2013-12-11 00:14:37 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 03:20:24 +08:00
|
|
|
Ptr<FilterEngine> createGaussianFilter( int type, Size ksize,
|
2013-12-11 00:14:37 +08:00
|
|
|
double sigma1, double sigma2,
|
|
|
|
int borderType )
|
|
|
|
{
|
|
|
|
Mat kx, ky;
|
|
|
|
createGaussianKernels(kx, ky, type, ksize, sigma1, sigma2);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
return createSeparableLinearFilter( type, type, kx, ky, Point(-1,-1), 0, borderType );
|
|
|
|
}
|
|
|
|
|
2016-10-21 13:17:45 +08:00
|
|
|
#ifdef HAVE_OPENCL
|
|
|
|
|
2016-11-30 17:06:05 +08:00
|
|
|
static bool ocl_GaussianBlur_8UC1(InputArray _src, OutputArray _dst, Size ksize, int ddepth,
|
|
|
|
InputArray _kernelX, InputArray _kernelY, int borderType)
|
2016-10-21 13:17:45 +08:00
|
|
|
{
|
|
|
|
const ocl::Device & dev = ocl::Device::getDefault();
|
|
|
|
int type = _src.type(), sdepth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
|
|
|
|
|
|
|
|
if ( !(dev.isIntel() && (type == CV_8UC1) &&
|
|
|
|
(_src.offset() == 0) && (_src.step() % 4 == 0) &&
|
2016-11-30 17:06:05 +08:00
|
|
|
((ksize.width == 5 && (_src.cols() % 4 == 0)) ||
|
|
|
|
(ksize.width == 3 && (_src.cols() % 16 == 0) && (_src.rows() % 2 == 0)))) )
|
2016-10-21 13:17:45 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
Mat kernelX = _kernelX.getMat().reshape(1, 1);
|
|
|
|
if (kernelX.cols % 2 != 1)
|
|
|
|
return false;
|
|
|
|
Mat kernelY = _kernelY.getMat().reshape(1, 1);
|
|
|
|
if (kernelY.cols % 2 != 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (ddepth < 0)
|
|
|
|
ddepth = sdepth;
|
|
|
|
|
|
|
|
Size size = _src.size();
|
|
|
|
size_t globalsize[2] = { 0, 0 };
|
|
|
|
size_t localsize[2] = { 0, 0 };
|
|
|
|
|
2016-11-30 17:06:05 +08:00
|
|
|
if (ksize.width == 3)
|
|
|
|
{
|
|
|
|
globalsize[0] = size.width / 16;
|
|
|
|
globalsize[1] = size.height / 2;
|
|
|
|
}
|
|
|
|
else if (ksize.width == 5)
|
|
|
|
{
|
|
|
|
globalsize[0] = size.width / 4;
|
|
|
|
globalsize[1] = size.height / 1;
|
|
|
|
}
|
2016-10-21 13:17:45 +08:00
|
|
|
|
|
|
|
const char * const borderMap[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", 0, "BORDER_REFLECT_101" };
|
|
|
|
char build_opts[1024];
|
2017-12-15 22:28:38 +08:00
|
|
|
sprintf(build_opts, "-D %s %s%s", borderMap[borderType & ~BORDER_ISOLATED],
|
2016-10-21 13:17:45 +08:00
|
|
|
ocl::kernelToStr(kernelX, CV_32F, "KERNEL_MATRIX_X").c_str(),
|
|
|
|
ocl::kernelToStr(kernelY, CV_32F, "KERNEL_MATRIX_Y").c_str());
|
|
|
|
|
2016-11-30 17:06:05 +08:00
|
|
|
ocl::Kernel kernel;
|
|
|
|
|
|
|
|
if (ksize.width == 3)
|
|
|
|
kernel.create("gaussianBlur3x3_8UC1_cols16_rows2", cv::ocl::imgproc::gaussianBlur3x3_oclsrc, build_opts);
|
|
|
|
else if (ksize.width == 5)
|
|
|
|
kernel.create("gaussianBlur5x5_8UC1_cols4", cv::ocl::imgproc::gaussianBlur5x5_oclsrc, build_opts);
|
|
|
|
|
2016-10-21 13:17:45 +08:00
|
|
|
if (kernel.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
UMat src = _src.getUMat();
|
|
|
|
_dst.create(size, CV_MAKETYPE(ddepth, cn));
|
|
|
|
if (!(_dst.offset() == 0 && _dst.step() % 4 == 0))
|
|
|
|
return false;
|
|
|
|
UMat dst = _dst.getUMat();
|
|
|
|
|
|
|
|
int idxArg = kernel.set(0, ocl::KernelArg::PtrReadOnly(src));
|
|
|
|
idxArg = kernel.set(idxArg, (int)src.step);
|
|
|
|
idxArg = kernel.set(idxArg, ocl::KernelArg::PtrWriteOnly(dst));
|
|
|
|
idxArg = kernel.set(idxArg, (int)dst.step);
|
|
|
|
idxArg = kernel.set(idxArg, (int)dst.rows);
|
|
|
|
idxArg = kernel.set(idxArg, (int)dst.cols);
|
|
|
|
|
|
|
|
return kernel.run(2, globalsize, (localsize[0] == 0) ? NULL : localsize, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2016-10-28 02:00:47 +08:00
|
|
|
#ifdef HAVE_OPENVX
|
|
|
|
|
2017-05-26 00:10:21 +08:00
|
|
|
namespace ovx {
|
|
|
|
template <> inline bool skipSmallImages<VX_KERNEL_GAUSSIAN_3x3>(int w, int h) { return w*h < 320 * 240; }
|
|
|
|
}
|
2016-10-28 02:00:47 +08:00
|
|
|
static bool openvx_gaussianBlur(InputArray _src, OutputArray _dst, Size ksize,
|
|
|
|
double sigma1, double sigma2, int borderType)
|
|
|
|
{
|
|
|
|
if (sigma2 <= 0)
|
|
|
|
sigma2 = sigma1;
|
|
|
|
// automatic detection of kernel size from sigma
|
|
|
|
if (ksize.width <= 0 && sigma1 > 0)
|
|
|
|
ksize.width = cvRound(sigma1*6 + 1) | 1;
|
|
|
|
if (ksize.height <= 0 && sigma2 > 0)
|
|
|
|
ksize.height = cvRound(sigma2*6 + 1) | 1;
|
|
|
|
|
2017-04-11 18:22:38 +08:00
|
|
|
if (_src.type() != CV_8UC1 ||
|
2017-04-11 19:15:26 +08:00
|
|
|
_src.cols() < 3 || _src.rows() < 3 ||
|
2017-04-11 18:22:38 +08:00
|
|
|
ksize.width != 3 || ksize.height != 3)
|
2016-10-28 02:00:47 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
sigma1 = std::max(sigma1, 0.);
|
|
|
|
sigma2 = std::max(sigma2, 0.);
|
|
|
|
|
2017-04-11 18:22:38 +08:00
|
|
|
if (!(sigma1 == 0.0 || (sigma1 - 0.8) < DBL_EPSILON) || !(sigma2 == 0.0 || (sigma2 - 0.8) < DBL_EPSILON) ||
|
2017-04-11 19:15:26 +08:00
|
|
|
ovx::skipSmallImages<VX_KERNEL_GAUSSIAN_3x3>(_src.cols(), _src.rows()))
|
2017-03-28 23:02:42 +08:00
|
|
|
return false;
|
|
|
|
|
2016-10-28 02:00:47 +08:00
|
|
|
Mat src = _src.getMat();
|
|
|
|
Mat dst = _dst.getMat();
|
|
|
|
|
|
|
|
if ((borderType & BORDER_ISOLATED) == 0 && src.isSubmatrix())
|
|
|
|
return false; //Process isolated borders only
|
2016-12-21 21:19:06 +08:00
|
|
|
vx_enum border;
|
2016-10-28 02:00:47 +08:00
|
|
|
switch (borderType & ~BORDER_ISOLATED)
|
|
|
|
{
|
|
|
|
case BORDER_CONSTANT:
|
2016-12-21 21:19:06 +08:00
|
|
|
border = VX_BORDER_CONSTANT;
|
2016-10-28 02:00:47 +08:00
|
|
|
break;
|
|
|
|
case BORDER_REPLICATE:
|
2016-12-21 21:19:06 +08:00
|
|
|
border = VX_BORDER_REPLICATE;
|
2016-10-28 02:00:47 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2017-02-14 18:32:17 +08:00
|
|
|
ivx::Context ctx = ovx::getOpenVXContext();
|
2016-10-28 02:00:47 +08:00
|
|
|
|
|
|
|
Mat a;
|
|
|
|
if (dst.data != src.data)
|
|
|
|
a = src;
|
|
|
|
else
|
|
|
|
src.copyTo(a);
|
|
|
|
|
|
|
|
ivx::Image
|
|
|
|
ia = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
|
|
|
|
ivx::Image::createAddressing(a.cols, a.rows, 1, (vx_int32)(a.step)), a.data),
|
|
|
|
ib = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
|
|
|
|
ivx::Image::createAddressing(dst.cols, dst.rows, 1, (vx_int32)(dst.step)), dst.data);
|
|
|
|
|
|
|
|
//ATTENTION: VX_CONTEXT_IMMEDIATE_BORDER attribute change could lead to strange issues in multi-threaded environments
|
2018-02-14 00:28:11 +08:00
|
|
|
//since OpenVX standard says nothing about thread-safety for now
|
2016-12-05 18:06:34 +08:00
|
|
|
ivx::border_t prevBorder = ctx.immediateBorder();
|
2016-12-21 21:19:06 +08:00
|
|
|
ctx.setImmediateBorder(border, (vx_uint8)(0));
|
2017-04-11 18:22:38 +08:00
|
|
|
ivx::IVX_CHECK_STATUS(vxuGaussian3x3(ctx, ia, ib));
|
2016-12-05 18:06:34 +08:00
|
|
|
ctx.setImmediateBorder(prevBorder);
|
2016-10-28 02:00:47 +08:00
|
|
|
}
|
2018-10-20 04:21:20 +08:00
|
|
|
catch (const ivx::RuntimeError & e)
|
2016-10-28 02:00:47 +08:00
|
|
|
{
|
2016-12-14 22:31:41 +08:00
|
|
|
VX_DbgThrow(e.what());
|
2016-10-28 02:00:47 +08:00
|
|
|
}
|
2018-10-20 04:21:20 +08:00
|
|
|
catch (const ivx::WrapperError & e)
|
2016-10-28 02:00:47 +08:00
|
|
|
{
|
2016-12-14 22:31:41 +08:00
|
|
|
VX_DbgThrow(e.what());
|
2016-10-28 02:00:47 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2016-10-21 13:17:45 +08:00
|
|
|
#ifdef HAVE_IPP
|
2018-04-16 18:29:14 +08:00
|
|
|
// IW 2017u2 has bug which doesn't allow use of partial inMem with tiling
|
|
|
|
#if IPP_DISABLE_GAUSSIANBLUR_PARALLEL
|
2017-08-17 19:57:58 +08:00
|
|
|
#define IPP_GAUSSIANBLUR_PARALLEL 0
|
|
|
|
#else
|
2017-05-31 17:16:47 +08:00
|
|
|
#define IPP_GAUSSIANBLUR_PARALLEL 1
|
2017-08-17 19:57:58 +08:00
|
|
|
#endif
|
2017-05-31 17:16:47 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_IPP_IW
|
|
|
|
|
|
|
|
class ipp_gaussianBlurParallel: public ParallelLoopBody
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ipp_gaussianBlurParallel(::ipp::IwiImage &src, ::ipp::IwiImage &dst, int kernelSize, float sigma, ::ipp::IwiBorderType &border, bool *pOk):
|
|
|
|
m_src(src), m_dst(dst), m_kernelSize(kernelSize), m_sigma(sigma), m_border(border), m_pOk(pOk) {
|
|
|
|
*m_pOk = true;
|
|
|
|
}
|
|
|
|
~ipp_gaussianBlurParallel()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-03-15 21:16:51 +08:00
|
|
|
virtual void operator() (const Range& range) const CV_OVERRIDE
|
2017-05-31 17:16:47 +08:00
|
|
|
{
|
2018-09-14 05:35:26 +08:00
|
|
|
CV_INSTRUMENT_REGION_IPP();
|
2017-05-31 17:16:47 +08:00
|
|
|
|
|
|
|
if(!*m_pOk)
|
|
|
|
return;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2017-08-17 19:57:58 +08:00
|
|
|
::ipp::IwiTile tile = ::ipp::IwiRoi(0, range.start, m_dst.m_size.width, range.end - range.start);
|
|
|
|
CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterGaussian, m_src, m_dst, m_kernelSize, m_sigma, ::ipp::IwDefault(), m_border, tile);
|
2017-05-31 17:16:47 +08:00
|
|
|
}
|
2018-10-17 03:09:26 +08:00
|
|
|
catch(const ::ipp::IwException &)
|
2017-05-31 17:16:47 +08:00
|
|
|
{
|
|
|
|
*m_pOk = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
::ipp::IwiImage &m_src;
|
|
|
|
::ipp::IwiImage &m_dst;
|
|
|
|
|
|
|
|
int m_kernelSize;
|
|
|
|
float m_sigma;
|
|
|
|
::ipp::IwiBorderType &m_border;
|
|
|
|
|
|
|
|
volatile bool *m_pOk;
|
|
|
|
const ipp_gaussianBlurParallel& operator= (const ipp_gaussianBlurParallel&);
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-04-13 20:50:23 +08:00
|
|
|
static bool ipp_GaussianBlur(InputArray _src, OutputArray _dst, Size ksize,
|
|
|
|
double sigma1, double sigma2, int borderType )
|
2015-05-15 16:15:00 +08:00
|
|
|
{
|
2017-04-13 20:50:23 +08:00
|
|
|
#ifdef HAVE_IPP_IW
|
2018-09-14 05:35:26 +08:00
|
|
|
CV_INSTRUMENT_REGION_IPP();
|
2016-08-18 14:53:00 +08:00
|
|
|
|
2017-08-17 19:57:58 +08:00
|
|
|
#if IPP_VERSION_X100 < 201800 && ((defined _MSC_VER && defined _M_IX86) || (defined __GNUC__ && defined __i386__))
|
2017-04-13 20:50:23 +08:00
|
|
|
CV_UNUSED(_src); CV_UNUSED(_dst); CV_UNUSED(ksize); CV_UNUSED(sigma1); CV_UNUSED(sigma2); CV_UNUSED(borderType);
|
|
|
|
return false; // bug on ia32
|
|
|
|
#else
|
|
|
|
if(sigma1 != sigma2)
|
2016-08-04 22:50:23 +08:00
|
|
|
return false;
|
|
|
|
|
2017-04-13 20:50:23 +08:00
|
|
|
if(sigma1 < FLT_EPSILON)
|
|
|
|
return false;
|
2015-05-15 16:15:00 +08:00
|
|
|
|
2017-04-13 20:50:23 +08:00
|
|
|
if(ksize.width != ksize.height)
|
|
|
|
return false;
|
2015-05-15 16:15:00 +08:00
|
|
|
|
2017-04-13 20:50:23 +08:00
|
|
|
// Acquire data and begin processing
|
|
|
|
try
|
2015-05-15 16:15:00 +08:00
|
|
|
{
|
2017-04-13 20:50:23 +08:00
|
|
|
Mat src = _src.getMat();
|
|
|
|
Mat dst = _dst.getMat();
|
2017-08-17 19:57:58 +08:00
|
|
|
::ipp::IwiImage iwSrc = ippiGetImage(src);
|
|
|
|
::ipp::IwiImage iwDst = ippiGetImage(dst);
|
|
|
|
::ipp::IwiBorderSize borderSize = ::ipp::iwiSizeToBorderSize(ippiGetSize(ksize));
|
2017-04-13 20:50:23 +08:00
|
|
|
::ipp::IwiBorderType ippBorder(ippiGetBorder(iwSrc, borderType, borderSize));
|
2017-08-17 19:57:58 +08:00
|
|
|
if(!ippBorder)
|
2017-04-13 20:50:23 +08:00
|
|
|
return false;
|
2015-05-15 16:15:00 +08:00
|
|
|
|
2017-05-31 17:16:47 +08:00
|
|
|
const int threads = ippiSuggestThreadsNum(iwDst, 2);
|
2017-08-17 19:57:58 +08:00
|
|
|
if(IPP_GAUSSIANBLUR_PARALLEL && threads > 1) {
|
2017-06-01 12:34:50 +08:00
|
|
|
bool ok;
|
|
|
|
ipp_gaussianBlurParallel invoker(iwSrc, iwDst, ksize.width, (float) sigma1, ippBorder, &ok);
|
|
|
|
|
|
|
|
if(!ok)
|
|
|
|
return false;
|
|
|
|
const Range range(0, (int) iwDst.m_size.height);
|
2017-05-31 17:16:47 +08:00
|
|
|
parallel_for_(range, invoker, threads*4);
|
|
|
|
|
2017-06-01 12:34:50 +08:00
|
|
|
if(!ok)
|
|
|
|
return false;
|
|
|
|
} else {
|
2017-08-17 19:57:58 +08:00
|
|
|
CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterGaussian, iwSrc, iwDst, ksize.width, sigma1, ::ipp::IwDefault(), ippBorder);
|
2017-06-01 12:34:50 +08:00
|
|
|
}
|
2017-04-13 20:50:23 +08:00
|
|
|
}
|
2018-10-17 03:09:26 +08:00
|
|
|
catch (const ::ipp::IwException &)
|
2017-04-13 20:50:23 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-15 16:15:00 +08:00
|
|
|
|
2017-04-13 20:50:23 +08:00
|
|
|
return true;
|
2015-10-12 15:51:28 +08:00
|
|
|
#endif
|
2015-09-25 22:13:11 +08:00
|
|
|
#else
|
|
|
|
CV_UNUSED(_src); CV_UNUSED(_dst); CV_UNUSED(ksize); CV_UNUSED(sigma1); CV_UNUSED(sigma2); CV_UNUSED(borderType);
|
2015-05-15 16:15:00 +08:00
|
|
|
return false;
|
2017-04-13 20:50:23 +08:00
|
|
|
#endif
|
2015-05-15 16:15:00 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-03-10 03:20:24 +08:00
|
|
|
void GaussianBlur(InputArray _src, OutputArray _dst, Size ksize,
|
|
|
|
double sigma1, double sigma2,
|
|
|
|
int borderType)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2018-09-14 05:35:26 +08:00
|
|
|
CV_INSTRUMENT_REGION();
|
2016-08-18 14:53:00 +08:00
|
|
|
|
2013-12-11 00:14:37 +08:00
|
|
|
int type = _src.type();
|
|
|
|
Size size = _src.size();
|
|
|
|
_dst.create( size, type );
|
2012-06-08 01:21:29 +08:00
|
|
|
|
2018-01-22 18:26:32 +08:00
|
|
|
if( (borderType & ~BORDER_ISOLATED) != BORDER_CONSTANT &&
|
|
|
|
((borderType & BORDER_ISOLATED) != 0 || !_src.getMat().isSubmatrix()) )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2013-12-11 00:14:37 +08:00
|
|
|
if( size.height == 1 )
|
2010-05-12 01:44:00 +08:00
|
|
|
ksize.height = 1;
|
2013-12-11 00:14:37 +08:00
|
|
|
if( size.width == 1 )
|
2010-05-12 01:44:00 +08:00
|
|
|
ksize.width = 1;
|
|
|
|
}
|
2012-01-05 02:20:03 +08:00
|
|
|
|
|
|
|
if( ksize.width == 1 && ksize.height == 1 )
|
|
|
|
{
|
2013-12-11 00:14:37 +08:00
|
|
|
_src.copyTo(_dst);
|
2012-01-05 02:20:03 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-24 22:34:02 +08:00
|
|
|
bool useOpenCL = (ocl::isOpenCLActivated() && _dst.isUMat() && _src.dims() <= 2 &&
|
2017-04-13 20:50:23 +08:00
|
|
|
((ksize.width == 3 && ksize.height == 3) ||
|
|
|
|
(ksize.width == 5 && ksize.height == 5)) &&
|
|
|
|
_src.rows() > ksize.height && _src.cols() > ksize.width);
|
2018-09-07 19:33:52 +08:00
|
|
|
CV_UNUSED(useOpenCL);
|
2012-01-05 02:20:03 +08:00
|
|
|
|
2017-11-01 21:45:07 +08:00
|
|
|
int sdepth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
|
|
|
|
|
2017-11-14 17:57:02 +08:00
|
|
|
Mat kx, ky;
|
|
|
|
createGaussianKernels(kx, ky, type, ksize, sigma1, sigma2);
|
|
|
|
|
|
|
|
CV_OCL_RUN(useOpenCL, ocl_GaussianBlur_8UC1(_src, _dst, ksize, CV_MAT_DEPTH(type), kx, ky, borderType));
|
|
|
|
|
|
|
|
CV_OCL_RUN(_dst.isUMat() && _src.dims() <= 2 && (size_t)_src.rows() > kx.total() && (size_t)_src.cols() > kx.total(),
|
|
|
|
ocl_sepFilter2D(_src, _dst, sdepth, kx, ky, Point(-1, -1), 0, borderType))
|
|
|
|
|
|
|
|
Mat src = _src.getMat();
|
|
|
|
Mat dst = _dst.getMat();
|
|
|
|
|
2017-11-01 21:45:07 +08:00
|
|
|
Point ofs;
|
|
|
|
Size wsz(src.cols, src.rows);
|
|
|
|
if(!(borderType & BORDER_ISOLATED))
|
|
|
|
src.locateROI( wsz, ofs );
|
|
|
|
|
2017-11-23 18:46:32 +08:00
|
|
|
CALL_HAL(gaussianBlur, cv_hal_gaussianBlur, src.ptr(), src.step, dst.ptr(), dst.step, src.cols, src.rows, sdepth, cn,
|
2017-11-13 22:43:32 +08:00
|
|
|
ofs.x, ofs.y, wsz.width - src.cols - ofs.x, wsz.height - src.rows - ofs.y, ksize.width, ksize.height,
|
2017-11-14 17:57:02 +08:00
|
|
|
sigma1, sigma2, borderType&~BORDER_ISOLATED);
|
2017-11-01 21:45:07 +08:00
|
|
|
|
2017-11-14 17:57:02 +08:00
|
|
|
CV_OVX_RUN(true,
|
2017-11-28 22:11:16 +08:00
|
|
|
openvx_gaussianBlur(src, dst, ksize, sigma1, sigma2, borderType))
|
2016-10-21 13:17:45 +08:00
|
|
|
|
2017-11-28 22:11:16 +08:00
|
|
|
CV_IPP_RUN_FAST(ipp_GaussianBlur(src, dst, ksize, sigma1, sigma2, borderType));
|
2016-10-21 13:17:45 +08:00
|
|
|
|
2018-09-03 23:39:42 +08:00
|
|
|
if(sdepth == CV_8U && ((borderType & BORDER_ISOLATED) || !_src.getMat().isSubmatrix()))
|
|
|
|
{
|
|
|
|
std::vector<ufixedpoint16> fkx, fky;
|
|
|
|
createGaussianKernels(fkx, fky, type, ksize, sigma1, sigma2);
|
|
|
|
if (src.data == dst.data)
|
|
|
|
src = src.clone();
|
2019-03-10 03:20:24 +08:00
|
|
|
CV_CPU_DISPATCH(GaussianBlurFixedPoint, (src, dst, (const uint16_t*)&fkx[0], (int)fkx.size(), (const uint16_t*)&fky[0], (int)fky.size(), borderType),
|
|
|
|
CV_CPU_DISPATCH_MODES_ALL);
|
2018-09-03 23:39:42 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-28 22:11:16 +08:00
|
|
|
sepFilter2D(src, dst, sdepth, kx, ky, Point(-1, -1), 0, borderType);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 03:20:24 +08:00
|
|
|
} // namespace
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
CV_IMPL void
|
|
|
|
cvSmooth( const void* srcarr, void* dstarr, int smooth_type,
|
|
|
|
int param1, int param2, double param3, double param4 )
|
|
|
|
{
|
|
|
|
cv::Mat src = cv::cvarrToMat(srcarr), dst0 = cv::cvarrToMat(dstarr), dst = dst0;
|
|
|
|
|
|
|
|
CV_Assert( dst.size() == src.size() &&
|
|
|
|
(smooth_type == CV_BLUR_NO_SCALE || dst.type() == src.type()) );
|
|
|
|
|
|
|
|
if( param2 <= 0 )
|
|
|
|
param2 = param1;
|
|
|
|
|
|
|
|
if( smooth_type == CV_BLUR || smooth_type == CV_BLUR_NO_SCALE )
|
|
|
|
cv::boxFilter( src, dst, dst.depth(), cv::Size(param1, param2), cv::Point(-1,-1),
|
|
|
|
smooth_type == CV_BLUR, cv::BORDER_REPLICATE );
|
|
|
|
else if( smooth_type == CV_GAUSSIAN )
|
|
|
|
cv::GaussianBlur( src, dst, cv::Size(param1, param2), param3, param4, cv::BORDER_REPLICATE );
|
|
|
|
else if( smooth_type == CV_MEDIAN )
|
|
|
|
cv::medianBlur( src, dst, param1 );
|
|
|
|
else
|
|
|
|
cv::bilateralFilter( src, dst, param1, param3, param4, cv::BORDER_REPLICATE );
|
|
|
|
|
|
|
|
if( dst.data != dst0.data )
|
|
|
|
CV_Error( CV_StsUnmatchedFormats, "The destination image does not have the proper type" );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* End of file. */
|