利用OpenCV获取二维高斯核(Python实现)

文档链接在此:getGaussianKernel(int ksize, double sigma, int ktype=CV_64F)

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 ) (ksizemod2=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 ksize×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} Gi=αe(i(ksize1)/2)2/(2sigma)2 where i = 0.. ksize − 1 i=0..\texttt{ksize}-1 i=0..ksize1 and \alpha is the scale factor chosen so that ∑ i G i = 1 \sum_i G_i=1 iGi=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
    评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:游动-白 设计师:我叫白小胖 返回首页
评论

打赏作者

Ddsluke

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值