I want to calibrate my camera. The goal is to remove the distortion and don't crop the image(like on the last photo). I do it in the such way:
1)load images, find the corners of the chessboards
import cv2
import numpy as np
import os
import glob
import sys
CHECKERBOARD = (7,7)
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)
_img_shape = None
objpoints = [] # 3d point in real world space
imgpoints = [] # 2d points in image plane.
images = os.listdir("./calib_snapshots")
gray = None
for fname in images:
fname = os.path.join("./calib_snapshots",fname)
if not os.path.isfile(fname):
continue
img = cv2.imread(fname)
if _img_shape == None:
_img_shape = img.shape[:2]
else:
assert _img_shape == img.shape[:2], "All images must share the same size."
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)
# If found, add object points, image points (after refining them)
if ret == True:
objpoints.append(objp)
cv2.cornerSubPix(gray,corners,(3,3),(-1,-1),subpix_criteria)
imgpoints.append(corners)
2)then calculate camera matrix and distortion coefficients:
DIM =_img_shape[::-1]
K = np.zeros((3, 3))
D = np.zeros((4, 1))
rvecs = [np.zeros((1, 1, 3), dtype=np.float64) for i in range(N_OK)]
tvecs = [np.zeros((1, 1, 3), dtype=np.float64) for i in range(N_OK)]
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)
)
map1, map2 = cv2.fisheye.initUndistortRectifyMap(K, D, np.eye(3), K, DIM, cv2.CV_16SC2)
my input image:
4) And when i remap my image - i get such cropped image in the result:
img = cv2.imread("goingcrazy.jpg")
undistorted_img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR)
How can i get such not-cropped image?
P.S i have the working solution in c++, and tried such thing: I printed the camera matrix and distortion coefficients from the calibrate() results, copited it from the c++ terminal, and passed to the initUndistortRectifyMap in python (with exactly the same parameters as in the c++), but i get different map1 and map2, and the image is still cropped...
Have found the solution:
image = cv2.imread("fisheye.jpg")
distCoeff = np.zeros((4,1),np.float64)
distCoeff[0,0] = k1
distCoeff[1,0] = k2
distCoeff[2,0] = 0
distCoeff[3,0] = 0
cam = np.eye(3,dtype=np.float32)
cam[0,2] = image.shape[1]/2.0 # define center x
cam[1,2] = image.shape[0]/2.0 # define center y
cam[0,0] = 10. # define focal length x
cam[1,1] = 10. # define focal length y
nk = cam.copy()
scale = 2
nk[0,0]=cam[0,0]/scale #scaling
nk[1,1]=cam[1,1]/scale
undistorted = cv2.undistort(image,cam,distCoeff, None, nk)