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的转置矩阵

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

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
斯滤波是一种常用的图像滤波方法,用于平滑图像并减少噪声。在OpenCV中,可以使用cv2.GaussianBlur()函数来实现斯滤波。该函数的语法格式为:dst = cv2.GaussianBlur(src, ksize, sigmaX, sigmaY, borderType)。其中,src是输入图像,ksize是滤波核的大小,sigmaX和sigmaY是斯核在X和Y方向上的标准差,borderType是边界处理方式。\[1\]\[3\] 下面是一个示例代码,演示了如何使用cv2.GaussianBlur()函数对噪声图像进行斯滤波并显示结果: ```python import cv2 o = cv2.imread("image\\lenaNoise.png") r = cv2.GaussianBlur(o, (5, 5), 0, 0) cv2.imshow("original", o) cv2.imshow("result", r) cv2.waitKey() cv2.destroyAllWindows() ``` 在这个示例中,我们首先读取了一个带有噪声的图像o,然后使用cv2.GaussianBlur()函数对其进行斯滤波,滤波核的大小为(5, 5),sigmaX和sigmaY都为0,表示使用自动计算的标准差。最后,我们通过cv2.imshow()函数显示原始图像和滤波结果。\[2\] #### 引用[.reference_title] - *1* [关于斯模糊与opencv中的GaussianBlur函数](https://blog.csdn.net/vbLittleBoy/article/details/9187447)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [win10+Python3.7.3+OpenCV3.4.1入门学习(七)————7.3斯滤波](https://blog.csdn.net/qq_43069920/article/details/103344514)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值