lil*_*lla 1 gaussian scipy imagefilter psychopy gaussianblur
对于实验,我需要在整个窗口(例如显示的中间部分)上使用高斯滤波器。由于我使用 PsychoPy,基本上,我需要一个 N x M 数组(N 和 M 是窗口的像素大小)在中间(其中潜在的刺激可见,在边缘处变为 -1。然后我可以将此数组用作 GratingStim 中的掩码。到目前为止,我一直在尝试 ndimage.filters.gaussian_filter(filter_input, sigma = 7)
但是我在使用此功能时遇到了一些麻烦。如果 filter_input 是一个包含 1 或 0 的 NxM 矩阵,则 ndimage 函数保持它们不变。如果 filter_input 是一个带有随机数的矩阵,它会改变它们。但我仍然没有得到我希望的结果。我知道 PsychoPy 掩码只允许 -1 和 1 之间的值,但现在在下面的代码中,我应该看不到任何东西,因为掩码是 -1。
所以,更具体地说:为什么 ndimage.filters.gaussian_filter(filter_input, sigma = 7) 表现得像它一样?我怎样才能让它为 NxM 矩阵中的每个点分配一个值,使得分配的值具有高斯二维分布?后来我可以砍掉高于 1 和低于 -1 的值。
如果我的问题微不足道,我很抱歉,我一直在用 PsychoPy 做一些编程,但我是 numpy 和 scipy 的新手......
谢谢你的帮助!
下面是一些示例代码:
# -*- coding: utf-8 -*-
from psychopy import visual, event
import numpy as np
from scipy import ndimage
win = visual.Window([500,500])
#draw rectangle
perc25 = visual.Rect(win, width = 0.6,height=0.4, lineColor='red',fillColor = 'red', pos=(0.0, 0.1)) #notloesu
perc25.draw()
#add circle with fuzzy edges
perc75 = visual.GratingStim(win, sf=0, size=0.5, color='green',pos=(0.0, -0.1), mask = 'raisedCos', maskParams={'fringeWidth':0.6})
perc75.draw()
#create the matrix that should result in a gaussian filter laied centrally over the entire window
#desired Result: some red in the upper part of the visible circle in the middle, the rest beeing green
filter_input = (np.ones([500,500]))*(-1.)
gaussian = ndimage.filters.gaussian_filter(filter_input, sigma = 0.2)
print(filter_input == gaussian)
#i know it's ugly, I just can't think of another way to apply the filter to the entire image and I haven't found anything in the internet
unnecesary_stim_to_get_the_mask_over_the_window = visual.GratingStim(win, sf=0, size=0.0000001, color='green',pos=(0, 0), mask = gaussian)
unnecesary_stim_to_get_the_mask_over_the_window.draw()
win.flip()
event.waitKeys()
Run Code Online (Sandbox Code Playgroud)
您的输入gaussian_filter是一个填充 -1 的数组。无论何时进行过滤,都必须考虑如何处理边缘。边的处理gaussian_filter由mode参数决定。默认mode值为'reflect',这意味着数组“外部”的数据(从过滤器的角度来看)是数组内部数据的反射副本。所以gaussian_filter看到的唯一值是常数 -1。高斯滤波器是一个低通滤波器,所以一个常数值不变地通过。这就是为什么您的数组gaussian包含与filter_input.
要创建实际的高斯曲面,请传递一个数组,该数组除中心的单个 1 外全为零。例如,
In [92]: x = np.zeros((101, 101))
In [93]: x[50, 50] = 1
In [94]: y = ndi.filters.gaussian_filter(x, sigma=16)
In [95]: imshow(y, interpolation='none', cmap=cm.gray)
Out[95]: <matplotlib.image.AxesImage at 0x1127e4390>
Run Code Online (Sandbox Code Playgroud)
(ndi是scipy.ndimage,并且imshow是matplotlib.pyplot.imshow。)