2016-08-24 113 views
0

我想提出一個簡單的高斯模糊,因爲我不能讓SciPy的的卷積的工作,我做我自己的:我高斯濾波器太慢

def Convolve(matr_, ker_): 
    output = matr_.astype(np.float64) 
    for x in range(len(matr_)): 
     for y in range(len(matr_[x])): 
      sum = 0 
      count = 0 
      width = int(len(ker_)/2) 
      for x_c in range(len(ker_)): 
       for y_c in range(len(ker_)): 
        x_index = x - x_c + width 
        y_index = y - y_c + width 
        if (x_index >= 0) and (x_index < len(matr_)) and (y_index >= 0) and (y_index < len(matr_[x])): 
         sum += ker_[x_c][y_c] * matr_[x_index][y_index] 
         count += ker_[x_c][y_c] 
        else: 
         #print("{0} -> {1}, {2} -> {3}".format(x, x_index, y, y_index)) 
         pass 
      output[x][y] = sum/count 
    return output.astype(matr_.dtype) 

我也正常化像素就在這裏,所以他們仍然會總是適合matr_的類型。但它的工作非常慢,需要20秒才能處理1440x900圖像。這怎麼能更快地工作呢?

+0

你可以使用'scipy.ndimage.gaussian_filter':http://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.gaussian_filter.html –

+0

@WarrenWeckesser我想嘗試寫我自己的。 – RomaValcer

回答

0

如果您可以避免使用Python,則永遠不要使用Python中的循環。使用Numpy或熊貓,並在矢量上工作。

如果你使用循環(在你的例子中似乎不是這種情況),使用Numba軟件包。

+0

你會如何將卷積用於矢量? – RomaValcer