camera_calibration

#################### camcal1 -start1 : demo_camcal_from_webcam2b.py, a simple interactive tool for calibration of webcam ################

#demo_camcal_from_webcam2b.py, 2021 Jan 6, khwong

#known problem: Give absolute measurement of Translation in mm, required measurement and entering of square size checkers in mm.

import numpy as np

import cv2

import glob

#interactive camera calibration demo, first beta version (ref: opencv (cv2) documentation)

# run > python demo_camcal_from_webcam1.py # (not from files as many other examples)

#Show the checker board to the webcam

#See the command windows for instruction , ‘y’ or ‘n’ , or ‘q’, enter at lease 3 images,the more the better

#‘q’ to complete, you will see the camera intrinsic or extrinsic parameters

#results store in calib_result.txt of the current directory

#

WAIT_TIME = 1000

# 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((6*7,3), np.float32)

objp[:,:2] = np.mgrid[0:7,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 = glob.glob('calib_images/*.jpg') # if you have the sample files

#for xx in range(6): #set max to 6

key=0

capture = cv2.VideoCapture(0) #labtop cam, for external usb-cam :cv2.VideoCapture(1)

while(key !='q'):

# print(xx)

# key=0

# while(key !='q'):

ret, frame = capture.read()

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

cv2.imshow('video gray', gray)

#cv2.imshow('video original', frame)

ret, corners = cv2.findChessboardCorners(gray, (7,6),None)

print(ret)

cv2.waitKey(1) #this is neccsary , otherwsie you cannot see the screen

# if cv2.waitKey(1) == 27:

# break

if ret == True:

print(corners)

frame = cv2.drawChessboardCorners(frame, (7,6), corners,ret)

cv2.imshow("frame",frame)

cv2.waitKey(WAIT_TIME)

key=input('Accept this tracking resust or not, to complete and quit:\n (y or n or q) > ')

if key == 'y':

objpoints.append(objp)

print('object point found')

corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)

imgpoints.append(corners2)

# Draw and display the corners

#img = cv2.drawChessboardCorners(img, (7,6), corners2,ret)

print('result accepted and appended')

if key== 'q':

print('DONE')

break

else:

print('result not accepted xxx')

cv2.destroyWindow("frame");

#break

ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)

print(mtx)

print(dist)

print(rvecs)

print(tvecs)

cv_file = cv2.FileStorage("calib_result.txt", cv2.FILE_STORAGE_WRITE)

cv_file.write("camera_matrix2", mtx)

cv_file.write("dist_coeff2", dist)

# note you *release* you don't close() a FileStorage object

cv_file.release()

capture.release()

cv2.destroyAllWindows()

################### camcal1 -end- : demo_camcal_from_webcam2b.py, a simple interactive tool for calibration of webcam ################