david
david

Reputation: 1265

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? I use this method in Matlab:

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

but in python, we have some function like this

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]. could you please help me with this issue?

Upvotes: 1

Views: 1513

Answers (2)

9769953
9769953

Reputation: 12241

scipy.ndimage.gaussian_filter has the argument truncate, which sets the filter size (truncation) in 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. 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.

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.

Upvotes: 1

Daweo
Daweo

Reputation: 36765

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:

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

where img is array representing image, possibly created by using cv2.imread function.

Upvotes: 1

Related Questions