如何将 x1、x2、y1、y2 数组转换为矩形顶点数组?
How do I turn x1, x2, y1, y2 array into rectangular vertices array?
假设我有一个 4 列的二维数组,让我们将列命名为 x1、x2、y1、y2,数组的数据是 0 到 1 之间的浮点数,x2 总是大于 x1,y2 总是大于 y1.
每行代表一个矩形,其左下角位于(x1, y1),宽度为x2-x1,高度为y2-y1。
数组是这样创建的:
import numpy as np
bounds = np.random.uniform(size=(100, 4))
bounds[:,:2].sort(1)
bounds[:,2:].sort(1)
现在如何在不使用 for 循环的情况下按以下顺序从数组创建矩形顶点:左下角、右下角、右上角、左上角?
我能做到:
rectangles = [[(x1, y1), (x2, y1), (x2, y2), (x1, y2)] for x1, x2, y1, y2 in bounds]
但这首先违背了使用数组的目的。
相反,我可以这样做:
lower_left = np.array((bounds[:, 0], bounds[:, 2])).T
lower_right = np.array((bounds[:, 1], bounds[:, 2])).T
upper_right = np.array((bounds[:, 1], bounds[:, 3])).T
upper_left = np.array((bounds[:, 0], bounds[:, 3])).T
现在我只需要弄清楚如何水平连接列,以便列中的每一行都保留为子行。
我试过这样做:
np.hstack((lower_left, lower_right, upper_right, upper_left))
但它并没有完全按预期工作,字段已连接,出于与问题无关的原因,我需要将点分开。
如何使用矢量化获得与我的列表理解完全相同的结果?
编辑
我刚刚弄清楚如何将数组解包为列:
lower_left, lower_right, upper_right, upper_left = bounds.T
请记住,我在 Android phone 上写下了所有这些内容,而且我无法实际访问我可以在夜间进行编程的实际计算机(这是我的夜间)撰写本文时的语言环境),这是一个复杂的问题,我不会在这里讨论。
问题是我不能 运行 复杂的代码,对于仅使用内置函数或标准库的简单代码,我可以使用 w3c 学校的 Python TryIt 编辑器来 运行代码,但是对于任何需要导入通过 pip 安装的任何库的东西,我不能 运行 代码,所以我不能确定我的示例是否会 运行.
编辑
修复了示例代码中的错误,我将所有这些都写在 phone 上,所以我不能 运行 代码。
我已经完成了,我会post下面的代码,这样它可以帮助任何通过Google搜索在这里绊倒的人:
import numpy as np
values = np.random.random((100, 4))
values[:, :2].sort(1)
values[:, 2:].sort(1)
x1, x2, y1, y2 = values.T
ll = np.array((x1, y1)).T
lr = np.array((x2, y1)).T
ur = np.array((x2, y2)).T
ul = np.array((x1, y2)).T
rects = np.stack((ll, lr, ur, ul), axis=1)
rects.shape
它产生这个:
(100, 4, 2)
样本:
array([[[0.0296138 , 0.29131621],
[0.3693961 , 0.29131621],
[0.3693961 , 0.97544314],
[0.0296138 , 0.97544314]],
[[0.51630027, 0.39519657],
[0.59897507, 0.39519657],
[0.59897507, 0.76210139],
[0.51630027, 0.76210139]],
[[0.84594749, 0.42784315],
[0.86691432, 0.42784315],
[0.86691432, 0.79314211],
[0.84594749, 0.79314211]],
[[0.34412745, 0.21132195],
[0.68466905, 0.21132195],
[0.68466905, 0.25444418],
[0.34412745, 0.25444418]],
[[0.08766792, 0.39237762],
[0.89154757, 0.39237762],
[0.89154757, 0.44256892],
[0.08766792, 0.44256892]],
[[0.05022344, 0.54610173],
[0.73931811, 0.54610173],
[0.73931811, 0.96692603],
[0.05022344, 0.96692603]],
[[0.51423357, 0.48105812],
[0.90815268, 0.48105812],
[0.90815268, 0.93234461],
[0.51423357, 0.93234461]],
[[0.18887874, 0.28019687],
[0.59905412, 0.28019687],
[0.59905412, 0.61594758],
[0.18887874, 0.61594758]],
[[0.00589912, 0.50042431],
[0.02952724, 0.50042431],
[0.02952724, 0.93056696],
[0.00589912, 0.93056696]],
[[0.2267573 , 0.04920763],
[0.43967406, 0.04920763],
[0.43967406, 0.92892994],
[0.2267573 , 0.92892994]],
[[0.2885361 , 0.70178778],
[0.73626986, 0.70178778],
[0.73626986, 0.71951917],
[0.2885361 , 0.71951917]],
[[0.50806757, 0.2228232 ],
[0.64801743, 0.2228232 ],
[0.64801743, 0.58853764],
[0.50806757, 0.58853764]],
[[0.52227217, 0.08670711],
[0.79866514, 0.08670711],
[0.79866514, 0.95127646],
[0.52227217, 0.95127646]],
[[0.09714329, 0.38051135],
[0.68042731, 0.38051135],
[0.68042731, 0.6425565 ],
[0.09714329, 0.6425565 ]],
[[0.28889578, 0.83151436],
[0.58681713, 0.83151436],
[0.58681713, 0.94071716],
[0.28889578, 0.94071716]],
[[0.2109561 , 0.05147464],
[0.52700784, 0.05147464],
[0.52700784, 0.21706122],
[0.2109561 , 0.21706122]]])
使用花哨的索引,可以避免不必要的堆叠数组:
>>> rand = np.random.rand(10, 4)
>>> rand
array([[0.9975164 , 0.24502806, 0.39619553, 0.07442783],
[0.57017416, 0.08316826, 0.16822264, 0.93233668],
[0.62260765, 0.77285638, 0.30093986, 0.76024642],
[0.26245902, 0.42329171, 0.31591031, 0.1685193 ],
[0.55910866, 0.33132002, 0.37912662, 0.99307642],
[0.02038662, 0.11176963, 0.72928163, 0.6766679 ],
[0.59143993, 0.03891871, 0.80613796, 0.34442057],
[0.47100568, 0.98703327, 0.60235766, 0.97791171],
[0.63499569, 0.94171562, 0.06308767, 0.78651194],
[0.679388 , 0.0464196 , 0.37712365, 0.76514397]])
>>> rand[:, :2].sort(1)
>>> rand[:, 2:].sort(1)
>>> rand[:, [0, 2, 1, 2, 1, 3, 0, 3]].reshape(-1, 4, 2)
array([[[0.24502806, 0.07442783],
[0.9975164 , 0.07442783],
[0.9975164 , 0.39619553],
[0.24502806, 0.39619553]],
[[0.08316826, 0.16822264],
[0.57017416, 0.16822264],
[0.57017416, 0.93233668],
[0.08316826, 0.93233668]],
[[0.62260765, 0.30093986],
[0.77285638, 0.30093986],
[0.77285638, 0.76024642],
[0.62260765, 0.76024642]],
[[0.26245902, 0.1685193 ],
[0.42329171, 0.1685193 ],
[0.42329171, 0.31591031],
[0.26245902, 0.31591031]],
[[0.33132002, 0.37912662],
[0.55910866, 0.37912662],
[0.55910866, 0.99307642],
[0.33132002, 0.99307642]],
[[0.02038662, 0.6766679 ],
[0.11176963, 0.6766679 ],
[0.11176963, 0.72928163],
[0.02038662, 0.72928163]],
[[0.03891871, 0.34442057],
[0.59143993, 0.34442057],
[0.59143993, 0.80613796],
[0.03891871, 0.80613796]],
[[0.47100568, 0.60235766],
[0.98703327, 0.60235766],
[0.98703327, 0.97791171],
[0.47100568, 0.97791171]],
[[0.63499569, 0.06308767],
[0.94171562, 0.06308767],
[0.94171562, 0.78651194],
[0.63499569, 0.78651194]],
[[0.0464196 , 0.37712365],
[0.679388 , 0.37712365],
[0.679388 , 0.76514397],
[0.0464196 , 0.76514397]]])
假设我有一个 4 列的二维数组,让我们将列命名为 x1、x2、y1、y2,数组的数据是 0 到 1 之间的浮点数,x2 总是大于 x1,y2 总是大于 y1.
每行代表一个矩形,其左下角位于(x1, y1),宽度为x2-x1,高度为y2-y1。
数组是这样创建的:
import numpy as np
bounds = np.random.uniform(size=(100, 4))
bounds[:,:2].sort(1)
bounds[:,2:].sort(1)
现在如何在不使用 for 循环的情况下按以下顺序从数组创建矩形顶点:左下角、右下角、右上角、左上角?
我能做到:
rectangles = [[(x1, y1), (x2, y1), (x2, y2), (x1, y2)] for x1, x2, y1, y2 in bounds]
但这首先违背了使用数组的目的。
相反,我可以这样做:
lower_left = np.array((bounds[:, 0], bounds[:, 2])).T
lower_right = np.array((bounds[:, 1], bounds[:, 2])).T
upper_right = np.array((bounds[:, 1], bounds[:, 3])).T
upper_left = np.array((bounds[:, 0], bounds[:, 3])).T
现在我只需要弄清楚如何水平连接列,以便列中的每一行都保留为子行。
我试过这样做:
np.hstack((lower_left, lower_right, upper_right, upper_left))
但它并没有完全按预期工作,字段已连接,出于与问题无关的原因,我需要将点分开。
如何使用矢量化获得与我的列表理解完全相同的结果?
编辑
我刚刚弄清楚如何将数组解包为列:
lower_left, lower_right, upper_right, upper_left = bounds.T
请记住,我在 Android phone 上写下了所有这些内容,而且我无法实际访问我可以在夜间进行编程的实际计算机(这是我的夜间)撰写本文时的语言环境),这是一个复杂的问题,我不会在这里讨论。
问题是我不能 运行 复杂的代码,对于仅使用内置函数或标准库的简单代码,我可以使用 w3c 学校的 Python TryIt 编辑器来 运行代码,但是对于任何需要导入通过 pip 安装的任何库的东西,我不能 运行 代码,所以我不能确定我的示例是否会 运行.
编辑
修复了示例代码中的错误,我将所有这些都写在 phone 上,所以我不能 运行 代码。
我已经完成了,我会post下面的代码,这样它可以帮助任何通过Google搜索在这里绊倒的人:
import numpy as np
values = np.random.random((100, 4))
values[:, :2].sort(1)
values[:, 2:].sort(1)
x1, x2, y1, y2 = values.T
ll = np.array((x1, y1)).T
lr = np.array((x2, y1)).T
ur = np.array((x2, y2)).T
ul = np.array((x1, y2)).T
rects = np.stack((ll, lr, ur, ul), axis=1)
rects.shape
它产生这个:
(100, 4, 2)
样本:
array([[[0.0296138 , 0.29131621],
[0.3693961 , 0.29131621],
[0.3693961 , 0.97544314],
[0.0296138 , 0.97544314]],
[[0.51630027, 0.39519657],
[0.59897507, 0.39519657],
[0.59897507, 0.76210139],
[0.51630027, 0.76210139]],
[[0.84594749, 0.42784315],
[0.86691432, 0.42784315],
[0.86691432, 0.79314211],
[0.84594749, 0.79314211]],
[[0.34412745, 0.21132195],
[0.68466905, 0.21132195],
[0.68466905, 0.25444418],
[0.34412745, 0.25444418]],
[[0.08766792, 0.39237762],
[0.89154757, 0.39237762],
[0.89154757, 0.44256892],
[0.08766792, 0.44256892]],
[[0.05022344, 0.54610173],
[0.73931811, 0.54610173],
[0.73931811, 0.96692603],
[0.05022344, 0.96692603]],
[[0.51423357, 0.48105812],
[0.90815268, 0.48105812],
[0.90815268, 0.93234461],
[0.51423357, 0.93234461]],
[[0.18887874, 0.28019687],
[0.59905412, 0.28019687],
[0.59905412, 0.61594758],
[0.18887874, 0.61594758]],
[[0.00589912, 0.50042431],
[0.02952724, 0.50042431],
[0.02952724, 0.93056696],
[0.00589912, 0.93056696]],
[[0.2267573 , 0.04920763],
[0.43967406, 0.04920763],
[0.43967406, 0.92892994],
[0.2267573 , 0.92892994]],
[[0.2885361 , 0.70178778],
[0.73626986, 0.70178778],
[0.73626986, 0.71951917],
[0.2885361 , 0.71951917]],
[[0.50806757, 0.2228232 ],
[0.64801743, 0.2228232 ],
[0.64801743, 0.58853764],
[0.50806757, 0.58853764]],
[[0.52227217, 0.08670711],
[0.79866514, 0.08670711],
[0.79866514, 0.95127646],
[0.52227217, 0.95127646]],
[[0.09714329, 0.38051135],
[0.68042731, 0.38051135],
[0.68042731, 0.6425565 ],
[0.09714329, 0.6425565 ]],
[[0.28889578, 0.83151436],
[0.58681713, 0.83151436],
[0.58681713, 0.94071716],
[0.28889578, 0.94071716]],
[[0.2109561 , 0.05147464],
[0.52700784, 0.05147464],
[0.52700784, 0.21706122],
[0.2109561 , 0.21706122]]])
使用花哨的索引,可以避免不必要的堆叠数组:
>>> rand = np.random.rand(10, 4)
>>> rand
array([[0.9975164 , 0.24502806, 0.39619553, 0.07442783],
[0.57017416, 0.08316826, 0.16822264, 0.93233668],
[0.62260765, 0.77285638, 0.30093986, 0.76024642],
[0.26245902, 0.42329171, 0.31591031, 0.1685193 ],
[0.55910866, 0.33132002, 0.37912662, 0.99307642],
[0.02038662, 0.11176963, 0.72928163, 0.6766679 ],
[0.59143993, 0.03891871, 0.80613796, 0.34442057],
[0.47100568, 0.98703327, 0.60235766, 0.97791171],
[0.63499569, 0.94171562, 0.06308767, 0.78651194],
[0.679388 , 0.0464196 , 0.37712365, 0.76514397]])
>>> rand[:, :2].sort(1)
>>> rand[:, 2:].sort(1)
>>> rand[:, [0, 2, 1, 2, 1, 3, 0, 3]].reshape(-1, 4, 2)
array([[[0.24502806, 0.07442783],
[0.9975164 , 0.07442783],
[0.9975164 , 0.39619553],
[0.24502806, 0.39619553]],
[[0.08316826, 0.16822264],
[0.57017416, 0.16822264],
[0.57017416, 0.93233668],
[0.08316826, 0.93233668]],
[[0.62260765, 0.30093986],
[0.77285638, 0.30093986],
[0.77285638, 0.76024642],
[0.62260765, 0.76024642]],
[[0.26245902, 0.1685193 ],
[0.42329171, 0.1685193 ],
[0.42329171, 0.31591031],
[0.26245902, 0.31591031]],
[[0.33132002, 0.37912662],
[0.55910866, 0.37912662],
[0.55910866, 0.99307642],
[0.33132002, 0.99307642]],
[[0.02038662, 0.6766679 ],
[0.11176963, 0.6766679 ],
[0.11176963, 0.72928163],
[0.02038662, 0.72928163]],
[[0.03891871, 0.34442057],
[0.59143993, 0.34442057],
[0.59143993, 0.80613796],
[0.03891871, 0.80613796]],
[[0.47100568, 0.60235766],
[0.98703327, 0.60235766],
[0.98703327, 0.97791171],
[0.47100568, 0.97791171]],
[[0.63499569, 0.06308767],
[0.94171562, 0.06308767],
[0.94171562, 0.78651194],
[0.63499569, 0.78651194]],
[[0.0464196 , 0.37712365],
[0.679388 , 0.37712365],
[0.679388 , 0.76514397],
[0.0464196 , 0.76514397]]])