scikit-image Gabor filter error: `filter weights array has incorrect shape`

我只是一个虾纸丫 提交于 2019-12-11 13:47:55

问题


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.

my code is as follows

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

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.


回答1:


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.

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


来源:https://stackoverflow.com/questions/34233642/scikit-image-gabor-filter-error-filter-weights-array-has-incorrect-shape

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!