Carlo
Carlo

Reputation: 1578

cv2.filter2D: no results if kernel size is too big

I'm trying to learn how to use OpenCV, and following the basic tutorials. Testing the simple cv2.filter2D, I'm experiencing a problem...if the kernel size is higher than a certain number, the program won't produce any output.

So, I'm writing something like:

img = cv2.imread('images/img.jpg')

kernel = np.ones((n,n),np.float32)/n**2
smoothed = cv2.filter2D(img,-1,kernel)

cv2.imshow('orig', img)
cv2.imshow('smoothed', smoothed)
cv2.waitKey(0)
cv2.destroyAllWindows()

and the problem is that, for n>11, the smoothed image is not produced (I tried to save it or show it, but I got no results). Tried with various images of different sizes, but always with the same result. What am I doing wrong here? Thanks!

EDIT: Ok, short update.
The script works normally, and produces the blurred image, when I execute it from the terminal (I'm using Ubuntu 18.04 with OpenCv 4.1.0). The problem is when I try to execute it in Pycharm, and in this case it throws the error "Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)", which should indicate some sort of...segmentation problem, as far as I understand? Sorry for the initial vagueness, I was in a little bit of a hurry and didn't notice the Pycharm message!

EDIT: New update.
As crazy as it sounds, I tried to run the script both from terminal and from Pycharm, and now it doesn't work from either of them! I honestly have no clue how this is possible, as far as I remember I didn't change anything significant! Anyway, if it can still be helpful, when running from terminal the sys.path is:

['/home/lews/PycharmProjects/opencv/00_basics',
'/home/lews/PycharmProjects/tf_models/research',
'/home/lews/PycharmProjects/tf_models/research/slim',
'/home/lews/PycharmProjects/opencv/00_basics',
'/home/lews/anaconda3/envs/tf2/lib/python37.zip',
'/home/lews/anaconda3/envs/tf2/lib/python3.7',
'/home/lews/anaconda3/envs/tf2/lib/python3.7/lib-dynload',
'/home/lews/anaconda3/envs/tf2/lib/python3.7/site-packages']

while from Pycharm is:

['/home/lews/PycharmProjects/opencv/00_basics',
'/snap/pycharm-professional/159/helpers/pydev',
'/home/lews/PycharmProjects/opencv',
'/snap/pycharm-professional/159/helpers/pycharm_display',
'/snap/pycharm-professional/159/helpers/third_party/thriftpy',
'/snap/pycharm-professional/159/helpers/pydev',
'/home/lews/anaconda3/envs/tf2/lib/python37.zip',
'/home/lews/anaconda3/envs/tf2/lib/python3.7',
'/home/lews/anaconda3/envs/tf2/lib/python3.7/lib-dynload',
'/home/lews/anaconda3/envs/tf2/lib/python3.7/site-packages',
'/snap/pycharm-professional/159/helpers/pycharm_matplotlib_backend',
'/home/lews/anaconda3/envs/tf2/lib/python3.7/site-packages/IPython/extensions',
'/home/lews/PycharmProjects/opencv']

Again, neither of them work, in both cases the blurred image isn't produced. When running from terminal, it returns the error:

Segmentation fault (core dumped)

while with Pycharm, as I said, I get

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

Upvotes: 0

Views: 687

Answers (1)

gavinb
gavinb

Reputation: 20018

A kernel of size n=11 is huge, and computationally incredibly expensive to apply to an image. There are typically upper limits on kernel sizes for convolutions to avoid unreasonably long processing times.

According to the documentation:

The function uses the DFT-based algorithm in case of sufficiently large kernels (~11 x 11 or larger) and the direct algorithm (that uses the engine retrieved by createLinearFilter() ) for small kernels.

This implies that it should produce some output, albeit via the DFT algorithm. Perhaps the DFT alternative doesn't support float32 pixel type?

Upvotes: 1

Related Questions