OpenCV总结——高斯核

OpenCV提供了一些滤波需要的核,虽然没有函数,但是可以使用卷积函数结合核进行计算。这里总结高斯核函数。

getGaussianKernel
Returns Gaussian filter coefficients.

C++: Mat getGaussianKernel(int ksize, double sigma, int ktype=CV_64F )
Python: cv2.getGaussianKernel(ksize, sigma[, ktype]) → retval
Parameters:	
ksize – Aperture size. It should be odd ( \texttt{ksize} \mod 2 = 1 ) and positive.
sigma – Gaussian standard deviation. If it is non-positive, it is computed from ksize as sigma = 0.3*((ksize-1)*0.5 - 1) + 0.8 .
ktype – Type of filter coefficients. It can be CV_32F or CV_64F .
The function computes and returns the \texttt{ksize} \times 1 matrix of Gaussian filter coefficients:

G_i= \alpha *e^{-(i-( \texttt{ksize} -1)/2)^2/(2* \texttt{sigma} )^2},

where i=0..\texttt{ksize}-1 and \alpha is the scale factor chosen so that \sum_i G_i=1.

Two of such generated kernels can be passed to sepFilter2D(). Those functions automatically recognize smoothing kernels (a symmetrical kernel with sum of weights equal to 1) and handle them accordingly. You may also use the higher-level GaussianBlur().

See also sepFilter2D(), getDerivKernels(), getStructuringElement(), GaussianBlur()

上方是OpenCV给出的函数定义,可以通过设置核的大小以及sigma的值确定一个高斯核,但是这个函数只可以获得一维的高斯核,如果需要使用二维高斯核需要进行一下简单的处理。

Mat keX = getGaussianKernel(3, 1.5);//函数只可以获得一维的高斯核,需要进行处理
Mat keY = getGaussianKernel(3, 1.5);
Mat kernel = keX*keY.t();//通过矩阵乘法,x乘以y的转置矩阵

通过上面的计算可以将一维的高斯核转化为二维的高斯核。

你可能感兴趣的:(OpenCV)