【问题标题】:Python Scipy ndimage.gaussian_filter throwing runtime errorPython Scipy ndimage.gaussian_filter 抛出运行时错误
【发布时间】:2018-08-05 03:22:36
【问题描述】:

我想做什么:

  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/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

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

【问题讨论】:

  • 我不知道这个函数,但从错误消息中我猜img 是 2D 但你给出了 3 个值,或者 order 也必须有 3 个值。
  • @CrisLuengo 的猜测是正确的。通过在第二个参数中输入 0,您告诉 imread 将图像转换为灰度。所以img 是一个二维数组。
  • @CrisLuengo,你是对的。将 sigma 更改为 sigma=(5, 5) 解决了它。您能否将其添加为这个问题的答案?

标签: python image-processing scipy computer-vision gaussian


【解决方案1】:

根据堆栈跟踪,img = ndimage.gaussian_filter(img, sigma=(5, 5, 0), order=0) 行抛出错误“sequence argument must have length equal to input rank”

sigma 是一个序列参数,您应该为每个图像维度提供一个值(这是错误消息中提到的“输入等级”)。

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

【讨论】:

    猜你喜欢
    • 2011-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-06
    • 2012-05-03
    • 2013-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多