Python/scipy 中的一维非极大值抑制

发布于 2024-12-01 22:55:14 字数 231 浏览 6 评论 0原文

你碰巧有一个用Python编写的一维非极大值抑制算法吗?我需要它来使用 scipy 在 Python 中制作 Canny 边缘检测器,该检测器将一维强度向量作为输入。

我在网上浏览了一下,有很多描述 Canny 边缘检测器行为的信息以及一些用 Java 编写的示例,但它们都描述了 2D 边缘检测。

然而 scipy 确实支持 Canny 边缘检测所需的其他算法,即一维的高斯滤波和微分。

提前致谢。

Do you happen to have a 1D non-maximum suppression algorithm written in Python. I need it for making a Canny edge detector in Python using scipy that takes as input a 1D intensity vector.

I've looked around on the web and there is a lot of information describing the behavior of the Canny edge detector and some examples written in Java but they all describe edge detection in 2D.

However scipy does support the other algorithms needed for the Canny edge detection, namely the Gaussian filtering and differentiation for 1D.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

╭⌒浅淡时光〆 2024-12-08 22:55:14

你的意思是最大过滤器吗?如果是这样,请查看 scipy.ndimage.maximum_filter1d

作为一个简单的例子:

import numpy as np
import scipy.ndimage as ndimage

input = np.sin(np.linspace(0, 4*np.pi, 20))
input = (input * 10).astype(np.int) # Makes it easier to read
output = ndimage.maximum_filter1d(input, 4)

print 'In: ', input
print 'Out:', output

这会产生:

In:  [ 0  6  9  9  4 -1 -7 -9 -8 -3  3  8  9  7  1 -4 -9 -9 -6  0]
Out: [ 6  9  9  9  9  9  4 -1 -3  3  8  9  9  9  9  7  1 -4  0  0]

Do you just mean a maximum filter? If so, have a look at scipy.ndimage.maximum_filter1d

As a quick example:

import numpy as np
import scipy.ndimage as ndimage

input = np.sin(np.linspace(0, 4*np.pi, 20))
input = (input * 10).astype(np.int) # Makes it easier to read
output = ndimage.maximum_filter1d(input, 4)

print 'In: ', input
print 'Out:', output

This yields:

In:  [ 0  6  9  9  4 -1 -7 -9 -8 -3  3  8  9  7  1 -4 -9 -9 -6  0]
Out: [ 6  9  9  9  9  9  4 -1 -3  3  8  9  9  9  9  7  1 -4  0  0]
〃安静 2024-12-08 22:55:14

我假设你的数据不是周期性的。让我给你一些伪代码,希望这已经足够了。

-- 比较数据

-- 您正在寻找差异中的符号变化。对于麦克斯来说,这将是从积极到消极;零是一个特殊情况。 numpy.sign() 为您提供负值、零值和正值的 -1、0、1 中每个值的符号。

-- 您可以再次比较 -- 您正在寻找 -2 和 -1 或最小抑制 2 和 1。正确处理边界。

祝你好运,E

import matplotlib.pyplot as plt
import numpy as np

dat= np.linspace(1, 5, 5)
dat2 = np.concatenate([dat, dat[::-1]+.5, dat,dat[::-1]])
## ignore above.

res = np.diff(np.sign(np.diff(dat2, prepend=[0])), append=0)
plt.plot(dat2)  ## NB end conditions, and you can ignore plotting
plt.plot(res)

干杯,E

I assume your data is not periodic. Let me give you some pseudocode and hopefully that is enough.

-- diff the data

-- you are looking for sign changes in the diff. For Max that would be going from positive to negative; zero is a special case. numpy.sign() gives you the sign of each value in -1, 0, 1 for negative, zero and positive values.

-- You can diff again -- you are looking for -2 and -1 or for min suppression 2 and 1. Handle the boundaries properly.

Good luck, E

import matplotlib.pyplot as plt
import numpy as np

dat= np.linspace(1, 5, 5)
dat2 = np.concatenate([dat, dat[::-1]+.5, dat,dat[::-1]])
## ignore above.

res = np.diff(np.sign(np.diff(dat2, prepend=[0])), append=0)
plt.plot(dat2)  ## NB end conditions, and you can ignore plotting
plt.plot(res)

Cheers, E

素罗衫 2024-12-08 22:55:14

如果您的输入数据是变量“信号”,那么您需要的结果可以通过以下方式给出:(

k = np.sum(np.abs(np.diff(signal>0)))

使用 Numpy 函数)

If your input data is the variable "signal", then the result you need can be given by:

k = np.sum(np.abs(np.diff(signal>0)))

(using Numpy functions)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文