scikit-image Gabor滤波器错误:`滤波器权重数组具有不正确的形状`

时间:2015-12-11 22:33:10

标签: python arrays numpy scikit-image

输入是灰度图像,转换为130x130 numpy矩阵。我总是得到错误:

Traceback (most recent call last):
  File "test_final.py", line 87, in <module>
    a._populate_gabor()

  File "C:\Users\Bears\Dropbox\School\Data Science\final.py", line 172, in _populate_gabor
    self.gabor_imgs[i] = self._matrix_2_1d(self._gabor_this(self.grey_imgs[i]),kernels[0])

  File "C:\Users\Bears\Dropbox\School\Data Science\final.py", line 179, in _gabor_this
    filtered = ndi.convolve(image, kernel, mode='reflect')

  File "C:\Users\Bears\Anaconda3\lib\site-packages\scipy\ndimage\filters.py", line 696, in convolve
    origin, True)

  File "C:\Users\Bears\Anaconda3\lib\site-packages\scipy\ndimage\filters.py", line 530, in _correlate_or_convolve
    raise RuntimeError('filter weights array has incorrect shape.')
RuntimeError: filter weights array has incorrect shape.

我的代码如下

def _populate_gabor(self):
    kernels = []
    for theta in range(self.gabor_range[0],self.gabor_range[1]):
        theta = theta / 4. * np.pi
        for sigma in (1, 3):
            for frequency in (0.05, 0.25):
                kernel = np.real(gabor_kernel(frequency, theta=theta,
                                      sigma_x=sigma, sigma_y=sigma))
                kernels.append(kernel)
    print (len(kernels))

    for i in range(self.length):
        self.gabor_imgs[i] = self._matrix_2_1d(self._gabor_this(self.grey_imgs[i]),kernels[0])


def _gabor_this(image, kernels): 
    feats = np.zeros((len(kernels), 2), dtype=np.double)
    for k, kernel in enumerate(kernels):
        filtered = ndi.convolve(image, kernel, mode='reflect')
        feats[k, 0] = filtered.mean()
        feats[k, 1] = filtered.var()
    return feats

我直接从http://scikit-image.org/docs/dev/auto_examples/plot_gabor.html的示例中获取此代码,但我无法弄清楚如何解决此错误。任何帮助,将不胜感激。 请注意,所有其他功能都在使用其他过滤器,而不是gabor。

1 个答案:

答案 0 :(得分:4)

好像你正在使用scipy的'ndimage.convolve'函数。请记住,ndimage提供“N”维卷积。因此,如果您希望卷积起作用,则图像和内核必须具有相同数量的维度。其中任何一个尺寸不正确都会导致您说出错误。

从上面的评论中,内核(4,4,7)无法与图像(130,130)进行卷积。尝试在卷积之前添加单个维度,然后在之后将其删除。

long