python - scipy.ndimage.filter.laplace() 中使用的拉普拉斯掩码/内核是什么?

标签 python numpy image-processing filter scipy

一个简单的水平/垂直拉普拉斯掩模在内核的中心(图的左侧)有 4 个。类似地,对对角线特征敏感的拉普拉斯掩码在内核的中心(下图中的右侧)有 8 个。 scipy 使用的是什么掩码,我可以选择使用哪个掩码吗?

enter image description here

最佳答案

一个简单的检查是声明一个二维零数组,除了中心的一个系数设置为 1,然后对其应用 laplace 函数。具有过滤功能的属性是,如果您提交带有单个 1 的图像,则输出将是实际过滤器本身,以 1 所在的位置为中心 - 查找 impulse response ...或者更具体地说,Point Spread Function .

如果你这样做,那么在运行 laplace 方法后你会看到它的样子:

In [13]: import numpy as np

In [14]: import scipy.ndimage.filters

In [15]: A = np.zeros((5,5))

In [16]: A[2,2] = 1

In [17]: B = scipy.ndimage.filters.laplace(A)

In [18]: A
Out[18]:
array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  1.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]])

In [19]: B
Out[19]:
array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  1.,  0.,  0.],
       [ 0.,  1., -4.,  1.,  0.],
       [ 0.,  0.,  1.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]])

因此,它是第一个正在使用的内核,但请注意符号的变化。中心系数为正,其余系数为负。


但是,如果您真的想知道引擎盖下发生了什么,请查看有关函数的文档:http://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.filters.laplace.html - 有指向函数定义来源的链接:

https://github.com/scipy/scipy/blob/v0.16.0/scipy/ndimage/filters.py#L396

你需要看的相关代码在这里:

def derivative2(input, axis, output, mode, cval):
    return correlate1d(input, [1, -2, 1], axis, output, mode, cval, 0)
return generic_laplace(input, derivative2, output, mode, cval)

基本上,[1, -2, 1] 的一维内核被独立应用于每个维度,正如 correlate1d 函数所做的那样...所以行首先,然后是列。这实际上计算了您在问题中看到的第一个掩码。

关于python - scipy.ndimage.filter.laplace() 中使用的拉普拉斯掩码/内核是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32768407/

相关文章:

python - 在 scikit-image 中导入相对/绝对函数时出现问题

c++ - C++中的图像处理

python - OpenCV - 如何通过斜线将图像分割为两个区域?

python - 过滤图像以提高文本识别

python - 从 git 存储库中的 requirements.txt 文件安装包

python - 新窗口是空的

带有 Chrome 的 Python Selenium。如何在不同的选项卡之间切换

python - python对不规则(x,y,z)网格的4D插值

python - 删除 pandas 中的前导 NaN

python - 不可散列类型 : 'numpy.ndarray' error in tensorflow