渐变适用于一张照片,但不适用于其他照片

时间:2014-09-30 03:00:07

标签: python scipy scikit-image

我正在尝试使用Scipy指南跟踪图像分析并弄乱事物,但是当我更改图像时,很多事情都不起作用。例如,

import skdemo
from skimage import data
# Rename module so we don't shadow the builtin function
import skimage.filter as filters

image = data.camera()
pixelated = image[::10, ::10]
gradient = filters.sobel(pixelated)
skdemo.imshow_all(pixelated, gradient)

当我运行它时,它可以工作,但当我使用data.coffee()data.chelsea()时,我会遇到大量错误。每当我使用convolve函数时,也会发生这种情况。知道为什么吗?

RuntimeError                              Traceback (most recent call last)
<ipython-input-148-2dc2336cd0ef> in <module>()
  6 image = data.coffee()
  7 pixelated = image[::10, ::10]
----> 8 gradient = filters.sobel(pixelated)
  9 skdemo.imshow_all(pixelated, gradient)

/Users/(me)/anaconda/lib/python2.7/site-packages/skimage/filter/edges.pyc in sobel(image, mask)
 81     has to be further processed to perform edge detection.
 82     """
---> 83     return np.sqrt(hsobel(image, mask)**2 + vsobel(image, mask)**2)
 84 
 85 

/Users/(me)/anaconda/lib/python2.7/site-packages/skimage/filter/edges.pyc in hsobel(image, mask)
112     """
113     image = img_as_float(image)
--> 114     result = np.abs(convolve(image, HSOBEL_WEIGHTS))
115     return _mask_filter_result(result, mask)
116 

/Users/(me)/anaconda/lib/python2.7/site-packages/scipy/ndimage/filters.pyc in convolve(input, weights, output, mode, cval, origin)
693     """
694     return _correlate_or_convolve(input, weights, output, mode, cval,
--> 695                                   origin, True)
696 
697 

/Users/(me)/anaconda/lib/python2.7/site-packages/scipy/ndimage/filters.pyc in _correlate_or_convolve(input, weights, output, mode, cval, origin, convolution)
527     wshape = [ii for ii in weights.shape if ii > 0]
528     if len(wshape) != input.ndim:
--> 529         raise RuntimeError('filter weights array has incorrect shape.')
530     if convolution:
531         weights = weights[tuple([slice(None, None, -1)] * weights.ndim)]

RuntimeError: filter weights array has incorrect shape.

1 个答案:

答案 0 :(得分:2)

sobel需要一个二维数组。 skimage.data.coffee()skimage.data.chelsea()返回的数组是三维的,形状为(m,n,3)。它们代表彩色图像,有红色,绿色和蓝色通道。

要将其中一个与演示代码一起使用,您可以选择其中一个频道。例如,以下工作:

image = data.coffee()
pixelated = image[::10, ::10, 0]  # Use the red channel.
gradient = filters.sobel(pixelated)
相关问题