Python Scipy ndimage.gaussian_filter 抛出运行时错误

Python Scipy ndimage.gaussian_filter throwing runtime error

我想做什么:

  1. 读取 python 中的图像。
  2. 使用 Scipy 的 ndimage.gaussian_filter() 函数应用高斯滤波器。
  3. 显示生成的图像。

这是我正在尝试的代码 运行:

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()

问题是什么:

我收到以下错误:

RuntimeError: sequence argument must have length equal to input rank

完整的堆栈跟踪是:

Traceback (most recent call last):
  File "/Users/guest/Whosebug.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

这是我要处理的图像: leng.png

根据堆栈跟踪,第 img = ndimage.gaussian_filter(img, sigma=(5, 5, 0), order=0)

行抛出了错误 "sequence argument must have length equal to input rank"

sigma 是一个序列参数,您应该为每个图像维度提供一个值(这是错误消息中提到的 "input rank")。

显然,语句 img = cv2.imread('lena.png', 0) returns 是一个二维数组(0 参数告诉 imread 将图像转换为 grey-value)。因此,gaussian_filter 需要 sigma 的 2 个值,而不是 3.