Ask Your Question
0

Can we use OpenCV to get a Gaussian Filter with size of (w,h) [closed]

asked 2020-06-10 04:34:45 -0600

Eric Song gravatar image

Hi guys, I am learning about Gaussian Distribution these days, and I want to know if we Can use OpenCV to get a Gaussian Filter with size of (w,h). After searching in the documentation, I found there is a function of cv.getGaussianKernel(), which can produce a Gaussian Filter of ksize. But this filter has a same width and heigh, which in a square shape. So does OpenCV provide other function which can produce a Gaussian Filter with size of (w,h)?

Your answer and idea will be appreciated!

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by supra56
close date 2020-06-11 07:27:35.709817

2 answers

Sort by ยป oldest newest most voted
0

answered 2020-06-10 04:48:24 -0600

berak gravatar image

updated 2020-06-10 04:57:51 -0600

if you read docs , it's actually an 1d filter (a single row or col in a 2d Mat), suitable for sepFilter2D() see:

>>> x = cv2.getGaussianKernel(3,1.2)
array([[0.29281452],
       [0.41437096],
       [0.29281452]])
>>> y = cv2.getGaussianKernel(5,1.2)
array([[0.08562916],
       [0.2426676 ],
       [0.34340648],
       [0.2426676 ],
       [0.08562916]])

so, if you need a "rectangular" kernel, you would apply 2 gaussians with different size

 blurred = cv.sepFilter2D(img, -1,  x, y)

you could also use the outer product of the kernels, to produce a 2d filter matrix:

kxy = x.T * y

and use filter2D(), but this is clearly less optimal than the seperated version

edit flag offensive delete link more

Comments

1

Oh, I am sorry for my absent knowledge and experience about that. So does OpenCV provide a function producing a 2D-Gaussian Distribution in the shape of (w,h)?

Eric Song gravatar imageEric Song ( 2020-06-10 04:59:54 -0600 )edit
1

Thanks sincerely for your answer and guide!

Eric Song gravatar imageEric Song ( 2020-06-10 05:45:50 -0600 )edit
1

answered 2020-06-10 05:54:15 -0600

supra56 gravatar image

updated 2020-06-10 05:54:56 -0600

@Eric Song. Yes, you can do that:

kernel_height = cv2.getGaussianKernel(frame.shape[0], frame.shape[0] / 2)
Kernel_width = cv2.getGaussianKernel(frame.shape[1], frame.shape[1] / 2)
w_h = np.dot(kernel_heigh,  np.transpose(Kernel_width))
m = 1  / np.max(w_h)

You can change value int or float / 2

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-06-10 04:34:45 -0600

Seen: 411 times

Last updated: Jun 10 '20