首页 > 解决方案 > 如何在python中找到最接近角落的图像中的哪个坐标?

问题描述

我有一个二维数组,[[x1, y1], [x2, y2], [x3, y3],, ..., [xn, yn]](n < 20)。这些数组是图像中的一对坐标

哪个坐标最靠近右上角、右下角和左下角?

例子:

arr = [[634 974] [1089 1721] [171 1719] [1092 228]]

预期结果:

最靠近右上角 =[1092, 228]

最接近右下角 =[1089, 1721]

最靠近左下角 =[171, 1719]

我有几种方法:

idk,我真的很困惑什么是最好的方法。我试过这个:

max_x, max_y = arr.max(axis=0)
min_x,min_y = arr.min(axis=0)

print("max X and min Y:",[max_x,min_y])
print("max X and max Y:",[max_x,max_y])
print("min X and max Y:",[min_x,max_y])

但最接近右下角的结果是 [1092, 1721]。这使得坐标混乱

标签: pythonarraysimage-processing

解决方案


您没有得到任何结果,因为迭代列表列表将导致每次都在一个列表上工作,这意味着,如果我迭代您arr的第一个值,i那么[634,974]显然最大值[634]是 634...要解决它,我们可以使用 2 个数组,一个保存 x 值,另一个保存 y 值。在这些数组上,我们想要找到最大值和最小值。所以!让我们实现它!

arr = [[634, 974], [1176, 1854], [94, 1853], [1176, 95]]
x_values = []
y_values = []
for pair in arr:
    x_values.append(pair[0]) #appending the x values
    y_values.append(pair[1]) # same for y
    
#now that we have all of our values sorted we can find the max and min:
print(max(x_values))
print(min(y_values))

推荐阅读