【发布时间】:2020-04-25 03:46:03
【问题描述】:
我想使用plt.quiver 以正确的角度显示数组渐变的箭头。
import numpy as np
import matplotlib.pyplot as plt
test_array = np.array([[ 0., 0., 0., 0., 0.],
[ 0., 64., 128., 64., 0.],
[ 0., 127., 255., 127., 0.],
[ 0., 64., 127., 64., 0.],
[ 0., 0., 0., 0., 0.]]).astype(np.float64)
dy,dx = np.gradient(test_array)
theta =np.degrees(np.arctan2(dy,dx))
print(theta)
color = np.sqrt(dy**2 + dx**2)
plt.quiver(dx,dy,color)
plt.show()
但是,如果我打印 theta:
[[ 0. 90. 90. 90. 0. ]
[ 0. 44.77531182 90. 135.22468818 180. ]
[ 0. 0. -90. 180. 180. ]
[ 0. -45. -90. -135. 180. ]
[ 0. -90. -90. -90. 0. ]]
我可以看到第一行的角度是[90,90,90]
当我使用quiver 绘制渐变时,我可以看到箭头指向相反的方向。
我会假设角度是逆时针显示的,但它们是顺时针显示的。我错过了什么?谁能解释一下?
【问题讨论】:
-
The arrows are pointing to the opposite direction是什么意思。您的预期结果是什么? -
正如我所说,我假设如果角度为
90 degrees,则箭头朝向正 y 轴(但它朝向负 y 轴)。如果它是180 degrees,那么我预计箭头将面向负 x 轴。如果角度为270 degrees,那么我预计箭头将面向负 y 轴。逆时针...(注意我用 quiver 函数看到的坐标系)看起来像一个普通的笛卡尔坐标系 -
你确定
np.gradient返回的dx和dy的顺序吗? -
np.gradient将计算沿图像给定轴的梯度,因此axis=0=dy将是行,axis=1=dx将是列。所以我不是100%肯定,但这是有道理的。否则角度虽然正确,但箭头与图像的真实强度变化不匹配。 -
根据文档:
For two dimensional arrays, the return will be two arrays ordered by axis. In this example the first array stands for the gradient in rows and the second one in columns direction:docs.scipy.org/doc/numpy/reference/generated/…
标签: numpy opencv matplotlib computer-vision