查找坐标数组的角点 [英] Finding the Corners of the an array of coordinates
问题描述
我在 Numpy 中有一个二维坐标数组.
I have a 2D array of Coordinates in Numpy.
我的目标是尝试找到角落(好像它是一个正方形).所以:
My goal is to attempt to find the corners (as if it were a square). So the :
左上:最小的x,最大的y右上角:最大的 x,最大的 y左下:最小的 x,最小的 y右下:最大的x,最小的y
Top left: smallest x, highest y Top right: largest x, largest y bottom left: smallest x, smallest y bottom right: largest x, smallest y
显然,这些对中的每一个都需要考虑其他值.
Obviously each of these pairs need to consider the other values.
我试图根据行取最小值和最大值:
I was trying to take the min and max depending on the row:
BottomLeft = np.min(np.min(hull, axis=1), axis=0)
然而,这并不能将这对值保持在一起.它必须是最小可能的 X 值,以及其中最小的 y 值.或者类似的东西.我假设有一种有效的方法可以用 numpy 做到这一点?
However, this does not keep the pair of values together. It would have to be something like the smallest possible X values, and out of those, the smallest y value. Or something along these lines. I am assuming there is efficient way to do this with numpy?
这是一个数据示例:
[[[260 156]]
[[248 176]]
[[235 197]]
[[233 199]]
[[192 199]]
[[174 197]]
[[160 171]]
[[150 151]]
[[154 149]]
[[156 149]]
[[260 151]]]
谢谢!
推荐答案
假设x坐标如下
x = np.arange(0, 22, 2)
并假设y坐标如下
y = np.arange(20, 32, 2)
xx, yy = np.meshgrid(x, y)
yy = np.flip(yy, 0)
print(xx)
print(yy)
然后你可以对 xx 和 yy 做任何操作,因为它们是坐标.
Then you can do whatever operation with xx and yy as they are coordinates.
例如
让我们假设 z 是海拔
let us assume z is the elevation
z = np.random.randint(2, high=20, size=(yy.shape[0], yy.shape[1])) # xx can also be used
import matplotlib.pyplot as plt
plt.contourf(xx, yy, z)
plt.colorbar()
这篇关于查找坐标数组的角点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!