# 利用OpenCV获取二维高斯核（Python实现）

1 篇文章 0 订阅
13 篇文章 0 订阅

Python:
cv2.getGaussianKernel(ksize, sigma[, ktype]) → retval
Parameters:

• ksize – Aperture size. It should be odd ( ksize m o d    2 = 1 ) ( \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 ksize × 1 \texttt{ksize} \times 1 matrix of Gaussian filter coefficients: G i = α ∗ e − ( i − ( ksize − 1 ) / 2 ) 2 / ( 2 ∗ sigma ) 2 G_i= \alpha *e^{-(i-( \texttt{ksize} -1)/2)^2/(2* \texttt{sigma} )^2} where i = 0.. ksize − 1 i=0..\texttt{ksize}-1 and \alpha is the scale factor chosen so that ∑ i G i = 1 \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().

>>> import cv2 as cv
>>> cv.getGaussianKernel(3, 1)
array([[0.27406862],
[0.45186276],
[0.27406862]])


>>> def gaussian_kernel_2d(ksize, sigma):
return cv.getGaussianKernel(ksize, sigma) * cv.getGaussianKernel(ksize,sigma).T

>>> gaussian_kernel_2d(3,1)
array([[0.07511361, 0.1238414 , 0.07511361],
[0.1238414 , 0.20417996, 0.1238414 ],
[0.07511361, 0.1238414 , 0.07511361]])

• 3
点赞
• 1
收藏
• 打赏
• 0
评论
10-18 5398
09-23 1002
06-11 3933
07-05 6235
05-01 4万+
09-14 1万+
01-02 813
12-12 3万+
06-12 412

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

Ddsluke

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。