'scikit-image Gabor filter error: `filter weights array has incorrect shape`
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.
Solution 1:[1]
Here is an exemple on how to use ndimage.convole to apply a filter on a RGB image (3 channels image):
import cv2
import numpy as np
from PIL import Image
from pylab import plt
from scipy import ndimage
# Partly based on https://stackoverflow.com/questions/6094957/high-pass-filter-for-image-processing-in-python-by-using-scipy-numpy
# Load an rgb image
img_in = np.array(Image.open("a_256x256_rgb_image.bmp")).reshape(256, 256, 3)
rgb_kernel = np.ones((5, 5, 3))
# A highpass filter
single_chan_kernel = np.array(
[
[-1, -1, -1, -1, -1], # nopep8
[-1, 1, 2, 1, -1], # nopep8
[-1, 2, 4, 2, -1], # nopep8
[-1, 1, 2, 1, -1], # nopep8
[-1, -1, -1, -1, -1],
] # nopep8
) # nopep8
rgb_kernel[:, :, 0] = single_chan_kernel
rgb_kernel[:, :, 1] = single_chan_kernel
rgb_kernel[:, :, 2] = single_chan_kernel
filtered_img = ndimage.convolve(img_in, rgb_kernel)
im = plt.imshow(filtered_img)
plt.show()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Greg7000 |
