问题描述
我正在使用scikit.morphology对二维数组进行腐蚀。我还需要确定每个像元与腐蚀中确定的最小值之间的距离。
示例:
np.reshape(np.arange(1,126,step=5),[5,5])
array([[ 1,6,11,16,21],[ 26,31,36,41,46],[ 51,56,61,66,71],[ 76,81,86,91,96],[101,106,111,116,121]])
erosion(np.reshape(np.arange(1,5]),selem=disk(3))
array([[ 1,1,6],[ 1,11],[26,46]])
现在我想做的就是还返回一个数组,该数组可以使我达到最小距离,如下所示:
array([[ 0,2,3,3],[ 2,[ 3,3]])
是否有可以执行此操作的scikit工具?如果没有,关于如何有效实现此结果的任何提示?
解决方法
您可以使用scipy.ndimage.distance_transform_cdt
找到距脚印中心的距离,然后使用SciPy的ndimage.generic_filter
返回这些值:
import numpy as np
from skimage.morphology import erosion,disk
from scipy import ndimage as ndi
input_arr = np.reshape(np.arange(1,126,step=5),[5,5])
footprint = disk(3)
def distance_from_min(values,distance_values):
d = np.inf
min_val = np.inf
for i in range(len(values)):
if values[i] <= min_val:
min_val = values[i]
d = distance_values[i]
return d
full_footprint = np.ones_like(footprint,dtype=float)
full_footprint[tuple(i//2 for i in footprint.shape)] = 0
# use `ndi.distance_transform_edt` instead for the euclidean distance
distance_footprint = ndi.distance_transform_cdt(
full_footprint,metric='taxicab'
)
# set values outside footprint to 0 for pretty-printing
distance_footprint[~footprint.astype(bool)] = 0
# then,extract it into values matching the values in generic_filter
distance_values = distance_footprint[footprint.astype(bool)]
output = ndi.generic_filter(
input_arr.astype(float),distance_from_min,footprint=footprint,mode='constant',cval=np.inf,extra_arguments=(distance_values,),)
print('input:\n',input_arr)
print('footprint:\n',footprint)
print('distance_footprint:\n',distance_footprint)
print('output:\n',output)
哪个给:
input:
[[ 1 6 11 16 21]
[ 26 31 36 41 46]
[ 51 56 61 66 71]
[ 76 81 86 91 96]
[101 106 111 116 121]]
footprint:
[[0 0 0 1 0 0 0]
[0 1 1 1 1 1 0]
[0 1 1 1 1 1 0]
[1 1 1 1 1 1 1]
[0 1 1 1 1 1 0]
[0 1 1 1 1 1 0]
[0 0 0 1 0 0 0]]
distance_footprint:
[[0 0 0 3 0 0 0]
[0 4 3 2 3 4 0]
[0 3 2 1 2 3 0]
[3 2 1 0 1 2 3]
[0 3 2 1 2 3 0]
[0 4 3 2 3 4 0]
[0 0 0 3 0 0 0]]
output:
[[0. 1. 2. 3. 3.]
[1. 2. 3. 3. 3.]
[2. 3. 4. 4. 4.]
[3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3.]]
但是,此功能非常慢。如果要使其更快,您将需要(a)像Numba或Cython这样的解决方案用于过滤器函数,并与SciPy LowLevelCallables结合使用;以及(b)将距离数组硬编码到距离函数中,因为LowLevelCallables,传递额外的参数比较困难。这是llc-tools
的完整示例,您可以使用pip install numba llc-tools
进行安装。
import numpy as np
from scipy import ndimage as ndi
from skimage.morphology import erosion,disk
import llc
def filter_func_from_footprint(footprint):
# first,create a footprint where the values are the distance from the
# center
full_footprint = np.ones_like(footprint,dtype=float)
full_footprint[tuple(i//2 for i in footprint.shape)] = 0
# use `ndi.distance_transform_edt` instead for the euclidean distance
distance_footprint = ndi.distance_transform_cdt(
full_footprint,metric='taxicab'
)
# then,extract it into values matching the values in generic_filter
distance_footprint[~footprint.astype(bool)] = 0
distance_values = distance_footprint[footprint.astype(bool)]
# finally,create a filter function with the values hardcoded
@llc.jit_filter_function
def distance_from_min(values):
d = np.inf
min_val = np.inf
for i in range(len(values)):
if values[i] <= min_val:
min_val = values[i]
d = distance_values[i]
return d
return distance_from_min
if __name__ == '__main__':
input_arr = np.reshape(np.arange(1,5])
footprint = disk(3)
eroded = erosion(input_arr,selem=footprint)
filter_func = filter_func_from_footprint(footprint)
result = ndi.generic_filter(
# use input_arr.astype(float) when using euclidean dist
input_arr,filter_func,footprint=disk(3),)
print('input:\n',input_arr)
print('output:\n',result)
哪个给:
input:
[[ 1 6 11 16 21]
[ 26 31 36 41 46]
[ 51 56 61 66 71]
[ 76 81 86 91 96]
[101 106 111 116 121]]
output:
[[0 1 2 3 3]
[1 2 3 3 3]
[2 3 4 4 4]
[3 3 3 3 3]
[3 3 3 3 3]]
除了SciPy网站上的LowLevelCallable文档(上面有链接,还有其中的链接)之外,有关低级可调用对象和llc工具的更多信息,您还可以阅读我几年前写的这两篇博客文章: / p>