-
-
Save mesutpiskin/0ced27981487491403610324fea55038 to your computer and use it in GitHub Desktop.
import numpy as np | |
import cv2 | |
import glob | |
# Define the chess board rows and columns | |
CHECKERBOARD = (6,9) | |
subpix_criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.1) | |
calibration_flags = cv2.fisheye.CALIB_RECOMPUTE_EXTRINSIC + cv2.fisheye.CALIB_CHECK_COND + cv2.fisheye.CALIB_FIX_SKEW | |
objp = np.zeros((1, CHECKERBOARD[0]*CHECKERBOARD[1], 3), np.float32) | |
objp[0,:,:2] = np.mgrid[0:CHECKERBOARD[0], 0:CHECKERBOARD[1]].T.reshape(-1, 2) | |
objpoints = [] # 3d point in real world space | |
imgpoints = [] # 2d points in image plane. | |
counter = 0 | |
for path in glob.glob('datasets/*.png'): | |
# Load the image and convert it to gray scale | |
img = cv2.imread(path) | |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
# Find the chess board corners | |
ret, corners = cv2.findChessboardCorners(gray, CHECKERBOARD, cv2.CALIB_CB_ADAPTIVE_THRESH+cv2.CALIB_CB_FAST_CHECK+cv2.CALIB_CB_NORMALIZE_IMAGE) | |
# Make sure the chess board pattern was found in the image | |
if ret: | |
objpoints.append(objp) | |
cv2.cornerSubPix(gray,corners,(3,3),(-1,-1),subpix_criteria) | |
imgpoints.append(corners) | |
#cv2.drawChessboardCorners(img, (rows, cols), corners, ret) | |
print(str(path)) | |
counter+=1 | |
N_imm = counter# number of calibration images | |
K = np.zeros((3, 3)) | |
D = np.zeros((4, 1)) | |
rvecs = [np.zeros((1, 1, 3), dtype=np.float64) for i in range(N_imm)] | |
tvecs = [np.zeros((1, 1, 3), dtype=np.float64) for i in range(N_imm)] | |
rms, _, _, _, _ = cv2.fisheye.calibrate( | |
objpoints, | |
imgpoints, | |
gray.shape[::-1], | |
K, | |
D, | |
rvecs, | |
tvecs, | |
calibration_flags, | |
(cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER, 30, 1e-6)) | |
img =cv2.imread("datasets/1.png") | |
map1, map2 = cv2.fisheye.initUndistortRectifyMap(K, D, np.eye(3), K, (340,240), cv2.CV_16SC2) | |
undistorted_img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT) | |
cv2.imshow('Original Image', img) | |
cv2.imshow('Undistort Image', undistorted_img) | |
cv2.waitKey(0) |
The intrinsic matrix, also known as the camera matrix, is a 3x3 matrix that contains the parameters of a camera's internal characteristics. It defines the relationship between the 3D world coordinates and the 2D image coordinates. The intrinsic matrix includes the focal length of the camera, the image sensor's pixel size, the principal point (the optical center of the lens), and other camera calibration parameters. The intrinsic matrix is typically computed during the camera calibration process and is used to correct the distortion in the images captured by the camera.
Hi,
Thanks for sharing. May I ask about your error (RMS) and how many images you use? Do you have any hints on how to get pictures (including tilting, different rotations angles etc.)?
Regards,
Ruben
Hi there,
Just for curiosity, is there any reason you are not using cv2.fisheye.undistortImage()
rather than the combination of initUndistortRectifyMap
and remap
?
what is intrinsic matrix