-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaliblation.py
More file actions
61 lines (49 loc) · 2.06 KB
/
Copy pathcaliblation.py
File metadata and controls
61 lines (49 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
カメラの内部パラメータを計算する
square_sizeとreference_imgを変更することで正方形の1辺のサイズと参照画像の枚数を変更できる。
"""
import cv2
import matplotlib.pyplot as plt
import numpy as np
square_size = 2.6 # 正方形の1辺のサイズ[cm]
pattern_size = (7, 7) # 交差ポイントの数
reference_img = 100 # 参照画像の枚数
pattern_points = np.zeros( (np.prod(pattern_size), 3), np.float32 ) #チェスボード(X,Y,Z)座標の指定 (Z=0)
pattern_points[:,:2] = np.indices(pattern_size).T.reshape(-1, 2)
pattern_points *= square_size
objpoints = []
imgpoints = []
capture = cv2.VideoCapture(0)
capture.set(cv2.CAP_PROP_FPS, 30)
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
while len(objpoints) < reference_img:
# 画像の取得
ret, img = capture.read()
height = img.shape[0]
width = img.shape[1]
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# チェスボードのコーナーを検出
ret, corner = cv2.findChessboardCorners(gray, pattern_size)
# コーナーがあれば
if ret == True:
print("detected coner!")
print(str(len(objpoints)+1) + "/" + str(reference_img))
term = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1)
cv2.cornerSubPix(gray, corner, (5,5), (-1,-1), term)
imgpoints.append(corner.reshape(-1, 2)) #appendメソッド:リストの最後に因数のオブジェクトを追加
objpoints.append(pattern_points)
cv2.imshow('image', img)
# 毎回判定するから 200 ms 待つ.遅延するのはココ
if cv2.waitKey(1000) & 0xFF == ord('q'):
break
print("calculating camera parameter...")
# 内部パラメータを計算
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)
# 計算結果を保存
np.save("mtx", mtx) # カメラ行列
np.save("dist", dist.ravel()) # 歪みパラメータ
# 計算結果を表示
print("RMS = ", ret)
print("mtx = \n", mtx)
print("dist = ", dist.ravel())