我正在使用 python 填充一个 3d 数组,每个数组元素代表一个像素。
我需要插入数组的值存储在一个非常大的 .txt 文件中(5600 万行,格式如下 - x、y、z、r、g、b)
现在我:
用零初始化 3d 数组。
逐行读取文件。
对于仅采用前 3 个元素 (x,y,z) 的每一行。
从 x 和 y 计算数组 location[i,j]
如果 array[i,j] 等于 0 --> 插入从文件中读取的行
否则跳到下一个文件
对于 5600 万行,我大约需要 160 秒
如何使用 python 加快速度?(GPU可用)
array = np.zeros((height, width), dtype=np.float32)
with open(point_cloud_file) as pc_file:
while True:
line = pc_file.readline()
if not line:
break
nof_read_lines += 1
new_line = line.strip()
try:
x, y, z, _, _, _ = new_line.split(',')
except:
nof_skipped_lines += 1
continue
# insert to array
pixel_x = some calculation
pixel_y = some calculation
if 0 < pixel_x < width and 0 < pixel_y < height:
if array[int(pixel_y), int(pixel_x), 0] == 0:
array[int(pixel_y), int(pixel_x), :] = x, y, z
else:
nof_skipped_lines += 1 # pixel already filled with values