2016-02-26 60 views
0

對於一個實驗,我需要一個整個窗口上的高斯濾波器,如中間部分顯示。由於我使用的是PsychoPy,基本上,我需要一個N×M數組(N和M是窗口的像素大小),中間的數組(其中底層的刺激在邊緣可見-1),然後我可以使用這個數組作爲內GratingStim口罩。 到目前爲止,我一直在努力 ndimage.filters.gaussian_filter(filter_input,差= 7)我如何獲得一個高斯內核陣列來過濾我的圖像

,但我有一些麻煩與此funcion。如果filter_input是N×M的矩陣如果filter_input是一個帶有隨機數的矩陣,它會改變它們,但我仍然沒有得到我期望的結果,我意識到PsychoPy的面具只允許-1和1之間的值,但因爲它現在在下面的代碼中,所以我不應該能夠看到任何東西,因爲蒙版是-1。

所以,更具體地說: 爲什麼ndimage.filters.gaussian_filter(filter_input,sigma = 7)的行爲就像它一樣?我怎麼能得到它分配給NxM矩陣中的每個點的值,使得分配的值具有高斯2d分佈?後來我可以砍掉高於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() 

回答

3

你輸入到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> 

plot

ndiscipy.ndimage,並imshowmatplotlib.pyplot.imshow

+0

比較遺憾的是漫長的等待,感謝您的輸入,這讓我進一步想在哪裏結果。 – lilla

相關問題