2

I want to use a fast blurring function like cv2.GaussianBlur() but it only allows for rectangular kernel shapes. Is there a way to use elliptic kernel shape? If any other blur function allows it, can you please suggest that function?

  • The Gaussian filter is perfectly isotropic (==rotation invariant). The footprint of the kernel is irrelevant, as long as it's taken large enough w.r.t. the sigma parameter of the Gaussian. – Cris Luengo Nov 13 '19 at 23:02

1 Answers1

3

Instead of relying on built-in kernels or the built-in functions of opencv, use cv2.filter2D() function to apply a custom kernel of your choice with the values that you have chosen! With this you can apply not just "elliptic" kernel that you mentioned, but any kernel you want.

Here is the usage:

import cv2
import numpy as np

img = cv2.imread('image.png')

kernel = np.ones((5,5),np.float32)/25
dst = cv2.filter2D(img,-1,kernel)

So in the above code, a kernel that looks like this:

enter image description here

is used.

Now if you want an "elliptical" kernel, you can either manually build an np array (i.e kernel) with custom values in every row and column or you can use cv2.getStructuringElement function of cv2 to build the elliptical kernel for you and then you can use this kernel in filter2D() function.

cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))

The above function will print:

[[0, 0, 1, 0, 0],
 [1, 1, 1, 1, 1],
 [1, 1, 1, 1, 1],
 [1, 1, 1, 1, 1],
 [0, 0, 1, 0, 0]]

which is your elliptical kernel!

Sushanth
  • 2,224
  • 13
  • 29
  • Thank you! I had to play with the values of kernel in the `cv2.getStructuringElement` since it multiplies the values of array elements with the sum of kernel elements, in this case it made 1's equal to 17's so I divided the kernel to sum of elements. It seems working. – Onur Dasdemir Nov 14 '19 at 19:49