简体   繁体   English

如何将 x1、x2、y1、y2 数组转换为矩形顶点数组?

[英]How do I turn x1, x2, y1, y2 array into rectangular vertices array?

Say I have a two dimensional array of 4 columns, let's name the columns x1, x2, y1, y2, the data of the array are floats between 0 and 1, x2 is always larger than x1 and y2 is always larger than y1.假设我有一个 4 列的二维数组,我们将列命名为 x1、x2、y1、y2,数组的数据是 0 到 1 之间的浮点数,x2 总是大于 x1,y2 总是大于 y1。

Each row represents a rectangle whose lower left corner is at (x1, y1) and whose width is x2-x1 and whose height is y2-y1.每行代表一个矩形,其左下角在 (x1, y1),宽度为 x2-x1,高度为 y2-y1。

The array is created like this:数组是这样创建的:


import numpy as np

bounds = np.random.uniform(size=(100, 4))
bounds[:,:2].sort(1)
bounds[:,2:].sort(1)

Now how do I create rectangular vertices from the array in this order: lower left, lower right, upper right, upper left, without using for loops?现在如何按以下顺序从数组创建矩形顶点:左下、右下、右上、左上,而不使用 for 循环?

I can do this:我可以做这个:


rectangles = [[(x1, y1), (x2, y1), (x2, y2), (x1, y2)] for x1, x2, y1, y2 in bounds]

But that defeats the purpose of using arrays in the first place.但这首先违背了使用数组的目的。

Instead I can do these:相反,我可以这样做:

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

Now I only need to figure out how to join the columns horizontally so that each row in the columns stays a sub row.现在我只需要弄清楚如何水平连接列,以便列中的每一行都保持一个子行。

I have tried to do this:我试图这样做:

np.hstack((lower_left, lower_right, upper_right, upper_left))

But it isn't exactly working as intended, the fields are joined, I need the points to stay separate for reasons that are irrelevant to the question.但它并没有完全按预期工作,字段已连接,出于与问题无关的原因,我需要将这些点分开。

How can I achieve exactly the same result as my list comprehension using vectorization?如何使用矢量化获得与列表理解完全相同的结果?


Edit编辑

I just figured out how to unpack array to columns:我刚刚想出了如何将数组解包到列:

lower_left, lower_right, upper_right, upper_left = bounds.T

Keep in mind I wrote all of these on an Android phone and I don't have physical access to actual computer on which I can do programming during nighttime (it is nighttime at my locale at time of writing), it is a complicated matter I won't discuss here.请记住,我在 Android 手机上编写了所有这些内容,并且我无法实际访问可以在夜间进行编程的实际计算机(在撰写本文时,这是我所在地区的夜间),这是一件复杂的事情,我不会在这里讨论。

The thing is I can't run complex code, for simple code using only built-in functions or standard library I can use w3c school's Python TryIt editor to run the code, but for anything requiring imports of any library installed via pip, I can't run the code, so I can't know for sure if my example will run.问题是我无法运行复杂代码,对于仅使用内置函数或标准库的简单代码,我可以使用 w3c 学校的 Python TryIt 编辑器来运行代码,但对于需要导入通过 pip 安装的任何库的任何内容,我可以'不运行代码,所以我不能确定我的示例是否会运行。


Edit编辑

Fixed bug in example code, I wrote all of these on a phone so I can't run the code.修复了示例代码中的错误,我在手机上写了所有这些,所以我无法运行代码。

With fancy indexes, you can avoid unnecessary stacked arrays:使用精美的索引,您可以避免不必要的堆叠数组:

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

I have already done it, I will post the code below just so that it may help anyone who stumbles here by Google searching:我已经完成了,我将在下面发布代码,以便它可以帮助任何通过谷歌搜索在这里绊倒的人:

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

It produces this:它产生这个:

(100, 4, 2)

Sample:样本:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 numpy 数组 [(x1,y1),(x2,y2),(x3,y3),...(xn,yn)] 转换为 [(x1,y1,x1^2,y1^2),(x2 ,y2,x2^2,y2^2),…(xn,yn,xn^2,yn^2)] - How to convert a numpy array [(x1,y1),(x2,y2),(x3,y3),…(xn,yn)] to [(x1,y1,x1^2,y1^2),(x2,y2,x2^2,y2^2),…(xn,yn,xn^2,yn^2)] 切片 numpy 数组的多个帧,具有多个 y1:y2, x1:x2 - Slice multiple frame of numpy array with multiple y1:y2, x1:x2 如何将 yolov3 补丁坐标数组转换为边界框数组 (x1,y1, x2,y2) - how to convert yolov3 patch coordinates array to bounding box array (x1,y1, x2,y2) 将二维数组转换为 [ [ x0 y0 ] [x1 y1] [x2 y2] ] 形式 - Converting 2D array into [ [ x0 y0 ] [x1 y1] [x2 y2] ] form 如何从列表中表达 x1, y1, x2, y2 - How to express x1, y1, x2, y2 from a list Python中如何将YOLO格式标注转换为x1,y1,x2,y2坐标? - How to convert YOLO format annotations to x1, y1, x2, y2 coordinates in Python? 如何在Python中缩放一条线的长度并获得相应的坐标((x1,y1),(x2,y2))? - How can I scale the length of a line and obtain the corresponding co-ordinates ((x1, y1), (x2, y2)) in Python? 如何计算x,y坐标的点相对于直线的比例百分比(也有x1,y1,x2,y2) - How to calculate the proportional percentage of a point of an x,y coordinate with respect to a line (also with x1, y1, x2, y2) 使用matplotlib.pyplot [[x1,y1],[x2,y2]]绘制点 - draw points using matplotlib.pyplot [[x1,y1],[x2,y2]] 通过完全控制自动将鼠标从 X1、Y1 移动到 X2、Y2 - Automate Mouse Movement from X1,Y1 to X2,Y2 with complete control
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM