-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCameraCalibrationTest.py
66 lines (51 loc) · 2.24 KB
/
CameraCalibrationTest.py
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
62
63
64
import cv2
import numpy as np
import os
import math
# termination criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((9*6,3), np.float32)
objp[:,:2] = np.mgrid[0:9,0:6].T.reshape(-1,2)
# Arrays to store object points and image points from all the images.
objpoints = [] # 3d point in real world space
imgpoints = [] # 2d points in image plane.
images = "/home/pi/Pictures/IntrensicCameraCalibrationImages"
for imgFileName in os.listdir(images):
print "Processing ", imgFileName
if imgFileName[-4:] != ".png":
print "skipping..."
continue
fullFileName = os.path.join(images, imgFileName)
img = cv2.imread(fullFileName)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
h,w = gray.shape[:2]
# Find the chess board corners
#ret, corners = cv2.findCirclesGrid(gray, (4, 11), None, cv2.CALIB_CB_ASYMMETRIC_GRID)
ret, corners = cv2.findChessboardCorners(gray, (9, 6))
# If found, add object points, image points (after refining them)
if ret == True:
objpoints.append(objp)
corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
#print 'corners2', corners2
imgpoints.append(corners2)
# Draw and display the corners
#img = cv2.drawChessboardCorners(img, (6,8), corners2,ret)
#small = cv2.resize(img, (0,0), fx = 0.3, fy = 0.3)
#cv2.imshow('window',img)
#cv2.waitKey(0)
cv2.destroyAllWindows()
print "calibrating camera..."
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, (w,h),None,None)
print 'mtx is: ', mtx
print 'dist is ', dist
meanError = 0
for i in xrange(len(objpoints)):
imagePoints2, none = cv2.projectPoints(objpoints[i], rvecs[i], tvecs[i], mtx, dist)
error = cv2.norm(imgpoints[i], imagePoints2, cv2.NORM_L2)/len(imagePoints2)
meanError += error
print "Total Error: ", meanError/len(objpoints)
with open('/home/pi/Desktop/NameOfRaspberryPi') as f:
m_nameOfRaspberryPi = f.read()
np.save(('/home/pi/test/AndromedaVision/' + m_nameOfRaspberryPi + '/mtx.npy'), mtx)
np.save(('/home/pi/test/AndromedaVision/' + m_nameOfRaspberryPi + '/dist.npy'), dist)