scikit-image Gabor过滤器错误:“过滤器权重数组的形状不正确" [英] scikit-image Gabor filter error: `filter weights array has incorrect shape`

查看:93
本文介绍了scikit-image Gabor过滤器错误:“过滤器权重数组的形状不正确"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Input is a greyscale image, converted to a 130x130 numpy matrix. I always get the error:

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一起使用.

I took this code directly from the example at http://scikit-image.org/docs/dev/auto_examples/plot_gabor.html and I can't figure out how to get around this error. Any help would be appreciated. Note that all the other functions are working with other filters, just not gabor.

推荐答案

似乎您正在使用scipy中的"ndimage.convolve"功能.请记住,ndimage提供了"N"维卷积.因此,如果要使卷积起作用,则图像和内核都必须具有相同数量的维.其中任何一个尺寸不正确都会引起您所描述的错误.

Seems like you are using 'ndimage.convolve' function from scipy. Remember that ndimage provides a "N" Dimensional convolution. So if you want the convolution to work, both the image and the kernel must have the same number of dimensions. Any one of them with incorrect dimension will cause error you have descirbed.

根据上面的评论,内核(4,4,7)无法与image(130,130)卷积.尝试在卷积之前添加单例维度,然后再将其删除.

From you comment above , kernel (4,4,7) cannot be convolved with and image (130,130). Try adding a singleton dimension before convolution and then removing it afterwards.

img = np.zeros(shape=(130,130),dtype=np.float32)
img = img[:,:,None] # Add singleton dimension
result = convolve(img,kernel)
finalOutput = result.squeeze() # Remove singleton dimension

这篇关于scikit-image Gabor过滤器错误:“过滤器权重数组的形状不正确"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆