Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Scipy ndimage.gaussian_filter throwing runtime error

What I am trying to do:

  1. Read an image in python.
  2. Apply Gaussian filter using Scipy's ndimage.gaussian_filter() function.
  3. Display the resultant image.

Here is the code that I am trying to run:

import cv2
from matplotlib import pyplot as plt
import scipy.ndimage as ndimage

img = cv2.imread('lena.png', 0)
img = ndimage.gaussian_filter(img, sigma=(5, 5, 0), order=0)
plt.imshow(img, cmap='gray', interpolation='bicubic')
plt.show()

What the problem is:

I am getting the following error:

RuntimeError: sequence argument must have length equal to input rank

The complete stack trace is:

Traceback (most recent call last):
  File "/Users/guest/stackoverflow.py", line 6, in <module>
    img = ndimage.gaussian_filter(img, sigma=(5, 5, 0), order=0)
  File "/Users/guest/anaconda/envs/MyEnv/lib/python3.5/site-packages/scipy/ndimage/filters.py", line 346, in gaussian_filter
    sigmas = _ni_support._normalize_sequence(sigma, input.ndim)
  File "/Users/sguest/anaconda/envs/MyEnv/lib/python3.5/site-packages/scipy/ndimage/_ni_support.py", line 65, in _normalize_sequence
    raise RuntimeError(err)
RuntimeError: sequence argument must have length equal to input rank

Here is the image I am trying to process: leng.png

like image 781
Trevor Track Avatar asked Jan 29 '26 21:01

Trevor Track


1 Answers

According to the stack trace, the error "sequence argument must have length equal to input rank" is thrown by the line img = ndimage.gaussian_filter(img, sigma=(5, 5, 0), order=0)

sigma is a sequence argument, and you're expected to give one value per image dimension (this is the "input rank" mentioned in the error message).

Apparently, the statement img = cv2.imread('lena.png', 0) returns a 2D array (the 0 argument tells imread to convert the image to grey-value). Thus, gaussian_filter needs 2 values for sigma, not 3.

like image 193
Cris Luengo Avatar answered Feb 01 '26 11:02

Cris Luengo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!