cv2:去扭曲后不需要的裁剪(initUndistortRectifyMap)

cv2: Unwanted cropping after dewarping (initUndistortRectifyMap)

我想校准我的相机。目标是消除失真并且不裁剪图像(如上一张照片)。我是这样做的:

1)加载图片,找到棋盘角

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)然后计算相机矩阵和畸变系数:

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)
    )
  1. 然后计算地图:
map1, map2 = cv2.fisheye.initUndistortRectifyMap(K, D, np.eye(3), K, DIM, cv2.CV_16SC2)

我的输入图像: 4) 当我重新映射我的图像时 - 我在结果中得到这样的裁剪图像:

img = cv2.imread("goingcrazy.jpg")
undistorted_img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR)

我得到了什么:

想要的图片:

我怎样才能得到这样的未裁剪图像?

P.S 我有 C++ 的工作解决方案,并尝试过这样的事情:我从 calibrate() 结果中打印相机矩阵和畸变系数,从 C++ 终端复制它,并传递给 initUndistortRectifyMap在 python 中(与 c++ 中的参数完全相同),但我得到不同的 map1 和 map2,并且图像仍然被裁剪...

已找到解决方案:


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)