【发布时间】:2022-02-03 04:59:41
【问题描述】:
我有一个简单的数据框结构,如下所示:
print(scene_2d_df.head())
x y z
0 963 1691.0 0
1 911 1881.0 0
2 837 864.0 1
3 785 1054.0 0
4 897 59.0 0
print(scene_2d_df.shape)
(2294591, 3)
每一行代表图像中的一个白点或黑点(1 或 0)。 x 和 y 列是像素位置。图像约为。在这种情况下为 1200 x 1800。我有我认为有效的代码,但即使在现代机器上运行也非常缓慢。这种方法有点暴力。
def construct_image_from_df(df_1):
xmax = int(df_1.max(axis=0)['x'])
xmin = int(df_1.min(axis=0)['x'])
ymax = int(df_1.max(axis=0)['y'])
ymin = int(df_1.min(axis=0)['y'])
zmax = int(df_1.max(axis=0)['z'])
zmin = int(df_1.min(axis=0)['z'])
print("xmin :: " + str(xmin) + " // xmax :: " + str(xmax)) # 1200-something
print("ymin :: " + str(ymin) + " // ymax :: " + str(ymax)) # 1800-something
print("zmin :: " + str(zmin) + " // zmax :: " + str(zmax)) # 1, all values 0 or 1
img = np.zeros((xmax, ymax))
length = df_1.shape[0] # number of rows
for i in range(0, length):
x, y, z = int(df_1.iloc[i]['x']), int(df_1.iloc[i]['y']), int(df_1.iloc[i]['z'])
img[x - 1, y - 1] = z
return img
基本上,我抓取数据帧的每一行,并手动将像素写入我的 2D img 数组。速度很慢。
有没有更快(可能是矢量化)的方法来做到这一点?
【问题讨论】: