【问题标题】:Gradient orientation with arctan2 results in flipped anglesarctan2 的梯度方向导致翻转角度
【发布时间】: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返回的dxdy的顺序吗?
  • 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


【解决方案1】:

plt.quiver 绘制时,将数组想象成上下翻转。您可以通过在 test_array 中引入不对称来看到这一点。例如

test_array = np.array([[   0.,    0.,    20.,    50.,    0.],
                       [   0.,   64.,  128.,   64.,    0.],
                       [   0.,  127.,  255.,  127.,    0.],
                       [   0.,   64.,  127.,   64.,    0.],
                       [   0.,    0.,    0.,    0.,    0.]]).astype(np.float64)

将绘制如下(注意theta 中的角度和绘图底部箭头的角度):

>>> print(theta)

[[   0.           81.11934085   76.96664367  125.53767779  180.        ]
 [   0.           44.77531182   90.          148.97046189  180.        ]
 [   0.            0.          -90.          180.          180.        ]
 [   0.          -45.          -90.         -135.          180.        ]
 [   0.          -90.          -90.          -90.            0.        ]]

可能当您查看test_arraytheta 时,您会认为原点位于左上角。但是matplotlib中的原点在左下角。

【讨论】:

  • 可能反过来,但不错:)
  • 要理解这个比较plt.imshowplt.pcolormesh如何绘制一个数组。
猜你喜欢
  • 2011-01-16
  • 2013-11-17
  • 1970-01-01
  • 1970-01-01
  • 2013-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-09
相关资源
最近更新 更多