0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

np.meshgridを使って格子配列を生成する

Posted at

はじめに

とりあえず調べたことを何かしらの形にするための記事なのでまずは殴り書きからはじめる
参考

np.meshgridとは

配列の各要素から格子配列を作成するもの
np.meshgrid(arg1,arg2)
arg1: 横軸
arg2: 縦軸
というふうに考えることができる.
最終的にはarg1*arg2の大きさの配列が返ってくる.
2次元の例:

x = [0,1,2,3]
y = [0,1,2,3]
xx, yy = np.meshrod(x,y)

###
xx = [[0,1,2,3],
      [0,1,2,3],
      [0,1,2,3],
      [0,1,2,3]]

yy = [[0,0,0,0],
      [1,1,1,1],
      [2,2,2,2],
      [3,3,3,3]]

3次元の例 indexing='ij'として行列形式にしたもの:

x = [0,1,2,3]
y = [0,1,2,3]
z = [0,1,2,3]
xx, yy, zz = np.meshrod(x,y,z)

###
xx = [[[0,0,0,0],
       [0,0,0,0],
       [0,0,0,0],
       [0,0,0,0]],

      [[1,1,1,1],
       [1,1,1,1],
       [1,1,1,1],
       [1,1,1,1]],

      [[2,2,2,2],
       [2,2,2,2],
       [2,2,2,2],
       [2,2,2,2]],

      [[3,3,3,3],
       [3,3,3,3],
       [3,3,3,3],
       [3,3,3,3]]]



yy = [[[0,0,0,0],
       [1,1,1,1],
       [2,2,2,2],
       [3,3,3,3]],

      [[0,0,0,0],
       [1,1,1,1],
       [2,2,2,2],
       [3,3,3,3]],

      [[0,0,0,0],
       [1,1,1,1],
       [2,2,2,2],
       [3,3,3,3]],

      [[0,0,0,0],
       [1,1,1,1],
       [2,2,2,2],
       [3,3,3,3]]]

zz = [[[0,1,2,3],
       [0,1,2,3],
       [0,1,2,3],
       [0,1,2,3]],

      [[0,1,2,3],
       [0,1,2,3],
       [0,1,2,3],
       [0,1,2,3]],

      [[0,1,2,3],
       [0,1,2,3],
       [0,1,2,3],
       [0,1,2,3]],

      [[0,1,2,3],
       [0,1,2,3],
       [0,1,2,3],
       [0,1,2,3]]]

あんまりスッと歯理解できないが雰囲気でわかる

今回やりたかったこと

[0,0,0], [0,0,1], [0,0,2] ... [2,1,0], [2,1,1], [2,1,2], ...[3,3,3]
という格子点群を作りたかった

これらの点群座標を作成する方法を以下に示す

x = np.arange(0, 4)
y = np.arange(0, 4)
z = np.arange(0, 4)

xx, yy, zz = np.meshgrid(x, y, z, indexing='ij')
result = np.vstack((xx.flatten(), yy.flatten(), zz.flatten())).T
pcd.points = o3d.utility.Vector3dVector(result)
# 可視化
o3d.visualization.draw_geometries([pcd])

作った点群を可視化した結果が以下となる.
スクリーンショット 2023-04-08 19.55.53.png

さいごに

初めて知ったメソッドだったが点群処理などの座標情報を用いる必要がある時に有用だと感じた

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?