Add x-y vertex coordinates to empty numpy.ndarray

頂点座標を空のnumpy.ndarrayに追加する操作。
よくやるのでメモ。

—(sample code)—–

import cv2
import numpy as np

## cat pol_int   (x-y point list)
## -126 133
## -126 131
## -117 131
## -117 134


in_data = open("./plist")
lines = in_data.readlines()

arr = np.empty((0,2), int)

for i in range(len(lines)):

  lines[i].strip()
  jj = lines[i].split()

  xx = int(jj[0])
  yy = int(jj[1])
  arr = np.append(arr, np.array([[xx, yy]]), axis=0)

  i = i + 1

print(arr)

—(exec result)—–
[[-126 133]
[-126 131]
[-117 131]
[-117 134]]

—(memo)—-
initialize ndarray for x-y point list.
at first array is empty(no entry) and add_type is [x,y].
so initialize parameter is (0,2).

arr = np.empty((0,2), int)

if append for ndarray, append element must ndarray.
we use only 1 axis, so axis = 0(axis memo)

arr = np.append(arr, np.array([[xx, yy]]), axis=0)

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA