Pythonを使用して3D配列を埋めています。各配列要素はピクセルを表します。
配列に挿入する必要がある値は、非常に大きな .txt ファイルに格納されています (5,600 万行、次のようにフォーマットされています - x、y、z、r、g、b)
今私は:
3 次元配列をゼロで初期化します。
ファイルを 1 行ずつ読み取ります。
最初の 3 つの要素 (x、y、z) のみを取得する各行に対して。
x と y から配列の位置 [i,j] を計算します
配列[i,j] がゼロの場合 --> ファイルから読み取った行を挿入
そうでなければ次のファイルにスキップ
5,600 万行の場合、約 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