Created
July 26, 2017 12:42
-
-
Save sakamoto-poteko/fcfe1c00e0769182cbdf5662a232ff8b to your computer and use it in GitHub Desktop.
OpenCV camera calibration and undistort
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import cv2 | |
import glob | |
# termination criteria | |
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001) | |
cbrow = 7 | |
cbcol = 9 | |
# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0) | |
objp = np.zeros((cbrow*cbcol,3), np.float32) | |
objp[:,:2] = np.mgrid[0:cbcol,0:cbrow].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 = glob.glob('*.jpg') | |
for fname in images: | |
img = cv2.imread(fname) | |
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) | |
# Find the chess board corners | |
ret, corners = cv2.findChessboardCorners(gray, (cbcol,cbrow),None) | |
# If found, add object points, image points (after refining them) | |
if ret == True: | |
print "%s: success" % fname | |
objpoints.append(objp) | |
cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria) | |
imgpoints.append(corners) | |
# Draw and display the corners | |
cv2.drawChessboardCorners(img, (cbcol,cbrow), corners,ret) | |
cv2.imshow('img',img) | |
cv2.waitKey(150) | |
else: | |
print "%s: failed" % fname | |
cv2.imshow('img',img) | |
cv2.waitKey(1) | |
cv2.destroyAllWindows() | |
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None) | |
print "mtx" | |
print mtx | |
print "dist" | |
print dist | |
mean_error = 0 | |
for i in xrange(len(objpoints)): | |
imgpoints2, _ = cv2.projectPoints(objpoints[i], rvecs[i], tvecs[i], mtx, dist) | |
error = cv2.norm(imgpoints[i],imgpoints2, cv2.NORM_L2)/len(imgpoints2) | |
mean_error += error | |
print "total error: ", mean_error/len(objpoints) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import cv2 | |
import glob | |
mtx = np.array([[460.71671753, 0., 288.27396648], [0., 462.18652446, 239.00294897], [0., 0., 1.]]) | |
dist = np.array([-0.35122887, 0.1698431, 0, 0, -0.05083561]) | |
images = glob.glob('*.jpg') | |
for fname in images: | |
img = cv2.imread(fname) | |
h,w = img.shape[:2] | |
newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (w, h), 0, (w, h)) | |
dst = cv2.undistort(img, mtx, dist, None, newcameramtx) | |
# crop the image | |
x,y,w,h = roi | |
dst = dst[y:y+h, x:x+w] | |
cv2.imshow('undist', dst) | |
cv2.waitKey(800) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment