gpt4 book ai didi

python - 我可以对图像使用 numpy 渐变函数吗

转载 作者:行者123 更新时间:2023-11-28 22:34:38 25 4
gpt4 key购买 nike

我最近一直在尝试测试 numpy.gradient 函数。但是,它的行为对我来说有点奇怪。我创建了一个包含随机变量的数组,然后在其上应用了 numpy.gradient,但这些值似乎很疯狂且无关紧要。但是当使用 numpy.diff 时,值是正确的。

因此,在查看 numpy.gradient 的文档后,我发现它在所需维度上使用 distance=1。

这就是我的意思:

import numpy as np;

a= np.array([10, 15, 13, 24, 15, 36, 17, 28, 39]);
np.gradient(a)
"""
Got this: array([ 5. , 1.5, 4.5, 1. , 6. , 1. , -4. , 11. , 11. ])
"""
np.diff(a)
"""
Got this: array([ 5, -2, 11, -9, 21, -19, 11, 11])
"""

我不明白第一个结果中的值是怎么来的。如果默认距离应该是 1,那么我应该得到与 numpy.diff 相同的结果。

谁能解释一下这里的距离是什么意思。它是相对于数组索引还是数组中的值?如果它取决于值,那么这是否意味着 numpy.gradient 不能用于图像,因为相邻像素的值没有固定值差异?

最佳答案

# load image
img = np.array([[21.0, 20.0, 22.0, 24.0, 18.0, 11.0, 23.0],
[21.0, 20.0, 22.0, 24.0, 18.0, 11.0, 23.0],
[21.0, 20.0, 22.0, 24.0, 18.0, 11.0, 23.0],
[21.0, 20.0, 22.0, 99.0, 18.0, 11.0, 23.0],
[21.0, 20.0, 22.0, 24.0, 18.0, 11.0, 23.0],
[21.0, 20.0, 22.0, 24.0, 18.0, 11.0, 23.0],
[21.0, 20.0, 22.0, 24.0, 18.0, 11.0, 23.0]])
print "image =", img

# compute gradient of image
gx, gy = np.gradient(img)
print "gx =", gx
print "gy =", gy

# plotting
plt.close("all")
plt.figure()
plt.suptitle("Image, and it gradient along each axis")
ax = plt.subplot("131")
ax.axis("off")
ax.imshow(img)
ax.set_title("image")

ax = plt.subplot("132")
ax.axis("off")
ax.imshow(gx)
ax.set_title("gx")

ax = plt.subplot("133")
ax.axis("off")
ax.imshow(gy)
ax.set_title("gy")
plt.show()

关于python - 我可以对图像使用 numpy 渐变函数吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38809852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com