-2

Pythonを使用して3D配列を埋めています。各配列要素はピクセルを表します。

配列に挿入する必要がある値は、非常に大きな .txt ファイルに格納されています (5,600 万行、次のようにフォーマットされています - x、y、z、r、g、b)

今私は:

  1. 3 次元配列をゼロで初期化します。

  2. ファイルを 1 行ずつ読み取ります。

  3. 最初の 3 つの要素 (x、y、z) のみを取得する各行に対して。

  4. x と y から配列の位置 [i,j] を計算します

  5. 配列[i,j] がゼロの場合 --> ファイルから読み取った行を挿入

  6. そうでなければ次のファイルにスキップ

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
4

1 に答える 1