Skip to content

Commit 1e587c8

Browse files
committed
save calibration npz
1 parent d8b1340 commit 1e587c8

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

ch200_Extra_modules/aruco/Camera Calibration using ChArUco and Python/aruco_test1.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
allCorners = []
2929
allIds = []
3030
decimator = 0
31-
for i in range(100):
31+
for i in range(200):
3232

3333
ret, frame = cap.read()
3434
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
@@ -57,6 +57,9 @@
5757
cal = cv2.aruco.calibrateCameraCharuco(allCorners, allIds, board, imsize, None, None)#return retval, cameraMatrix, distCoeffs, rvecs, tvecs
5858
print(cal)
5959
retval, cameraMatrix, distCoeffs, rvecs, tvecs = cal#TODO 然后怎么办?
60+
#TODO saveCameraParams
61+
np.savez('calib.npz',mtx=cameraMatrix,dist=distCoeffs,rvecs=rvecs,tvecs=tvecs)
62+
# np.savez(outfile, x=x, y=y)
6063
except:
6164
cap.release()
6265

Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2017/7/27 18:04
3+
# @Author : play4fun
4+
# @File : calibrateCamera2.py
5+
# @Software: PyCharm
6+
7+
"""
8+
calibrateCamera2.py:
9+
"""
10+
11+
import cv2
12+
import numpy as np
13+
14+
15+
def draw_axis(img, charuco_corners, charuco_ids, board):
16+
vecs = np.load("./calib.npz") # I already calibrated the camera
17+
mtx, dist, _, _ = [vecs[i] for i in ('mtx', 'dist', 'rvecs', 'tvecs')]
18+
ret, rvec, tvec = cv2.aruco.estimatePoseCharucoBoard(
19+
charuco_corners, charuco_ids, board, mtx, dist)
20+
if ret is not None and ret is True:
21+
cv2.aruco.drawAxis(img, mtx, dist, rvec, tvec, 0.1)
22+
23+
24+
def get_image(camera):
25+
ret, img = camera.read()
26+
return img
27+
28+
29+
def make_grayscale(img):
30+
ret = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
31+
return ret
32+
33+
34+
def main():
35+
camera = cv2.VideoCapture(0)
36+
img = get_image(camera)
37+
while True:
38+
cv2.imshow('calibration', img)
39+
cv2.waitKey(10)
40+
img = get_image(camera)
41+
gray = make_grayscale(img)
42+
corners, ids, rejected = cv2.aruco.detectMarkers(gray, aruco_dict,
43+
corners, ids)
44+
cv2.aruco.drawDetectedMarkers(img, corners, ids)
45+
if ids is not None and corners is not None \
46+
and len(ids) > 0 and len(ids) == len(corners):
47+
diamond_corners, diamond_ids = \
48+
cv2.aruco.detectCharucoDiamond(img, corners, ids,
49+
0.05 / 0.03, cameraMatrix=mtx,
50+
distCoeffs=dist)
51+
cv2.aruco.drawDetectedDiamonds(img, diamond_corners, diamond_ids)
52+
'''if diamond_ids is not None and len(diamond_ids) >= 4:
53+
break'''
54+
board = cv2.aruco.CharucoBoard_create(9, 6, 0.05, 0.03,
55+
aruco_dict)
56+
if diamond_corners is not None and diamond_ids is not None \
57+
and len(diamond_corners) == len(diamond_ids):
58+
count, char_corners, char_ids = \
59+
cv2.aruco.interpolateCornersCharuco(diamond_corners,
60+
diamond_ids, gray,
61+
board)
62+
if count >= 3:
63+
draw_axis(img, char_corners, char_ids, board)
64+
65+
66+
if __name__ == '__main__':
67+
main()

0 commit comments

Comments
 (0)