簡體   English   中英

圓形窗戶的本地千里馬

[英]Local Maxima with circular window

我正在嘗試使用圓形內核在矩陣上計算局部最大值過濾器。 輸出應該是局部最大值的像元。 對於輸入“數據”中的每個像素,我需要通過圓形窗口查看它是否是局部最大值,因此返回值1,否則返回0。

我有基於以下答案的代碼: 如何將圓盤形蒙版應用於numpy數組?

import numpy as np
import scipy.ndimage as sc

radius = 2
kernel = np.zeros((2*radius+1, 2*radius+1))
y,x = np.ogrid[-radius:radius+1, -radius:radius+1]
mask2 = x**2 + y**2 <= radius**2
kernel[mask2] = 1

def local_maxima(matrix, window_size):
    loc_max = sc.maximum_filter(matrix, window_size, mode='constant')
    return loc_max


data = np.array([(1, 1, 1, 1, 1, 1, 1, 1, 1), (1, 1, 1, 1, 1, 1, 1, 1, 1), (1, 1, 1, 1, 1, 1, 1, 1, 1),
                 (1, 1, 1, 1, 1, 1, 1, 1, 1), (1, 1, 1, 1, 4, 1, 1, 1, 1), (1, 1, 1, 1, 1, 1, 1, 1, 1),
                 (1, 1, 1, 1, 1, 1, 1, 1, 1), (1, 1, 1, 1, 1, 1, 1, 1, 1), (1, 1, 1, 1, 1, 1, 1, 1, 1),
                 (1, 1, 1, 1, 1, 1, 1, 1, 1)])

loc_max = sc.filters.generic_filter(data, local_maxima(data, np.shape(kernel)), footprint=kernel)
max_matrix = np.where(loc_max == data, 1, 0)
np.savetxt('.....\Local\Test_Local_Max.txt', max_matrix, delimiter='\t')

內核具有以下形狀:

[[ 0.  0.  1.  0.  0.]
 [ 0.  1.  1.  1.  0.]
 [ 1.  1.  1.  1.  1.]
 [ 0.  1.  1.  1.  0.]
 [ 0.  0.  1.  0.  0.]]

因此,搜索像元將僅是具有值1的像元。應從局部最大值搜索中排除具有0的像元。

但是腳本在第21行給出了以下錯誤:

 RuntimeError: function parameter is not callable 

謝謝你的幫助!

sc.filters.generic_filter()的第二個參數應該是一個函數,您要向其傳遞由local_maxima(data, np.shape(kernel))調用返回的值,即矩陣。

我對您在這里所做的工作有些困惑,但是我認為您根本不需要generic_filter調用, maximum_filter應該可以執行您想要的操作:

import numpy as np
import scipy.ndimage as sc

radius = 2
kernel = np.zeros((2*radius+1, 2*radius+1))
y,x = np.ogrid[-radius:radius+1, -radius:radius+1]
mask2 = x**2 + y**2 <= radius**2
kernel[mask2] = 1

data = np.array([(1, 1, 1, 1, 1, 1, 1, 1, 1), 
                 (1, 1, 1, 1, 1, 1, 1, 1, 1),  
                 (1, 1, 1, 1, 1, 1, 1, 1, 1),
                 (1, 1, 1, 1, 1, 1, 1, 1, 1),  
                 (1, 1, 1, 1, 4, 1, 1, 1, 1),  
                 (1, 1, 1, 1, 1, 1, 1, 1, 1),
                 (1, 1, 1, 1, 1, 1, 1, 1, 1),  
                 (1, 1, 1, 1, 1, 1, 1, 1, 1),  
                 (1, 1, 1, 1, 1, 1, 1, 1, 1),
                 (1, 1, 1, 1, 1, 1, 1, 1, 1)])

loc_max = sc.maximum_filter(data, footprint=kernel, mode='constant')
max_matrix = np.where(loc_max == data, 1, 0)
np.savetxt('.....\Local\Test_Local_Max.txt', max_matrix, delimiter='\t')

(我沒有在這台計算機上安裝python,所以尚未對此進行測試,對不起)

編輯:我已經測試過,它似乎可以給出正確的結果:

[[1, 1, 1, 1, 1, 1, 1, 1, 1],
 [1, 1, 1, 1, 1, 1, 1, 1, 1],
 [1, 1, 1, 1, 0, 1, 1, 1, 1],
 [1, 1, 1, 0, 0, 0, 1, 1, 1],
 [1, 1, 0, 0, 1, 0, 0, 1, 1],
 [1, 1, 1, 0, 0, 0, 1, 1, 1],
 [1, 1, 1, 1, 0, 1, 1, 1, 1],
 [1, 1, 1, 1, 1, 1, 1, 1, 1],
 [1, 1, 1, 1, 1, 1, 1, 1, 1],
 [1, 1, 1, 1, 1, 1, 1, 1, 1]]

如果訪問的單元格是kernel定義的圓形窗口的局部最大值,則可以使用下面的返回1的代碼(我只是使用%pylab繪制結果作為說明):

%pylab
import scipy.ndimage as sc
data = np.array([(1, 1, 1, 1, 1, 1, 1, 1, 1), (1, 1, 1, 1, 1, 1, 1, 1, 1), (1, 1, 1, 1, 1, 1, 1, 1, 1),
                 (1, 1, 1, 1, 1, 1, 1, 1, 1), (1, 1, 1, 1, 4, 1, 1, 1, 1), (1, 1, 1, 1, 1, 1, 1, 1, 1),
                 (1, 1, 1, 1, 1, 1, 1, 1, 1), (1, 1, 1, 1, 1, 1, 1, 1, 1), (1, 1, 1, 1, 1, 1, 1, 1, 1),
                 (1, 1, 1, 1, 1, 1, 1, 1, 1)])
matshow(data)
colorbar()

數據

radius = 2
kernel = np.zeros((2*radius+1, 2*radius+1))
y,x = np.ogrid[-radius:radius+1, -radius:radius+1]
mask2 = x**2 + y**2 <= radius**2
kernel[mask2] = 1
matshow(kernel)
colorbar()

核心

def filter_func(a):
    return a[len(a)/2] == a.max()
out = sc.generic_filter(data, filter_func, footprint=kernel)
matshow(out)
colorbar()

產量

以下是帶有隨機輸入數據數組的結果:

data = np.random.random(size=data.shape)
matshow(data)

隨機數組

out = sc.generic_filter(data, filter_func, footprint=kernel)
matshow(out)
colorbar()

在隨機數組上輸出

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM