我在这方面花了很多时间,并且我知道如何通过对边界行/列进行切片和索引来手动完成此操作,但 SciPy 必须有一种更简单的方法。
我需要将CVAL(当 时填充超出边缘的值mode=constant) 设置为 NaN,但是,这将返回 NaN。
我用代码和图来解释一下:
import numpy as np
from scipy import ndimage
m = np.reshape(np.arange(0,100),(10,10)).astype(np.float)
Run Code Online (Sandbox Code Playgroud)
使用 SciPy ndimage 均匀滤波器使用 3x3 内核计算平均值:
filter = ndimage.uniform_filter(m, size=3, mode='constant')
print(filter[1][1]) # equal to 11
print(filter[9][9]) # I need 93.5, however it gets 41.55 due to zeros
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,第一个值显示为 11,这符合预期,但是,对于边界上的任何单元格,它将用零填充值(我还尝试了所有其他模式)。
这是我需要实现的目标(左)与mode=constant和CVAL=0(默认 0)
我正在尝试使用 scipy.ndimage.filters.generic_filter 来计算邻域的加权总和。社区在某些时候会发生变化,但现在 3x3 是我正在努力的方向。到目前为止,这是我的位置:
def Func(a):
a = np.reshape((3,3))
weights = np.array([[0.5,.05,0.5],[0.5,1,0.5],[0.5,0.5,0.5]])
a = np.multiply(a,weights)
a = np.sum(a)
return a
ndimage.filters.generic_filter(Array,Func,footprint=np.ones((3,3)),mode='constant',cval=0.0,origin=0.0)
Run Code Online (Sandbox Code Playgroud)
我从 ndimage 收到一条错误消息,说“TypeError: a float is required”,但我不知道它指的是什么参数,它看起来与我见过的其他示例基本相同。
我想确定图像中两个特定点之间的最小路径,即通过像素强度(灰度)加权的相邻像素之间的距离之和的路径将被最小化.例如,此图显示输入图像
这是红色的(手绘)最小路径,从UL到LR角(黑色边界用作零重量填充):
我发现matlab只具有graydist功能; 在ndimage/scikit-image /中有类似的东西吗?我找到了scipy.ndimage.morphology.distance_transform_edt,但我不确定是否以及如何将其用于此目的.如果算法仅返回非唯一最小值中的一个,则可以.
我对实现提示不感兴趣,它在算法上是一个相当简单的任务(至少是使用动态编程的简单实现),我正在寻找(组合)已经编码的例程来实现这一点.
我正在使用ndimage插值缩放,我收到这个恼人的警告:
UserWarning:从scipy 0.13.0开始,zoom()的输出形状是用round()而不是int()计算的 - 对于这些输入,返回数组的大小已经改变.
我不确定我应该从中得到什么,我开始使用它与SciPy 1.0.0,所以我不相信它真的影响了我.
我认为称其为UserWarning有点值得怀疑,因为它不是供用户使用,但也许目标用户是导入库的开发人员.
我正在使用多处理,每个进程都会收到一个警告,甚至更烦人.
是否有一种理智的沉默方式?
给定一个由 0 和 1 组成的 N*N 数组,我想构建簇列表(簇是一组用 1 标记的连接点)。
scipy.ndimage.label非常有用,因为它告诉您哪些点是相连的。
但我还想在我的阵列上有周期性边界条件,即点(0,j)和(N,j)被识别(就像我粘合成圆柱体的平面)。所以我需要告诉 scipy.ndimage.label 特征是通过边界连接的。
例如,如果我的原始数组是:
In[187]: a = [[1, 1, 0, 0, 0, 0, 1, 1],[1, 1, 0, 1, 0, 0, 1, 1],[1, 1, 0, 0, 0, 1, 1, 1]]
labels = measurements.label(a)
print(labels)
Out [187]: (array([[1, 1, 0, 0, 0, 0, 2, 2],
[1, 1, 0, 3, 0, 0, 2, 2],
[1, 1, 0, 0, 0, 2, 2, 2]], dtype=int32), 3)
Run Code Online (Sandbox Code Playgroud)
我想要:
(array([[1, 1, 0, …Run Code Online (Sandbox Code Playgroud) 我有一个二进制图像。二值图像有一些隔离区域,如噪声。我知道预期区域比这些隔离区域大得多。因此,我使用连接组件通过找到最大的连接区域来删除隔离区域。我必须使用 scipy 包。我发现它有一些功能可以做到这一点。然而,我离结果还很远。如何使用这些函数获得可以忽略孤立区域的二值图像?谢谢
from scipy import ndimage
label_im, nb_labels = ndimage.label(binary_img)
# Find the largest connected component
sizes = ndimage.sum(binary_img, label_im, range(nb_labels + 1))
mask_size = sizes < 1000
remove_pixel = mask_size[label_im]
label_im[remove_pixel] = 0
labels = np.unique(label_im)
binary_img= np.searchsorted(labels, label_im)
#Select the biggest connected component
binary_img[binary_img < binary_img.max()]=0
binary_img[binary_img >= binary_img.max()]=1
Run Code Online (Sandbox Code Playgroud) 我有一个二维数组,我使用ndimage.label()如下函数来标记簇:
import numpy as np
from scipy.ndimage import label
input_array = np.array([[0, 1, 1, 0],
[1, 1, 0, 0],
[0, 0, 0, 1],
[0, 0, 0, 1]])
labeled_array, _ = label(input_array)
# Result:
# labeled_array == [[0, 1, 1, 0],
# [1, 1, 0, 0],
# [0, 0, 0, 2],
# [0, 0, 0, 2]]
Run Code Online (Sandbox Code Playgroud)
我可以获得标记簇的元素计数、质心或边界框。但我还想获得簇中每个元素的坐标。像这样的东西(数据结构不一定是这样的,任何数据结构都可以):
{
1: [(0, 1), (0, 2), (1, 0), (1, 1)], # Coordinates of the elements that have the label "1"
2: [(2, …Run Code Online (Sandbox Code Playgroud)