简体   繁体   English

如何在 python 中使用 kernel [3,3] 实现高斯滤波器?

[英]how do I implement Gaussian filter with kernel [3,3] in python?

I need to implement Gaussian filter 2d with kernel size [3,3] in python, but I do not know how can I do this?我需要在 python 中使用 kernel 大小 [3,3] 实现高斯滤波器 2d,但我不知道该怎么做? I use this method in Matlab:我在 Matlab 中使用这种方法:

G = fspecial('gaussian',[3 3],0.5);
Ig = imfilter(watermarkImage,G,'same');

but in python, we have some function like this但是在 python 中,我们有一些像这样的 function

blurred_img = gaussian_filter(img, Q, mode='reflect')

that Q is the std and I do not know how can I produce a blurred image with kernel [3,3]. Q是标准,我不知道如何使用 kernel [3,3] 生成模糊图像。 could you please help me with this issue?你能帮我解决这个问题吗?

If OpenCV is option then it has function for this, namely cv2.GaussianBlur it does accept width and height of the kernel (both should be odd and positive) and standard deviation, so using kernel size [3,3] and deviation 0.5, would be as follow: If OpenCV is option then it has function for this, namely cv2.GaussianBlur it does accept width and height of the kernel (both should be odd and positive) and standard deviation, so using kernel size [3,3] and deviation 0.5, would如下:

blur = cv2.GaussianBlur(img,(3,3),0.5)

where img is array representing image, possibly created by using cv2.imread function.其中img是表示图像的数组,可能是使用cv2.imread function 创建的。

scipy.ndimage.gaussian_filter has the argument truncate , which sets the filter size (truncation) in sigma . scipy.ndimage.gaussian_filter有参数truncate ,它在sigma中设置过滤器大小(截断)。 Your sigma here is 0.5, and assuming 3 x 3 is symmetrical around the centre, that would mean it truncates at 3/2 = 1.5 = 3 sigma.你的 sigma 是 0.5,假设 3 x 3 围绕中心对称,这意味着它会在 3/2 = 1.5 = 3 sigma 处截断。 So you could use gaussian_filter(img, 0.5, order=0, truncate=3.0) and see if you get the same result as your matlab code.因此,您可以使用gaussian_filter(img, 0.5, order=0, truncate=3.0)并查看是否得到与 matlab 代码相同的结果。

Note that the documentation for fspecial , for the case of a Gaussian filter, mentions it is not recommended to use this function, but to use imgaussfilt or imgaussfilt3 instead.请注意,对于高斯滤波器的情况, fspecial的文档提到不建议使用此 function,而是使用imgaussfiltimgaussfilt3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM