如何产生以下图像(gabor patches)
How to produce the following images (gabor patches)
我正在尝试创建四个 gabor 补丁,与下面的非常相似。
我不需要它们与下面的图片相同,但相似。
尽管进行了一些修改,但我无法重现这些图像...
我相信它们最初是在 MATLAB 中创建的。我无法访问原始 MATLAB 代码。
我在 python (2.7.10) 中有以下代码:
import numpy as np
from scipy.misc import toimage # One can also use matplotlib*
data = gabor_fn(sigma = ???, theta = 0, Lambda = ???, psi = ???, gamma = ???)
toimage(data).show()
*graphing a numpy array with matplotlib
gabor_fn,来自here,定义如下:
def gabor_fn(sigma,theta,Lambda,psi,gamma):
sigma_x = sigma;
sigma_y = float(sigma)/gamma;
# Bounding box
nstds = 3;
xmax = max(abs(nstds*sigma_x*numpy.cos(theta)),abs(nstds*sigma_y*numpy.sin(theta)));
xmax = numpy.ceil(max(1,xmax));
ymax = max(abs(nstds*sigma_x*numpy.sin(theta)),abs(nstds*sigma_y*numpy.cos(theta)));
ymax = numpy.ceil(max(1,ymax));
xmin = -xmax; ymin = -ymax;
(x,y) = numpy.meshgrid(numpy.arange(xmin,xmax+1),numpy.arange(ymin,ymax+1 ));
(y,x) = numpy.meshgrid(numpy.arange(ymin,ymax+1),numpy.arange(xmin,xmax+1 ));
# Rotation
x_theta=x*numpy.cos(theta)+y*numpy.sin(theta);
y_theta=-x*numpy.sin(theta)+y*numpy.cos(theta);
gb= numpy.exp(-.5*(x_theta**2/sigma_x**2+y_theta**2/sigma_y**2))*numpy.cos(2*numpy.pi/Lambda*x_theta+psi);
return gb
如您所知,图像之间的唯一区别(我相信)是对比度。因此,gabor_fn
可能需要进行更改以允许这样做(除非我误解了其中一个参数)...我只是不确定如何。
更新:
from math import pi
from matplotlib import pyplot as plt
data = gabor_fn(sigma=5.,theta=pi/2.,Lambda=12.5,psi=90,gamma=1.)
unit = #From left to right, unit was set to 1, 3, 7 and 9.
bound = 0.0009/unit
fig = plt.imshow(
data
,cmap = 'gray'
,interpolation='none'
,vmin = -bound
,vmax = bound
)
plt.axis('off')
似乎 toimage
缩放输入数据,以便 min/max 值映射到 black/white。
我不知道 gabor 补丁的合理预期幅度是多少,但你应该尝试这样的事情:
toimage(data, cmin=-1, cmax=1).show()
这告诉 toimage
您的数据在什么范围内。您可以尝试使用 cmin 和 cmax,但要确保它们是对称的(即 cmin=-x, cmax=x
),以便值为 0映射为灰色。
您遇到的问题是可视化问题(不过,我认为您选择的参数太大)。
默认情况下,matplotlib 和 scipy 的 (toimage) 使用双线性(或三线性)插值,具体取决于您的 matplotlib 的配置脚本。这就是为什么您的图像看起来如此流畅。这是因为您的像素值正在被插值,并且您没有显示刚刚计算的原始内核。
尝试使用不带插值的 matplotlib:
from matplotlib import pyplot as plt
plt.imshow(data, 'gray', interpolation='none')
plt.show()
对于以下参数:
data = gabor_fn(sigma=5.,theta=pi/2.,Lambda=25.,psi=90,gamma=1.)
你得到这个输出:
如果你将 lamda 减少到 15
,你会得到这样的结果:
此外,您选择的 sigma 会改变平滑的强度,将参数 vmin=-1
和 vmax=1
添加到 imshow
(类似于@kazemakase)的建议,会给您所需 对比度.
查看此 指南 以获得 gabor 内核的合理值(和使用方法):
http://scikit-image.org/docs/dev/auto_examples/plot_gabor.html
我正在尝试创建四个 gabor 补丁,与下面的非常相似。 我不需要它们与下面的图片相同,但相似。
尽管进行了一些修改,但我无法重现这些图像... 我相信它们最初是在 MATLAB 中创建的。我无法访问原始 MATLAB 代码。
我在 python (2.7.10) 中有以下代码:
import numpy as np
from scipy.misc import toimage # One can also use matplotlib*
data = gabor_fn(sigma = ???, theta = 0, Lambda = ???, psi = ???, gamma = ???)
toimage(data).show()
*graphing a numpy array with matplotlib
gabor_fn,来自here,定义如下:
def gabor_fn(sigma,theta,Lambda,psi,gamma):
sigma_x = sigma;
sigma_y = float(sigma)/gamma;
# Bounding box
nstds = 3;
xmax = max(abs(nstds*sigma_x*numpy.cos(theta)),abs(nstds*sigma_y*numpy.sin(theta)));
xmax = numpy.ceil(max(1,xmax));
ymax = max(abs(nstds*sigma_x*numpy.sin(theta)),abs(nstds*sigma_y*numpy.cos(theta)));
ymax = numpy.ceil(max(1,ymax));
xmin = -xmax; ymin = -ymax;
(x,y) = numpy.meshgrid(numpy.arange(xmin,xmax+1),numpy.arange(ymin,ymax+1 ));
(y,x) = numpy.meshgrid(numpy.arange(ymin,ymax+1),numpy.arange(xmin,xmax+1 ));
# Rotation
x_theta=x*numpy.cos(theta)+y*numpy.sin(theta);
y_theta=-x*numpy.sin(theta)+y*numpy.cos(theta);
gb= numpy.exp(-.5*(x_theta**2/sigma_x**2+y_theta**2/sigma_y**2))*numpy.cos(2*numpy.pi/Lambda*x_theta+psi);
return gb
如您所知,图像之间的唯一区别(我相信)是对比度。因此,gabor_fn
可能需要进行更改以允许这样做(除非我误解了其中一个参数)...我只是不确定如何。
更新:
from math import pi
from matplotlib import pyplot as plt
data = gabor_fn(sigma=5.,theta=pi/2.,Lambda=12.5,psi=90,gamma=1.)
unit = #From left to right, unit was set to 1, 3, 7 and 9.
bound = 0.0009/unit
fig = plt.imshow(
data
,cmap = 'gray'
,interpolation='none'
,vmin = -bound
,vmax = bound
)
plt.axis('off')
似乎 toimage
缩放输入数据,以便 min/max 值映射到 black/white。
我不知道 gabor 补丁的合理预期幅度是多少,但你应该尝试这样的事情:
toimage(data, cmin=-1, cmax=1).show()
这告诉 toimage
您的数据在什么范围内。您可以尝试使用 cmin 和 cmax,但要确保它们是对称的(即 cmin=-x, cmax=x
),以便值为 0映射为灰色。
您遇到的问题是可视化问题(不过,我认为您选择的参数太大)。
默认情况下,matplotlib 和 scipy 的 (toimage) 使用双线性(或三线性)插值,具体取决于您的 matplotlib 的配置脚本。这就是为什么您的图像看起来如此流畅。这是因为您的像素值正在被插值,并且您没有显示刚刚计算的原始内核。
尝试使用不带插值的 matplotlib:
from matplotlib import pyplot as plt
plt.imshow(data, 'gray', interpolation='none')
plt.show()
对于以下参数:
data = gabor_fn(sigma=5.,theta=pi/2.,Lambda=25.,psi=90,gamma=1.)
你得到这个输出:
如果你将 lamda 减少到 15
,你会得到这样的结果:
此外,您选择的 sigma 会改变平滑的强度,将参数 vmin=-1
和 vmax=1
添加到 imshow
(类似于@kazemakase)的建议,会给您所需 对比度.
查看此 指南 以获得 gabor 内核的合理值(和使用方法):
http://scikit-image.org/docs/dev/auto_examples/plot_gabor.html