python - 如何在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]
我有几种方法:
- 最大 X 和最大 Y
- x 和 y 之和的最大值,然后是 x 和 y 之比的最小值
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]。这使得坐标混乱
解决方案
您没有得到任何结果,因为迭代列表列表将导致每次都在一个列表上工作,这意味着,如果我迭代您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))
推荐阅读
- reactjs - Stanza.io 在 ReactJS 中发送带有客户属性的消息
- cordova - 混合应用程序上的本地存储
- r - 事后刻字不匹配ggplot2
- ssis - 转换成 &
- undefined - Stripe 和 back4app:无法读取未定义的属性“id”
- mysql - MySQL in 子句慢,有 10 个或更多项目
- magento-1.9 - Magento 1.9,修改Category下已有的Label
- matlab - 如何在matlab中将二维形状划分为大约相等大小的块?
- vba - 来自动态添加的组合框的 VBA 值
- node.js - 如何将数据保存到猫鼬模式中的数组对象?