OpenCV Calibration Script
This file contains 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 the opencv library | |
import cv2 | |
import numpy as np | |
# define a video capture object (continuinity camera) | |
vid = cv2.VideoCapture(1) | |
# Define the dimensions of checkerboard | |
CHECKERBOARD = (6, 9) | |
# stop the iteration when specified | |
# accuracy, epsilon, is reached or | |
# specified number of iterations are completed. | |
criteria = (cv2.TERM_CRITERIA_EPS + | |
cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001) | |
# Vector for 3D points | |
threedpoints = [] | |
# Vector for 2D points | |
twodpoints = [] | |
# 3D points real world coordinates | |
objectp3d = np.zeros((1, CHECKERBOARD[0] | |
* CHECKERBOARD[1], | |
3), np.float32) | |
objectp3d[0, :, :2] = np.mgrid[0:CHECKERBOARD[0], | |
0:CHECKERBOARD[1]].T.reshape(-1, 2) | |
prev_img_shape = None | |
while(True): | |
# Capture the video frame | |
# by frame | |
ret, frame = vid.read() | |
image = frame | |
if not ret: | |
print("Skipping") | |
continue | |
grayColor = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) | |
print("Iteration") | |
# cv2.imshow('img', grayColor) | |
# cv2.waitKey(1) | |
# continue | |
# Find the chess board corners | |
# If desired number of corners are | |
# found in the image then ret = true | |
ret, corners = cv2.findChessboardCorners( | |
grayColor, CHECKERBOARD, | |
cv2.CALIB_CB_ADAPTIVE_THRESH | |
+ cv2.CALIB_CB_FAST_CHECK + | |
cv2.CALIB_CB_NORMALIZE_IMAGE) | |
# If desired number of corners can be detected then, | |
# refine the pixel coordinates and display | |
# them on the images of checker board | |
if ret == True: | |
print("Return is TRUE") | |
threedpoints.append(objectp3d) | |
# Refining pixel coordinates | |
# for given 2d points. | |
corners2 = cv2.cornerSubPix( | |
grayColor, corners, (11, 11), (-1, -1), criteria) | |
twodpoints.append(corners2) | |
# Draw and display the corners | |
image = cv2.drawChessboardCorners(image, | |
CHECKERBOARD, | |
corners2, ret) | |
cv2.imshow('img', image) | |
key = cv2.waitKey(1) & 0xFF | |
if key == ord('q'): | |
break | |
# After the loop release the cap object | |
vid.release() | |
cv2.destroyAllWindows() | |
h, w = image.shape[:2] | |
# Perform camera calibration by | |
# passing the value of above found out 3D points (threedpoints) | |
# and its corresponding pixel coordinates of the | |
# detected corners (twodpoints) | |
ret, matrix, distortion, r_vecs, t_vecs = cv2.calibrateCamera( | |
threedpoints, twodpoints, grayColor.shape[::-1], None, None) | |
# Displaying required output | |
print(" Camera matrix:") | |
print(matrix.tolist()) | |
print("\n Distortion coefficient:") | |
print(distortion.tolist()) | |
# import pdb; pdb.set_trace() | |
# print("\n Rotation Vectors:") | |
# print(r_vecs) | |
# print("\n Translation Vectors:") | |
# print(t_vecs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment