Skip to content

Instantly share code, notes, and snippets.

@shks
shks / projectPoints_with_matrix.py
Created September 2, 2021 02:52
projectPoints with matrix / 投影計算をマトリクスで行う
# カメラ内部パラメータ(内部パラメータ行列)
# ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)
# -----reading camera intrinsic matrix
cameraMatrix = np.loadtxt("calib/cameraMatrix.csv",delimiter=",")
distCoeffs = np.loadtxt("calib/distCoeffs.csv",delimiter=",")
# 外部パラメータ行列
# https://gist.github.com/shks/e3cd5fc22ad9f4ddbccd5312a64803c3
@shks
shks / opencv_rvec_tvec_to_matrix.py
Created September 2, 2021 02:42
opencv rvec, tvec into matrix34 and 44 / opencvの姿勢推定で得たrvec, tvecベクトルをマトリクス表現へ変換する
retval, rvec, tvec = aruco.estimatePoseBoard(corners, ids, board, cameraMatrix, distCoeffs, None, None)
#rvec, tvec : 3 float vector
#ロドリゲス公式で、rvecから回転行列へ変換する
Rmatrix, Jacob = cv2.Rodrigues(rvec)
# [3 x 4 matrix]で、R|t マトリクスを作成
RTmatrix34_L = np.concatenate([Rmatrix, tvec], axis=1)
@shks
shks / estimatePoseBoard_cv2_td_saveRT.py
Created September 2, 2021 02:38
estimatePoseBoard from TD and save RT matrix
# -load camera instrinsic matrix
cameraMatrix = np.loadtxt("calib/cameraMatrix.csv",delimiter=",")
distCoeffs = np.loadtxt("calib/distCoeffs.csv",delimiter=",")
# prepare the 'same' aruco board
aruco = cv2.aruco
dictionary = aruco.getPredefinedDictionary(aruco.DICT_4X4_50)
board = cv2.aruco.GridBoard_create(5, 5, 0.2,0.1,dictionary)
# retval = cv.aruco.GridBoard_create( markersX, markersY, markerLength, markerSeparation, dictionary[
@shks
shks / CharucoBoard_output.py
Created August 23, 2021 05:04
CharucoBoard file出力
from cv2 import aruco
import cv2
aruco_dict = aruco.Dictionary_get(aruco.DICT_6X6_250)
board = aruco.CharucoBoard_create(28, 16, 200, 140, aruco_dict)
imboard = board.draw((1920,1080))
cv2.imwrite('CharucoBoard.png', imboard)
@shks
shks / tdTOP_cv2_grayscale.py
Last active September 1, 2021 18:02
tdTOP_cv2_grayscale
import cv2
import numpy as np
arr = op('td_top').numpyArray('delayed=True')
gray = arr[:, :, 0]
gray = gray * 255.0
gray = gray.astype(np.uint8)
#flip
gray = cv2.flip(gray, 0)
@shks
shks / jpegs_into_mov.py
Created January 18, 2021 04:56
merge jpegs into mov
import sys
import cv2
import glob
_list = glob.glob("./some/*", recursive=True)
_list.sort()
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
video = cv2.VideoWriter('./some/video.mp4',fourcc, 60.0, (1024, 1024))
@shks
shks / get_file_list.py
Created January 18, 2021 04:52
特定ファイルリストを取得
import glob
_list = glob.glob("*.mov", recursive=True)
for fname in _list:
sampleName = os.path.basename(fname).split('.', 1)[0]
print(sampleName)
@shks
shks / video_into_jpegs.py
Created January 18, 2021 04:50
split video into jpegs. ビデオをフレーム画像に分割する
import cv2
import os
def save_all_frames(video_path, dir_path, basename, ext='jpg'):
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
return
os.makedirs(dir_path, exist_ok=True)
@shks
shks / ScriptTOP_matplotlib.py
Last active April 28, 2022 15:43
rendering matplotlib in ScriptTOP TouchDesigner
# me - this DAT
# scriptOp - the OP which is cooking
import numpy as np
import cv2
import io
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('Agg')
@shks
shks / doShapirotest.py
Last active January 2, 2021 18:29
doShapirotest in python
from scipy.stats import shapiro
#https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.shapiro.html
def doShapirotest(data):
stat, p = shapiro(data)
print("Shapirotest Wstat , p-value", stat, p)
if(p < 0.05):
print("USE NON-PARAMETRIC TEST :(p < 0.05)")
else:
print("USE PARAMETRIC TEST : (p > 0.05)")