numpy - 使用arctan2进行梯度定向会导致角度翻转

标签 numpy opencv matplotlib computer-vision

我想使用plt.quiver以正确的 Angular 显示数组渐变的箭头。

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.        ]]

我可以看到第一行的 Angular 是[90,90,90]当我使用quiver绘制渐变时,我可以看到箭头指向相反的方向。

我假设 Angular 是逆时针显示的,但它们是顺时针显示的。我想念什么?有人可以解释吗?

最佳答案

将数组视为由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中的 Angular 和图底部的箭头 Angular ):
>>> 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.        ]]
enter image description here
当您查看test_arraytheta时,您可能会认为原点位于左上角。但是matplotlib的起源在左下角。

关于numpy - 使用arctan2进行梯度定向会导致角度翻转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59631756/

相关文章:

python - 优化给定移位的数组距离的成对计算

Python 多处理数组和 OpenCV3 框架

python - 重新采样 - 无法单独创建训练集和测试集

c++ - findContours 和 RETR_TREE 遍历层次结构

python - 在 pyplot 中的图例上画一条线

python - 绘图遮盖了 matplotlib 中的其他绘图

python - Numpy 均值结构化数组

c++ - MJPEG 流无法在 OpenCV 2.4 中打开

opencv - 更新 ffmpeg 对 libopencv_core.so 版本的引用

python - 在 Matplotlib 中更新单条