はじめに
困っている人がいたので数か月前にやってみたやつです。
開発環境
- Touch Designer
- OpenCV
- Python
導入
始め方はこちらを参考にします。
マーカーの生成
arucoマーカーを30種類生成します。
import cv2
aruco = cv2.aruco
dictionary = aruco.getPredefinedDictionary(aruco.DICT_4X4_50)
def arGenerator():
for i in range(30):
fileName = "{}.png".format(i)
generator = aruco.drawMarker(dictionary, i, 100)
cv2.imwrite(fileName, generator)
cv2.waitKey(0)
arGenerator()
可視化
検出されたcornersをscript1で可視化します。
import cv2
import numpy as np
def onPostCook(changeOp):
image = changeOp.numpyArray(delayed=True)
image = np.array(image)
image = np.uint8(image * 255.0)
image = np.flipud(image)
image = cv2.cvtColor(image, cv2.COLOR_RGBA2BGR)
out = op('script1')
out.clear()
aruco = cv2.aruco
dictionary = aruco.getPredefinedDictionary(aruco.DICT_4X4_50)
corners, ids, rejectedImgPoints = aruco.detectMarkers(image, dictionary)
aruco.drawDetectedMarkers(image, corners, ids, (255, 0, 0))
if len(corners) > 0:
out.numSamples = len(corners)
else:
out.numSamples = 1
x0 = out.appendChan('x0')
y0 = out.appendChan('y0')
x1 = out.appendChan('x1')
y1 = out.appendChan('y1')
x2 = out.appendChan('x2')
y2 = out.appendChan('y2')
x3 = out.appendChan('x3')
y3 = out.appendChan('y3')
if len(corners) > 0:
for index, corner in enumerate(corners):
x0[index] = corner[0][0][0]
y0[index] = corner[0][0][1]
x1[index] = corner[0][1][0]
y1[index] = corner[0][1][1]
x2[index] = corner[0][2][0]
y2[index] = corner[0][2][1]
x3[index] = corner[0][3][0]
y3[index] = corner[0][3][1]
else:
x0[0] = 0.
y0[0] = 0.
x1[0] = 0.
y1[0] = 0.
x2[0] = 0.
y2[0] = 0.
x3[0] = 0.
y3[0] = 0.
return
実行
お疲れ様でした。